Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read flash attributes after redirection in Spring MVC 3.1?

I would like to know how to read a flash attributes after redirection in Spring MVC 3.1.

I have the following code:

@Controller @RequestMapping("/foo") public class FooController {    @RequestMapping(value = "/bar", method = RequestMethod.GET)   public ModelAndView handleGet(...) {     // I want to see my flash attributes here!   }    @RequestMapping(value = "/bar", method = RequestMethod.POST)   public ModelAndView handlePost(RedirectAttributes redirectAttrs) {     redirectAttrs.addFlashAttributes("some", "thing");     return new ModelAndView().setViewName("redirect:/foo/bar");   }  } 

What I am missing?

like image 954
Rubens Mariuzzo Avatar asked Aug 01 '12 16:08

Rubens Mariuzzo


People also ask

How use redirect attribute in Spring MVC?

You can use RedirectAttributes to store flash attributes and they will be automatically propagated to the "output" FlashMap of the current request. A RedirectAttributes model is empty when the method is called and is never used unless the method returns a redirect view name or a RedirectView.

How to GET Flash attribute in Spring?

In order to use Flash attribute in your Spring MVC application make sure you using version 3.1 or above. Also add mvc:annotation-driven to spring-servlet. xml file. Once this is done, Flash attribute is automatically set “on” for usage.

What is Flash attributes in Spring?

Flash attributes are saved temporarily before the redirect (typically in the session) to be made available to the request after the redirect and removed immediately. Spring MVC has two main abstractions in support of flash attributes.

What is FlashMap?

A FlashMap provides a way for one request to store attributes intended for use in another. This is most commonly needed when redirecting from one URL to another -- e.g. the Post/Redirect/Get pattern.


1 Answers

Use Model, it should have flash attributes prepopulated:

@RequestMapping(value = "/bar", method = RequestMethod.GET) public ModelAndView handleGet(Model model) {   String some = (String) model.asMap().get("some");   // do the job } 

or, alternatively, you can use RequestContextUtils#getInputFlashMap:

@RequestMapping(value = "/bar", method = RequestMethod.GET) public ModelAndView handleGet(HttpServletRequest request) {   Map<String, ?> inputFlashMap = RequestContextUtils.getInputFlashMap(request);   if (inputFlashMap != null) {     String some = (String) inputFlashMap.get("some");     // do the job   } } 

P.S. You can do return return new ModelAndView("redirect:/foo/bar"); in handlePost.

EDIT:

JavaDoc says:

A RedirectAttributes model is empty when the method is called and is never used unless the method returns a redirect view name or a RedirectView.

It doesn't mention ModelAndView, so maybe change handlePost to return "redirect:/foo/bar" string or RedirectView:

@RequestMapping(value = "/bar", method = RequestMethod.POST) public RedirectView handlePost(RedirectAttributes redirectAttrs) {   redirectAttrs.addFlashAttributes("some", "thing");   return new RedirectView("/foo/bar", true); } 

I use RedirectAttributes in my code with RedirectView and model.asMap() method and it works OK.

like image 64
Xaerxess Avatar answered Sep 29 '22 01:09

Xaerxess