Reputation: 11
I have login application there I am checking whether username is available or not, if it is not available then it will redirect to login.jsp and show message in login.jsp else move to index.jsp.but if i give wrong user name and password then everytime it is appending controller to url everytime i.e. two times validation check ,two times controller will be added i.e. http://localhost/cms/controller/controller/secureuserlogin
that I don't want and I also don't want to change url pattern as well i.e./controller/*. How to redirect with out changing url while redirecting to same page? Here's my code:
Upvotes: 0
Views: 2257
Reputation: 369
Linking to a png of your sample code is not very nice. SO has syntax highlighting features so it would be better to paste your code here in a code block or use something like pastebin..
Anyway, on to the answer..
You can have more control over redirects using RedirectView, but I think the problem that you're having is because your controllers do not map to your redirects.
Try:
// Returns login page
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login() {
ModelAndView modelAndView = new ModelAndView("views/login");
return modelAndView;
}
// Handles login submittion
@RequestMapping(value = "/loginSubmit", method = RequestMethod.POST) {
public String loginSubmitController(@Valid UserModel user, BindingResult bindingResult) {
RedirectView view;
// In production you would want to actually present errors to user
if(!bindingResult.hasErrors() && validUserCredentials(user)) {
return "redirect:/index";
}
return "redirect:/login";
}
Your JSP should POST
to /loginSubmit
.
If you are planning on using very standard SOAP sort of authentication (so a login page with page reload on POST and redirects), then it might be easier to use an existing framework (like Spring Security if you are already using Spring).
Of course, with that in mind these frameworks can become a hindrance if you need something more custom and you can easily fall into the trap of fighting your framework instead of it helping you.
If you are keen on using the Spring Security framework, try this quickstart guide. There are of course many other such frameworks, some might be better for your uses, or simpler ;) than others.
Upvotes: 1