Announcement Announcement Module
Collapse
No announcement yet.
Strange URL after "redirect:/" (@ModelAttribute values are attached) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Strange URL after "redirect:/" (@ModelAttribute values are attached)

    Hello.
    Here is some code below which is used in my application (only important snippets, actual names are changed):
    @Controller
    MyController {
    ...
    // PhoneCategory is Enum
    @ModelAttribute("phoneCategories")
    public List<PhoneCategory> populatePhoneCategories() {
    return Arrays.asList(PhoneCategory.Home, PhoneCategory.Mobile);
    }
    ...
    @RequestMapping(value = "/profiles/account", method = RequestMethod.GET)
    public AccountProfileForm accountProfileForm() {
    return new AccountProfileForm();
    }
    ...
    @RequestMapping(value = "/profiles/account", method = RequestMethod.POST)
    public String accountProfileForm(Account account, @Valid AccountProfileForm accountProfileForm, BindingResult formBinding, final WebRequest request) {
    ...
    return "redirect:/";
    }
    And after the POST request, I get the following URL:
    localhost:8080/appl/?phoneCategories=Home&phoneCategories=Mobile

    Why phoneCategories is attached into my URL?
    This param is missed for GET, and I don't have any issues with others controllers in my application. Is it for the case I use List<Enum> for @ModelAttribute?

  • #2
    As stated in the reference guide when doing a redirect by default all model attributes are added to request url. Since @ModelAttribute methods are always executed before any request handling method you have something in your model and thus that will be appended to your url.

    In Spring 3.1 you can use explicit RequestAttributes to specify the redirect attributes and it is also possible to disable the default behavior of adding all model attributes to the redirect url.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      In Spring 3.1 you can use explicit RequestAttributes to specify the redirect attributes and it is also possible to disable the default behavior of adding all model attributes to the redirect url.
      Correct. Check the Spring 3.1 reference documentation on the new RedirectAttributes controller method argument. When you use it, only what you add through it is appended to the URL (the default model content is ignored). Also see the ignoreDefaultModelOnRedirect property on RequestMappingHandlerAdapter.

      Comment


      • #4
        Thank you, guys (was traveling, so was unable to respond quickly). I will obviously try it, but my question was not probably full, the reason why I was surprised, that beside @ModelAttribute("phoneCategories") I had another two @ModelAttribute (countries, and industries) on this same @Controller, but only phoneCategories attribute was attached into the redirect URL, why only it, and why the rest are missing?

        Comment


        • #5
          Simple attributes are added to the redirect url (probably ENUMS also fall in that category). I assume your countries and industries list isn't a simply attribute (int, string etc.).

          Comment


          • #6
            Yes, you are right, they are objects. Now everything is clear.

            Thank you very much.

            Comment

            Working...
            X