Spring @RequestMapping is doubling up in URL upon multiple submits
I'm working with Spring's annotations for the first time and I'm having some issues with the URL doubling up on subsequent hits of @RequestMapping
. I have the following snippet:
@Controller @RequestMapping("/login")
public class Login {
private LoginService loginService;
@Autowired
public Login(LoginService loginService){
this.loginService = loginService;
}
@RequestMapping(method=RequestMethod.GET)
public String setupLogin(){ return "login"; }
@RequestMapping(method=RequestMethod.GET, value="/retry")
public String setupLoginRetry(){ return "login"; }
@RequestMapping(method=RequestMethod.POST)
public String processLogin(@ModelAttribute("userName") String userName, @ModelAttribute("password") String password){
if (true) return "redirect:login/retry";开发者_开发技巧 //hard-coded for example
return "redirect:home";
}
}
If I bring up the page and just hit the submit a bunch of times I get the following:
myApp/login
myApp/login/retry?userName=&password=
myApp/login/login // <-- this fails as it shouldn't be nesting logins
So obviously, I'm doing something wrong. My questions are:
1.) What can I do to prevent the parameters from showing up in the URL when it goes to retry? Edit: Removing this question - found the answer.
2.) Why does it start nesting logins and what is the proper way to declare this?
Any thoughts or assistance would be appreciated. Thanks!
Probably it's caused by the fact that you use relative URL as an action
attribute of the form in the login
view. Since page containing this form can be displayed in response to different URL, you need to use absolute URLs, something like this:
<spring:url var = "loginUrl" value = "/login" />
<form:form action = "${loginUrl}" ...>...</form:form>
精彩评论