I'm currently trying to implement the Post / Redirect / Get pattern using Spring MVC 3.1. What is the correct way to save and restore model data + validation errors? I know that I can save the model and BindingResult using RedirectAttributes in the POST method. But what is the correct way to recover them in the GET method from the flash area?
I did the following for POST:
@RequestMapping(value = "/user/create", method = RequestMethod.POST) public String doCreate(@ModelAttribute("user") @Valid User user, BindingResult result, RedirectAttributes rA){ if(result.hasErrors()){ rA.addFlashAttribute("result", result); rA.addFlashAttribute("user", user); return "redirect:/user"; } return "redirect:/user/success"; }
And to get the user creation form:
@RequestMapping(value = "/user", method = RequestMethod.GET) public ModelAndView showUserForm(@ModelAttribute("user") User user, ModelAndView model){ model.addObject("user", user); model.setViewName("userForm"); return model; }
This allows me to save user data in case of an error. But what is the correct way to recover errors? (BindingResult) I would like to show them in a form with Spring form tags:
<form:errors path="*" />
Also, it would be interesting how to access the flash area from the get method?
source share