0

In order to access the redirect attributes in the redirected method, we utilize the model's map, like this :

@Controller
@RequestMapping("/foo")
public class FooController {

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

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

}

But, why can't we access them in this way :

      @RequestMapping(value = "/bar", method = RequestMethod.GET)
      public ModelAndView handleGet(RedirectAttributes redAttr) {
         String some = redAttr.getFlashAttributes().get("some");
      }

If the only purpose of adding flashAttributes is that they become available to the model in the redirected method, what's the purpose of getFlashAttributes() ?

Daud
  • 7,429
  • 18
  • 68
  • 115

1 Answers1

2

RedirectAttributes are for setting flash attributes before redirection. They are merged into model after the redirection so there is no reason to access them again via RedirectAttributes again as you have suggested.


Being able to work with the attributes just like with a map might be useful. You can check what have you set (containsKey, isEmpty, ...). However the use of the wildcard generic parameter Map<String, ?> getFlashAttributes() prevents writing into map and it is strange why they have used it instead of a plain Object parameter.

Pavel Horal
  • 17,782
  • 3
  • 65
  • 89