spring-framework / org.springframework.web.bind.annotation / ModelAttribute

ModelAttribute

@Target([AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER]) class ModelAttribute

Annotation that binds a method parameter or method return value to a named model attribute, exposed to a web view. Supported for controller classes with RequestMapping methods.

Can be used to expose command objects to a web view, using specific attribute names, through annotating corresponding parameters of an RequestMapping method.

Can also be used to expose reference data to a web view through annotating accessor methods in a controller class with RequestMapping methods. Such accessor methods are allowed to have any arguments that RequestMapping methods support, returning the model attribute value to expose.

Note however that reference data and all other model content is not available to web views when request processing results in an Exception since the exception could be raised at any time making the content of the model unreliable. For this reason ExceptionHandler methods do not provide access to a Model argument.

Author
Juergen Hoeller

Author
Rossen Stoyanchev

Since
2.5

Constructors

<init>

ModelAttribute(value: String, name: String, binding: Boolean)

Annotation that binds a method parameter or method return value to a named model attribute, exposed to a web view. Supported for controller classes with RequestMapping methods.

Can be used to expose command objects to a web view, using specific attribute names, through annotating corresponding parameters of an RequestMapping method.

Can also be used to expose reference data to a web view through annotating accessor methods in a controller class with RequestMapping methods. Such accessor methods are allowed to have any arguments that RequestMapping methods support, returning the model attribute value to expose.

Note however that reference data and all other model content is not available to web views when request processing results in an Exception since the exception could be raised at any time making the content of the model unreliable. For this reason ExceptionHandler methods do not provide access to a Model argument.

Properties

binding

val binding: Boolean

Allows declaring data binding disabled directly on an @ModelAttribute method parameter or on the attribute returned from an @ModelAttribute method, both of which would prevent data binding for that attribute.

By default this is set to true in which case data binding applies. Set this to false to disable data binding.

name

val name: String

The name of the model attribute to bind to.

The default model attribute name is inferred from the declared attribute type (i.e. the method parameter type or method return type), based on the non-qualified class name: e.g. "orderAddress" for class "mypackage.OrderAddress", or "orderAddressList" for "List<mypackage.OrderAddress>".

value

val value: String

Alias for #name.