Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-7608

@PathVariable and @ModelAttribute incompatibility prevent me from having a nice "update" handler

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 3.0.3
    • Fix Version/s: 3.1 RC1
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      Spring MVC examples often refer to the creation of an entity using @ModelAttribute.

      A common pattern is to update an existing entity.
      Generally you want to load the existing entity and then apply on it the allowed fields.

      In my controller I have an handler for showing an entity, one for creating a new one...
      here are their signature:

      @RequestMapping("show/

      {pk}")
      public String show(@PathVariable("pk") Account account, Model model)

      @RequestMapping(value = "create", method = RequestMethod.POST)
      public String create(@Valid Account account, BindingResult bindingResult, Model model)


      Now, of course I need a handler, as nice as the one above to update the entity.
      Here it is:

      @RequestMapping(value = "update/{pk}

      ", method =

      { RequestMethod.PUT, RequestMethod.POST }

      )
      public String update(@Valid @PathVariable("pk") Account account, BindingResult bindingResult, Model model);

      The handler above is not accepted by SPring MVC, however I think it should! Here is what I would expect:
      1/ convert the pk to the Account entity using the corresponding Parser (it works for "show" handler above...)
      2/ Once converted, since the account is placed just before the bindingResult, it should be used to bind the request parameters that is to update directly the entity.
      3/Once updated, it should perform the validation.

      Note: I am confident in updating directly the entity as I know I can restrict the allowed fields thanks to an InitBinder.

      The code above throw an exception:
      org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String fr.nnn.web.controller.AccountController.update(fr.nnn.domain.Account,org.springframework.validation.BindingResult,java.lang.Boolean,java.lang.Boolean,org.springframework.ui.Model)]; nested exception is java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature!

      I tried to add the @ModelAttribute("account") annotation after @PathVariable("pk") but then another exception is thrown... stating that I cannot use both annotations at the same time.

      Am I missing something or do you agree it would be a nice feature?

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                rstoya05-aop Rossen Stoyanchev
                Reporter:
                nicolas Nicolas Romanetti
                Last updater:
                Trevor Marshall
              • Votes:
                2 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  6 years, 48 weeks, 2 days ago