Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring RedirectAttributes: addAttribute() vs addFlashAttribute()

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:

  • addAttribute()
  • addFlashAttribute()

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?

like image 392
gerrytan Avatar asked Jan 22 '13 23:01

gerrytan


People also ask

What is addFlashAttribute?

addFlashAttribute() actually stores the attributes in a flashmap (which is internally maintained in the users session and removed once the next redirected request gets fulfilled)

How do you use RedirectAttributes?

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 do I redirect a spring boot request?

There are two ways you can do this. Two using RedirectView object. The return type of the method should be ResponseEntity<Void>. STEP2: Build response entity object with 302 http status code and the external URL.

Which attribute is used to redirect?

Approach: To redirect from an HTML page to another page, you can use the <meta> tag by specifying the particular link in the URL attribute. It is the client-side redirection, the browsers request the server to provide another page.


2 Answers

Here is the difference:

  • addFlashAttribute() actually stores the attributes in a flashmap (which is internally maintained in the users session 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 with addAttribute() since the object that you add gets transformed to a normal request param, you are pretty limited to the object types like String or primitives.

like image 162
Biju Kunjummen Avatar answered Oct 22 '22 04:10

Biju Kunjummen


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

addFlashAttribute("modelkey", "modelvalue"); 

Then second controller's model contains now the above key value pair..

Second question ? What is difference between addAttribute and addFlashAttribute in RedirectAttributes class

addAttribute will pass the values as requestparameters instead of model,so when you add some using addAttribute you can access those values from request.getParameter

Here is the code.I have used to find out what is going on :

@RequestMapping(value = "/rm1", method = RequestMethod.POST) public String rm1(Model model,RedirectAttributes rm) {     System.out.println("Entered rm1 method ");      rm.addFlashAttribute("modelkey", "modelvalue");     rm.addAttribute("nonflash", "nonflashvalue");     model.addAttribute("modelkey", "modelvalue");      return "redirect:/rm2.htm"; }   @RequestMapping(value = "/rm2", method = RequestMethod.GET) public String rm2(Model model,HttpServletRequest request) {     System.out.println("Entered rm2 method ");      Map md = model.asMap();     for (Object modelKey : md.keySet()) {         Object modelValue = md.get(modelKey);         System.out.println(modelKey + " -- " + modelValue);     }      System.out.println("=== Request data ===");      java.util.Enumeration<String> reqEnum = request.getParameterNames();     while (reqEnum.hasMoreElements()) {         String s = reqEnum.nextElement();         System.out.println(s);         System.out.println("==" + request.getParameter(s));     }      return "controller2output"; } 
like image 32
JavaLearner Avatar answered Oct 22 '22 03:10

JavaLearner