Spring MVC supports us to redirect to another request after processing the current request. In this tutorial, we will learn how to do what I just said.
As you know, after processing the request, the controller typically selects a view to display the returned data to the user. In case you want to redirect after processing a request, we just need to add “redirect:/” before the URL to redirect, then Spring will automatically redirect to the URL we want. Of course this URL must be in our system!
Take a look at this example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.springmvc; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HomeController { @RequestMapping(value = "/doLogin", method = RequestMethod.POST) public String doLogin() { return "redirect:/home"; } } |
In the above example, after processing the login request, our site will automatically redirect to the “/home” page.