17

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>
4

4 回答 4

26

根据您使用的 Spring 版本,这里有一些选项:

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

你应该让你的 urls/actions root-relative 特定于你的上下文路径。

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

注意:Spring 3.2.3 引入servletRelativeAction但我从未使用过。

春天 3.2

不要做任何事情,上下文路径是预先设置的 - 这实际上是一个重大更改并最终回滚

<form:form action="/account/login" method="post"> 
//will produce action="/springmvc/account/login"
于 2013-09-05T12:56:09.713 回答
8

使用/.

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

如果不这样做,您就是在告诉浏览器将该操作附加到地址栏上已经存在的 URL。

如果您在 HTML 中直接有此类链接(不使用 Spring 的form:form),请尝试使用c:url正确构造 URL,包括上下文路径等。这使构建正确的相对 URL 变得很痛苦。

<a href="<c:url value="/account/register" />">Register</a>
于 2013-09-05T08:19:52.163 回答
0

我尝试通过仅设置相对路径来使用 spring 标记,它会自动附加上下文路径,例如:

<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html lang="en">

<head>
<!-- ... -->
<spring:url value="/account/login" var="loginUrl" />
<form:form action="${loginUrl}" method="post">

上下文路径在 application.properties 中设置如下:

server.servlet.contextPath=/MyApp

在 jsp 页面中,它产生:

<a class="nav-link" href="/MyApp/account/login"> <i class="fas fa-play"></i> <span>Click here</span></a>
于 2020-01-16T11:22:30.857 回答
0

用于../获取当前上下文根的 URL:

<form:form action="../account/login" method="post"> 

这里

于 2018-05-21T12:28:20.077 回答