Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: Url path appending when posting the form

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>
like image 427
manivannan Avatar asked Sep 05 '13 08:09

manivannan


2 Answers

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"
like image 98
ikumen Avatar answered Oct 02 '22 03:10

ikumen


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>
like image 44
adarshr Avatar answered Oct 02 '22 03:10

adarshr