2

I bumped into an answer about the usage of @ModelAttribute on spring MVC here on stackoverflow, and learned that it was not actually required to be added into the method's parameter.

I looked for some controllers from our old project, deleted the annotation, and surprisingly the application still runs flawlessly without the @ModelAttribute. Please see example below:

@RequestMapping(method = RequestMethod.POST, value = "/audit/filter")
public String getAuditLogsWithFilter(Model model, AuditLogFilter auditLogFilter, BindingResult bindingResult)

I have read some articles about it but I can not grasp onto why @ModelAttribute is used for some method parameters particularly for spring controllers.

Can anybody provide a simple explanation into why that is? Or can someone enumerate some cases into which I should add the @ModelAttribute annotation to my parameter object?

Donato Amasa
  • 846
  • 2
  • 6
  • 22

1 Answers1

2

as described in the official document, it's optional:

Note that using @ModelAttribute is optional (for example, to set its attributes). By default, any argument that is not a simple value type (as determined by BeanUtils#isSimpleProperty) and is not resolved by any other argument resolver is treated as if it were annotated with @ModelAttribute.

zhang-yuan
  • 418
  • 7
  • 18