I have a Spring MVC Controller with handlers like so:
@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";
}
The form in the login.html page POSTs to account/login (the same url). I’d like that after the authentication, I redirect the user to the home page of my application, so that he sees www.mywebappexample.com in address bar instead of www.mywebappexample.com/account/login. When I return the string from the login method, it renders the proper html but I don’t have the URL I want to show. How can I redirect?
Edit: I had to prefix my controller return String with redirect:. This works if you have a view resolver that subclasses UrlBasedViewResolver UrlBasedViewResolver. Thymeleaf’s view resolver doesn’t do that but it does have the behavior -> ThymeleafViewResolver. Here’s my servlet-context.xml (I’m using thymeleaf):
<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>
You can use a redirect in the tag instead which should update the URL in the browser window: