How do I enhance the Controller below to utilize Spring MVC’s Flash Attributes? The use case is a copy function.
POST/REQUEST/GET implementation:
- client clicks “copy” button in the UI
- server sets the response “Location” header
- client redirects to “path/to/page?copy”
- server provides ModelAndView
- client (jQuery success function) sets window.location
FooController redirect method:
@RequestMapping(value = "{fooId}", method = POST, params = { "copy" })
@Transactional
@ResponseStatus(CREATED)
public void getCopyfoo(@PathVariable String fooId,
HttpServletResponse response, RedirectAttributes redirectAttrs) {
response.setHeader("Location", uriPath);
//no worky?!:
redirectAttrs.addFlashAttribute("barKey", "barValue");
}
FooController get method:
@RequestMapping(value = "{fooId}", method = GET)
@Transactional(readOnly = true)
public ModelAndView findFooById(@PathVariable String fooId,
HttpServletRequest request){
Map<String, ?> map = RequestContextUtils.getInputFlashMap(request);
// map is empty...
return modelAndViewFromHelperMethod();
}
I’m affraid that
RedirectAttributeswork only withRedirectView,so Your controller should return for example:
If you realy need to handle server response with JS, then maybe handling HTTP status 302 would help.