Spring MVC映射 - 为什么路径在变化?

时间:2014-03-30 11:03:39

标签: java spring spring-mvc path navigation

我有这段代码:

    @RequestMapping(value="login/authUser", method=RequestMethod.POST)
public String authUser(@RequestParam("login") String login,
                        @RequestParam("password") String password, ModelMap model)
(...)

哪个应该捕获用户的身份验证:

<div id ="loginBar">
        <form id="loginForm" action="login/authUser" method="post">
            <label>Login:</label>
            <input id="login" type="text" title="podaj login" name="login"/><br><br>
            <label>Hasło:</label>
            <input id ="password" type="password" title="podaj haslo, minimum 8 znaków" name="password"><br>
            <input id ="loginAuthorisationFailed" type="text"/><br>
            <input id="loginButton" type="submit" value="Zaloguj"><br>
        </form>
    </div>

如果不好,它应该返回index.jsp和关于坏数据的信息

(...)
else{
            model.put("info", "Bad data!");
            return "index";

信息转到h1 HTML标记:

 <h1>${info}</h1>

这是我在浏览器中看到的路径:

http://localhost:8084/pracainz/login/authUser

但是我尝试再次登录,浏览器中的路径不同:

http://localhost:8084/pracainz/login/login/authUser

并且Tomcat向我显示错误:

The requested resource () is not available.

它增加了额外的“/ login”,我不知道为什么。我能做些什么来阻止表单以这种方式运行,我可以做什么来强制,每当我尝试将表单发送到Spring时重置“路径?”

不幸的是,这不起作用:

action="${pageContext.servletContext.contextPath}/login/authUser"

如果有人想帮助我,我会很高兴。

提前谢谢!

解决: 解决方案:

我应该使用:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
(...)

<form:form id="loginForm" action="${pageContext.servletContext.contextPath}/login/authUser" method="post">
 (...)
</form:form>

相反简单的HTML标记

1 个答案:

答案 0 :(得分:0)

使用Spring标记库,更具体地说,使用spring:url来创建正确的操作URL。查看this SO问题了解详情