Skip to content

Allow an @ModelAttribute method to be invoked after the @RequestMapping method [SPR-5695] #10365

@spring-projects-issues

Description

@spring-projects-issues

Costin Leau opened SPR-5695 and commented

@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.


Affects: 2.5.6

Issue Links:

3 votes, 5 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions