Spring MVC:发布表单时附加的 URL 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18631147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:12:31  来源:igfitidea点击:

Spring MVC: Url path appending when posting the form

springjspspring-mvc

提问by manivannan

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/loginsecong time in same page: //localhost:8090/springmvc/account/account/login. how do i fix this redirecting problem?

我是 spring mvc 的新手。我创建了一个简单的登录应用程序。但在我的情况下,第一次正确发布 url 和调用控制器方法。在第二次它附加了一个控制器的路径。第一次发帖: //localhost:8090/springmvc/account/login同一页面的第二次登录时间://localhost:8090/springmvc/account/account/login。我如何解决这个重定向问题?

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:

这是我的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>

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>

回答by ikumen

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

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

Spring 3.1 and lower OR Spring 3.2.3 and higher

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

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

您应该拥有特定于您的上下文路径的 urls/actions root-relative。

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

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

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

Spring 3.2

春天 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"

回答by adarshr

Start your form action with a /.

/.开始您的表单操作。

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

By not doing it, you're telling the browser to append the action to the already existing URL on the address bar.

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

And where you have such links directly in HTML (by not using Spring's form:form), try to use c:urlto properly construct the URL including the context path etc. This takes a lot of pain away from building proper relative URLs.

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

<a href="<c:url value="/account/register" />">Register</a>

回答by Majid

Use ../to get URL of your current context root:

使用../让您的当前上下文根的URL:

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

from here

这里

回答by Shessuky

I tried with the spring tag bysetting only the relative path, it appends automatically the context path like:

我尝试通过仅设置相对路径来使用 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">

The context path is set in application.properties as bellow:

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

server.servlet.contextPath=/MyApp

In the jsp page, it produces:

在jsp页面中,它产生:

<a class="nav-link" href="/MyApp/account/login"> <i class="fas fa-play"></i> <span>Click here</span></a>