@ModelAttribute is a Spring MVC annotation that binds request data (form fields, query parameters, URL-encoded values) to a Java object and exposes that object to the view model. On a method parameter it creates and populates the object for a handler; on a method it adds shared data to the model before every handler in the controller runs.
It is the standard way to handle HTML form submissions in server-rendered Spring MVC applications. For JSON bodies you use @RequestBody instead.
When a handler parameter is annotated with @ModelAttribute (or is a non-simple type with no annotation, which defaults to it), Spring does the following:
WebDataBinder.@Valid or @Validated.Model under the given name, so a template can display it.BindingResult must come directly after the attribute so validation errors do not throw MethodArgumentNotValidException. Records work as binding targets since Spring 6, using constructor binding.
A method annotated with @ModelAttribute runs before each handler in that controller, and its return value is added to the model.
The categories list is available in the template for every view this controller renders. Use it for small reference data such as dropdown lists. It runs on every request in that controller, so avoid heavy queries, or cache them.
Parameters are matched by property name, with nested paths such as address.city.
Form fields and query parameters both bind. A multipart request binds text parts and MultipartFile fields. Request bodies sent as JSON are not bound by @ModelAttribute; use @RequestBody for those.
| Annotation | Source of data | Typical use |
|---|---|---|
@ModelAttribute | Query and form parameters | HTML forms, search filters |
@RequestParam | A single named parameter | Simple scalar values |
@RequestBody | Request body via message converters | JSON or XML APIs |
@PathVariable | URI template segment | Resource identifiers |
@SessionAttributes | Session, with @ModelAttribute names | Multi-step forms |
Binding by field name means a client can send extra parameters that match properties you did not intend to expose (mass assignment). Reduce the risk:
binder.setAllowedFields(...) in an @InitBinder method when you must bind to a larger class.int field) land in BindingResult; without that parameter they cause a 400 response.@ModelAttribute method returning a database entity runs on every request and may fail on missing ids.No. A parameter that is not a simple type and has no other annotation is treated as a model attribute automatically. Writing it explicitly makes the intent clear and lets you set the name.
@ModelAttribute binds request parameters by name; @RequestBody converts the whole body from JSON or XML using a message converter.
The binding still works for parameters, but the model is not rendered to a view. Most REST handlers use @RequestParam or @RequestBody.
Yes. Spring 6 and later bind constructor parameters by name, so records and immutable classes are supported.
@ModelAttribute binds request parameters to an object, validates it and puts it in the model for the view. As a method annotation it supplies shared model data. Use dedicated form objects, BindingResult for errors, and @RequestBody for JSON.