Spring MVC redirects URL after login

I have a Spring MVC controller with these handlers:

@RequestMapping(value = "/account/login", method = RequestMethod.GET) public String login() { return "login"; } @RequestMapping(value = "/account/login", method = RequestMethod.POST, params = "login") public String login(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) { // do authentication return "home"; } 

Form in POST login.html files for account / login (same URL). I would like after authentication to redirect the user to the main page of my application, so that he sees www.mywebappexample.com in the address bar instead of www.mywebappexample.com/account/login . When I return the string from the login method, it displays the correct html, but I don't have the URL that I want to show. How can I redirect?

Edit: I had to prefix my controller to return String using redirect: This works if you have a view definition tool that subclasses UrlBasedViewResolver UrlBasedViewResolver . Thymeleaf view resolver does not do this, but has a behavior - > ThymeleafViewResolver . Here is my servlet-context.xml (I use thimeleaf):

  <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".html" /> <property name="templateMode" value="HTML5" /> </bean> <bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver" /> </bean> <bean id = "viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine" /> <property name="order" value = "1"/> </bean> 
+4
source share
2 answers

Instead, you can use the redirect in the tag, which should update the URL in the browser window:

 return "redirect:home"; 
+5
source

Check if authentication is successful and then redirects the request with the request manager

 RequestDispatcher rd = servletContext.getRequestDispatcher("/pathToResource"); rd.forward(request, response); 
0
source

All Articles