Announcement Announcement Module
No announcement yet.
RedirectAttributes as a replacement for ModelMap Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • RedirectAttributes as a replacement for ModelMap


    I just spent some time replacing my defaultannotationhandlermapping with RequestMappingHandlerMapping in order to be able to use the Flash scope in Spring. I thought it might be a nice way to simplify my handlers and avoid ugly urls.

    A typical handler scenario that I want to support with this is:
    PHP Code:
    SomeEntity entity mgr.get(id);


    model.addFlashAttribute("status","Something went wrong: " entity.getLabel());

    So, a model should be able to be either contain Flash attributes for redirection purposes or be a "normal" model for regular display purposes.

    So I thought I could just replace ModelMap with RedirectAttributes. Get a few extra options, cool! However, RedirectAttributes tries to format all objects I add to it through model.addAttribute to Strings, I suppose so it can put them in a URL if it has to. Of course, this makes it useless as a ModelMap for display purposes.

    I just want to be able to use FlashAttributes and not have to worry about clearing models and such. The idea is to reduce complexity after all and get rid of those ugly url strings! Any suggestions on the correct way of using this?


  • #2
    RedirectAttributes != ModelMap... They serve a different means as the name already suggests. The RedirectAttributes are either URL encoded attributes (Strings as you noticed) or used as flash attributes (stored using the FlashMap manager) it isn't intended to be used as a the Model (being a Model, Map or ModelMap).

    You will have to use both to handle your use case.


    • #3
      Yeah, I figured that out. Thanks. I don't really understand why they have to be separate entities but I guess I haven't put enough thought into it. It works.


      • #4
        In theory they could be the same but at times it is better to be explicit then to create a large one-class-to-rule-them-all .

        So basically RedirectAttributes are to be used for redirects and for a more fine grained control of what gets appended to the URL. A Model is used as a Model and when redirecting everything inside the model is appended to the URL (and often there is more in the model then what you want to expose on the URL).

        I like the explicitness of RedirectAttributes but that is me and I'm probably biased on that