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>
manivannan
  • 622
  • 1
  • 4
  • 17

4 Answers4

26

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"
ikumen
  • 11,275
  • 4
  • 41
  • 41
8

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>
adarshr
  • 61,315
  • 23
  • 138
  • 167
  • 1
    if i am using / beofre the action link like means browser not taking the application name in path url like this //localhost:8090/account/login – manivannan Sep 05 '13 at 08:31
0

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

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

from here

Majid
  • 13,853
  • 15
  • 77
  • 113
0

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>
Shessuky
  • 1,846
  • 21
  • 24