Spring MVC:发布表单时追加URL路径 [英] Spring MVC: Url path appending when posting the form

查看:237
本文介绍了Spring MVC:发布表单时追加URL路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring MVC的新手.我创建了一个简单的登录应用程序.但以我为例,这是第一次用于正确发布网址并正确调用控制器方法.第二次是在追加路径时再加上一次控制器.第一次发布: //localhost:8090/springmvc/account/login 在同一页面上的时间://localhost:8090/springmvc/account/account/login .我该如何解决此重定向问题?

这是我的控制器页面:

@Controller
@RequestMapping("account")
public class AccountController {
    AccountService service = new AccountService();
    @RequestMapping(value = "account/default", method = RequestMethod.GET)
    public ModelAndView RegisterUser() {
        return new ModelAndView("/Account/Index","command",new User());
    }

    @RequestMapping(value = "/registeruser", method = RequestMethod.POST)
    public ModelAndView RegisterUser(User user) {
        user.setMessage(service.Register(user));
        return new ModelAndView("/Account/Index", "command", user);
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ModelAndView RegisterUer(User user) {
        user.setMessage(service.Register(user));
        return new ModelAndView("/Account/create", "command", user);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView LoginUser(User user, ModelMap model) {
        String msg = service.isAuthendicated(user) ? "Logged in" : "Failed";
        user.setMessage(msg);
        return new ModelAndView("/Account/Index", "command", user);
    }
}

这是我的jsp页面:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>

<t:genericpage>
    <jsp:body>
       <h2>Login</h2>
       <div>
            ${command.message} </div>
      <a href="account/register">Register</a>
    <form:form action="account/login" method="post">
        <div>
                <form:input path="username" />
            </div>
        <div>
                <form:input path="password" />
            </div>
        <input type="submit" value="Login">
    </form:form>
    </jsp:body>
</t:genericpage>

我在普通页面上使用了标签库:

<%@tag description="Master Page" pageEncoding="UTF-8"%>
<html>
<body>
    <div id="pageheader">
        <h2>WElcome</h2>
    </div>
    <div id="body">
        <jsp:doBody />
    </div>
    <div id="pagefooter">
        <p id="copyright">Copyright</p>
    </div>
</body>
</html>

解决方案

根据您使用的Spring版本,以下是一些选项:

Spring 3.1或更低版本或Spring 3.2.3或更高版本

您应该相对于您的上下文路径具有与根目录相对的网址/操作.

<form:form action="${pageContext.request.contextPath}/account/login" method="post">

注意:Spring 3.2.3引入了servletRelativeAction,但我从未使用过它.

Spring 3.2

不做任何事情,上下文路径是前置的-这实际上是一个打破更改并最终滚动返回.

<form:form action="/account/login" method="post"> 
//will produce action="/springmvc/account/login"

i am new to spring mvc. i created a simple login application. but in my case the first time the for posting url and calling controller method correctly. in second time it's appending path with one more time of controller. first time post: //localhost:8090/springmvc/account/login secong time in same page: //localhost:8090/springmvc/account/account/login. how do i fix this redirecting problem?

this my controller page:

@Controller
@RequestMapping("account")
public class AccountController {
    AccountService service = new AccountService();
    @RequestMapping(value = "account/default", method = RequestMethod.GET)
    public ModelAndView RegisterUser() {
        return new ModelAndView("/Account/Index","command",new User());
    }

    @RequestMapping(value = "/registeruser", method = RequestMethod.POST)
    public ModelAndView RegisterUser(User user) {
        user.setMessage(service.Register(user));
        return new ModelAndView("/Account/Index", "command", user);
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ModelAndView RegisterUer(User user) {
        user.setMessage(service.Register(user));
        return new ModelAndView("/Account/create", "command", user);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView LoginUser(User user, ModelMap model) {
        String msg = service.isAuthendicated(user) ? "Logged in" : "Failed";
        user.setMessage(msg);
        return new ModelAndView("/Account/Index", "command", user);
    }
}

this my jsp page:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>

<t:genericpage>
    <jsp:body>
       <h2>Login</h2>
       <div>
            ${command.message} </div>
      <a href="account/register">Register</a>
    <form:form action="account/login" method="post">
        <div>
                <form:input path="username" />
            </div>
        <div>
                <form:input path="password" />
            </div>
        <input type="submit" value="Login">
    </form:form>
    </jsp:body>
</t:genericpage>

i used the tag library for common page:

<%@tag description="Master Page" pageEncoding="UTF-8"%>
<html>
<body>
    <div id="pageheader">
        <h2>WElcome</h2>
    </div>
    <div id="body">
        <jsp:doBody />
    </div>
    <div id="pagefooter">
        <p id="copyright">Copyright</p>
    </div>
</body>
</html>

解决方案

Depending on which version of Spring you're using, here are some options:

Spring 3.1 and lower OR Spring 3.2.3 and higher

You should have your urls/actions root-relative specific to your context path.

<form:form action="${pageContext.request.contextPath}/account/login" method="post">

Note: Spring 3.2.3 introduced servletRelativeAction but I've never used it.

Spring 3.2

Don't do anything, context path is prepended - this was actually a breaking change and eventually rolled back.

<form:form action="/account/login" method="post"> 
//will produce action="/springmvc/account/login"

这篇关于Spring MVC:发布表单时追加URL路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆