Skip to content

implicit creation of model (session) attribute [SPR-4408] #9086

Closed
@spring-projects-issues

Description

@spring-projects-issues

Andreas Schildbach opened SPR-4408 and commented

If you declare a model attribute with @ModelAttribute in a handler method (parameter annotation), it would be convenient if the framework would construct an empty attribute for you, if there isn't one already and there is a no-arg constructor on the attribute's class.

Instead of code like this

@RequestMapping
public String setupForm(ModelMap model) {
Command command = new Command();
command.setXyz(defaultValue);
model.addAttribute("command", command);
return view;
}

you could just write

@RequestMapping
public String setupForm(@ModelAttribute Command command) {
command.setXyz(defaultValue);
return view;
}

This feature would of course apply to @SessionAttributes, too.

If this feature looks too magical, you could also introduce a boolean parameter on the @ModelAttribute annotation, saying whether implicit creation is allowed or not.


Affects: 2.5.1

9 votes, 10 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions