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

Allow an @ModelAttribute method to be invoked after the @RequestMapping method

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Minor
    • Resolution: Won't Fix
    • Affects Version/s: 2.5.6
    • Fix Version/s: None
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      @ModelAttribute is used for common (static) data and is used before the request is passed to the controller.
      It would nice to be able to use the same annotation after the handling process as well, to take care of common request data. The idea is to isolate the common model functionality in a separate method (such as dealing defaults) and leave the controller to focus only on the dynamic behaviour.

      An example of handling the initial request and initializing defaults (if ModelAttribute would be used after) would be:

      @ModelAttribute("bundleList) List<Bundle> bundleList(@ModelAttribute("selection")  SelectionCommand selectionCommand) {
          if (selectionCommand.getDisplayChoice() == null)
               selectionCommand.setDisplayChoice(BundleListingOptions.NAME);
         // display bundles using the display choice...
      }
      

      In this case, if @ModelAttribute would be used after the controller, the method could inspect the parameters and populate the model map itself (currently this needs to be handled in the controller, i.e.:

      if (selection ...) {
        model.addAttribute("bundleList", listBundles());
      })
      

      To be backwards compatible, the annotation could have a new parameter added to it (an enum) that can indicate when the binding should occur, before or after the handler invocation.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                rstoya05-aop Rossen Stoyanchev
                Reporter:
                costin Costin Leau
                Last updater:
                Rossen Stoyanchev
              • Votes:
                3 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  4 years, 10 weeks, 6 days ago