'@ModelAttribute for form parameter in Spring post handler: is already an attribute?
There's this Spring behaviour that I can't explain from the documentation.
I have a form, which sends a name parameter. Name is a simple bean with a value property.
The handler looks something like this:
@PostMapping("/hello")
public String onSubmit(Name name, Model model) { // 1
// public String onSubmit(@ModelAttribute Name name, Model model) { // 2
// .. stuff
return "helloView";
}
Whether I add the @ModelAttribute annotation (commented line 2) or not (line 1) doesn't make a difference: both a unit test and a JSP page indicate the name attribute is there.
Is there some default behaviour which automatically forwards incoming attributes (from the form) to the model?
For what it's worth: I can use the annotation to change the attribute name. For example: @ModelAttribute("theName") Name name would no longer provide the attribute under the name key, but instead under the theName key (as you would pretty much expect).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
