My understanding so far is on our controller request mapping method we can specify RedirectAttributes parameter and populate it with attributes for when the request gets redirected.
Example:
@RequestMapping(value="/hello", method=GET)
public String hello(RedirectAttributes redirAttr)
{
// should I use redirAttr.addAttribute() or redirAttr.addFlashAttribute() here ?
// ...
return "redirect:/somewhere";
}
The redirect attributes will then be available on the target page where it redirects to.
However RedirectAttributes class has two methods:
Have been reading Spring documentation for a while but I'm a bit lost. What is the fundamental difference between those two, and how should I choose which one to use?
Javadoc description: "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. A FlashMap is saved before the redirect (typically in the session) and is made available after the redirect and removed immediately."
Here is the difference:
addFlashAttribute()
actually stores the attributes in a flashmap (which is internally maintained in the userssession
and removed once the next redirected request gets fulfilled)addAttribute()
essentially constructs request parameters out of your attributes and redirects to the desired page with the request parameters.So the advantage of
addFlashAttribute()
will be that you can store pretty much any object in your flash attribute (as it is not serialized into request params at all, but maintained as an object), whereas withaddAttribute()
since the object that you add gets transformed to a normal request param, you are pretty limited to the object types likeString
or primitives.Assume you have 2 controllers.If you redirect from one controller to another controller the values in model object won't be available in the other controller. So if you want to share the model object values then you have to say in first controller
Then second controller's model contains now the above key value pair..
Second question ? What is difference between
addAttribute
andaddFlashAttribute
inRedirectAttributes
classaddAttribute
will pass the values as requestparameters instead of model,so when you add some usingaddAttribute
you can access those values fromrequest.getParameter
Here is the code.I have used to find out what is going on :