Define RedirectAttributes
method parameter in the the handler
method that handles form submission
from first page:
@RequestMapping("/sendDataToNextPage", method = RequestMethod.POST)
public String submitForm(
@ModelAttribute("formBackingObj") @Valid FormBackingObj formBackingObj,
BindingResult result,
RedirectAttributes redirectAttributes) {
...
DataObject data = new DataObject();
redirectAttributes.addFlashAttribute("dataForNextPage", data);
...
return "redirect:/secondPageURL";
}
The flash attributes are saved temporarily before the redirect (typically in the session) and are available to the request after the redirect and removed immediately.
The above redirect will cause the client (browser) to send a request to /secondPageURL
. So you need to have a handler method to handle this request, and there you can get access to the DataObject data
set in the submitForm
handler method:
@RequestMapping(value = "/secondPageURL", method = RequestMethod.GET)
public String gotoCountrySavePage(
@ModelAttribute("dataForNextPage") DataObject data,
ModelMap model) {
...
//data is the DataObject that was set to redirectAttributes in submitForm method
return "pageToBeShown";
}
Here DataObject data
is the object that contains data from the submitForm
method.