简体   繁体   中英

Spring MVC @ModelAttribute as a method parameter annotation

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM