【问题标题】:Spring MVC mapping - why path is changing?Spring MVC 映射 - 为什么路径会发生变化?
【发布时间】:2014-03-30 11:03:38
【问题描述】:

我有这段代码:

    @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 标记

【问题讨论】:

    标签: java spring spring-mvc path navigation


    【解决方案1】:

    使用 Spring 标记库,更具体地说是 spring:url,为操作创建正确的 url。详情请查看this SO question

    【讨论】:

    • 谢谢,我也找到了不同的解决方案;我应该使用
      Spring 标签而不是简单的 HMTL
    猜你喜欢
    • 2015-01-25
    • 2015-01-18
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    相关资源
    最近更新 更多