Spring MVC Redirect Views Example
In a Spring application you may need to redirect to a view for many reasons
1. On a form submission, a controller will get the POST data to process. If after processing the data we internally forward to request to another Controller, then the new Controller will also see the same POST data used by the old Controller. This may lead to problems if the new Controller was expecting to see other Data.
2. Another problem is the multiple submission of the same form if we do a internal forward after the form submission.
Spring MVC provides ways to redirect to views using various techniques. In this post lets see the various Spring MVC Redirect Views examples.
1. Using RedirectView
We can use a instance of the
RedirectView to redirect to the view we want. RedirectView behaves diffently as supposed to to normal view resolution. The DispatcherServlet will not interfere in the RedirectView works :). RedirectView calls the
HttpServletResponse.sendRedirect()
to send a HTTP redirect to the client.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.kscodes.sampleproject.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.view.RedirectView; import com.kscodes.sampleproject.model.Employee; @Controller public class EmployeeController { @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) public RedirectView addEmployee(@ModelAttribute("employee") Employee employee) { // Your Implementation return new RedirectView("welcome"); } } |
2. Using redirect prefix
Another way to redirect to a view is using the redirect prefix.
redirect prefix is associated with a view name, where we need to redirect the page to. Using redirect prefix is a better way of handling redirects as it is not directly coupled to the the Controller.
As you can see, when using the
RedirectView the Controller has to knows that a redirection will be happening. But this couples things too tightly. Controller should not care how the response is handled. Hence using redirect prefix is more useful.
UrlBasedViewResolver resolves the view name associated with the redirect prefix.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.kscodes.sampleproject.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.kscodes.sampleproject.model.Employee; @Controller public class EmployeeController { @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) public String addEmployee(@ModelAttribute("employee") Employee employee) { // Your Implementation return "redirect:/welcome"; } } |
3. Using forward prefix
Similar to redirect prefix, we can use the forward prefix.
UrlBasedViewResolver resolves the view name associated with the forward prefix.
RequestDispatcher.forward() is used for the forward prefix.
1 2 3 4 5 6 7 | @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) public String addEmployee(@ModelAttribute("employee") Employee employee) { // Your Implementation return "forward:/welcome"; } |
How to pass attributes to the Redirected views
RedirectAttributes can be used to pass attributes to the Redirected Views. If the method redirects, the RedirectAttributes are used. Otherwise the the model is used. To use the RedirectAttributes we need to declare it as the methods parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @Controller public class EmployeeController { @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) public RedirectView addEmployee(@ModelAttribute("employee") Employee employee, RedirectAttributes redirectAttributes) { redirectAttributes.addAttribute("msg", "Welcome to the Company"); redirectAttributes.addFlashAttribute("firstName", employee.getFirstName()); redirectAttributes.addFlashAttribute("lastName", employee.getLastName()); return new RedirectView("welcome"); } } |