Spring MVC: Url path appending when posting the form

前端 未结 4 883
情深已故
情深已故 2020-12-24 00:45

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

相关标签:
4条回答
  • 2020-12-24 01:02

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

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

    from here

    0 讨论(0)
  • 2020-12-24 01:05

    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"
    
    0 讨论(0)
  • 2020-12-24 01:05

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

    <!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:

    server.servlet.contextPath=/MyApp
    

    In the jsp page, it produces:

    <a class="nav-link" href="/MyApp/account/login"> <i class="fas fa-play"></i> <span>Click here</span></a>
    
    0 讨论(0)
  • 2020-12-24 01:08

    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.

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

    <a href="<c:url value="/account/register" />">Register</a>
    
    0 讨论(0)
提交回复
热议问题