You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC登录表单提交后无法跳转页面问题的修复方案咨询

Spring MVC登录表单跳转故障修复方案

1. 补全Controller类的类级路径映射

你的Controller类缺少类级别的@RequestMapping注解,导致方法级路径无法被正确匹配。添加类级注解统一管理路径前缀:

@Controller
@RequestMapping("/auth") // 添加类级路径前缀
public class LoginController {

    @RequestMapping("/home")
    public String showForm() {
        return "home";
    }

    @RequestMapping(path = "/handleForm", method = RequestMethod.POST)
    public String handleForm(HttpServletRequest request) {
        String name = request.getParameter("username");
        String pass = request.getParameter("password");
        if (name != null && pass != null) {
            if (name.equals("Twinkle") && pass.equals("twinkle123")) {
                return "student-list";
            } else {
                return "home";
            }
        } else {
            return "error";
        }
    }
}

2. 修正表单提交路径

表单中使用相对路径handleForm会导致提交路径错误(比如当前页面URL为/auth/home时,提交路径会变成/auth/home/handleForm),无法匹配Controller的/auth/handleForm。使用Spring JSTL标签生成正确的绝对路径:

<!-- 在JSP顶部引入JSTL核心标签库 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<div class="container mt-5">
    <h3 class="text-center">Welcome, Login Here</h3>

    <!-- 用<c:url>生成正确的提交路径 -->
    <form action="<c:url value='/auth/handleForm'/>" method="POST">
        <div class="form-group">
            <label for="username">User Name</label>
            <input type="text" class="form-control"
                   id="username"
                   placeholder="Enter here"
                   name="username">
        </div>

        <div class="form-group">
            <label for="password">Password</label>
            <input type="password" class="form-control" id="password" minlength="4"
                   maxlength="8" placeholder="Enter here" name="password">
        </div>

        <div class="container mt-5 text-center">
            <button type="submit" class="btn btn-success">Submit</button>
        </div>
    </form>
</div>

如果不想用JSTL,也可以直接写完整绝对路径:action="/你的应用上下文路径/auth/handleForm",但<c:url>会自动处理上下文路径,适配性更强。

3. 检查视图解析器配置

确保Spring配置文件中的视图解析器前缀、后缀与你的JSP文件位置匹配,比如:

<!-- spring-servlet.xml 或相关配置文件 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/> <!-- JSP文件所在目录 -->
    <property name="suffix" value=".jsp"/> <!-- 文件后缀 -->
</bean>

需保证home.jsp、student-list.jsp、error.jsp都存放在配置的前缀目录下(如/WEB-INF/views/)。

4. 可选:简化参数获取方式

替换HttpServletRequest的参数获取方式,使用@RequestParam更符合Spring MVC规范,也能避免参数名拼写错误:

@RequestMapping(path = "/handleForm", method = RequestMethod.POST)
public String handleForm(@RequestParam("username") String name, 
                         @RequestParam("password") String pass) {
    if (name != null && pass != null) {
        if (name.equals("Twinkle") && pass.equals("twinkle123")) {
            return "student-list";
        } else {
            return "home";
        }
    } else {
        return "error";
    }
}

内容的提问来源于stack exchange,提问作者user97

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 04:54:11