Top Description Fields Constructors Methods
org.springframework.web.method.annotation

public Class ModelAttributeMethodProcessor

extends Object
implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler
Class Inheritance
All Implemented Interfaces
org.springframework.web.method.support.HandlerMethodReturnValueHandler, org.springframework.web.method.support.HandlerMethodArgumentResolver
Imports
java.lang.annotation.Annotation, java.util.Map, .Optional, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.BeanUtils, org.springframework.core.MethodParameter, .ResolvableType, org.springframework.lang.Nullable, org.springframework.util.Assert, .ObjectUtils, org.springframework.validation.BindException, .BindingResult, .Errors, .SmartValidator, org.springframework.validation.annotation.ValidationAnnotationUtils, org.springframework.web.bind.MethodArgumentNotValidException, .WebDataBinder, org.springframework.web.bind.annotation.ModelAttribute, org.springframework.web.bind.support.WebDataBinderFactory, .WebRequestDataBinder, org.springframework.web.context.request.NativeWebRequest, org.springframework.web.method.support.HandlerMethodArgumentResolver, .HandlerMethodReturnValueHandler, .ModelAndViewContainer

Resolve @ModelAttribute annotated method arguments and handle return values from @ModelAttribute annotated methods.

Model attributes are obtained from the model or created with a default constructor (and then added to the model). Once created the attribute is populated via data binding to Servlet request parameters. Validation may be applied if the argument is annotated with @jakarta.validation.Valid. or Spring's own @org.springframework.validation.annotation.Validated.

When this handler is created with annotationNotRequired=true any non-simple type argument and return value is regarded as a model attribute with or without the presence of an @ModelAttribute.

Authors
Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze, Vladislav Kisel
Since
3.1

Field Summary

Modifier and TypeField and Description
private final boolean
protected final Log

Constructor Summary

AccessConstructor and Description
public
ModelAttributeMethodProcessor(boolean
if "true", non-simple method arguments and return values are considered model attributes with or without a @ModelAttribute annotation
annotationNotRequired
)

Class constructor.

Method Summary

Modifier and TypeMethod and Description
protected void
bindRequestParameters(WebDataBinder
the data binder instance to use for the binding
binder
,
NativeWebRequest
the current request
request
)

Extension point to bind the request to the target object via setters/fields.

protected void
constructAttribute(WebDataBinder
the data binder instance to use for the binding
binder
,
NativeWebRequest
the current request
request
)

Extension point to create the attribute, binding the request to constructor args.

protected Object

Returns:

the created model attribute, or null
createAttribute
(String
the name of the attribute (never null)
attributeName
,
MethodParameter
the method parameter declaration
parameter
,
WebDataBinderFactory
for creating WebDataBinder instance
binderFactory
,
NativeWebRequest
the current request
request
)

Extension point to create the model attribute if not found in the model, with subsequent parameter binding through bean properties (unless suppressed).

public void
handleReturnValue(Object
the value returned from the handler method
returnValue
,
MethodParameter
the type of the return value. This type must have previously been passed to supportsReturnType which must have returned true.
returnType
,
ModelAndViewContainer
the ModelAndViewContainer for the current request
mavContainer
,
NativeWebRequest
the current request
webRequest
)

Implements org.springframework.web.method.support.HandlerMethodReturnValueHandler.handleReturnValue.

Add non-null return values to the ModelAndViewContainer.
protected boolean

Returns:

true if the next method parameter is not of type Errors
isBindExceptionRequired
(WebDataBinder
the data binder used to perform data binding
binder
,
MethodParameter
the method parameter declaration
parameter
)

Whether to raise a fatal bind exception on validation errors.

protected boolean

Returns:

true if the next method parameter is not of type Errors
isBindExceptionRequired
(MethodParameter
the method parameter declaration
parameter
)

Whether to raise a fatal bind exception on validation errors.

public final Object
resolveArgument(MethodParameter
the method parameter to resolve. This parameter must have previously been passed to supportsParameter which must have returned true.
parameter
,
ModelAndViewContainer
the ModelAndViewContainer for the current request
mavContainer
,
NativeWebRequest
the current request
webRequest
,
WebDataBinderFactory
a factory for creating WebDataBinder instances
binderFactory
)

Implements org.springframework.web.method.support.HandlerMethodArgumentResolver.resolveArgument.

Resolve the argument from the model or if not found instantiate it with its default if it is available.
public boolean
supportsParameter(MethodParameter
the method parameter to check
parameter
)

Implements org.springframework.web.method.support.HandlerMethodArgumentResolver.supportsParameter.

Returns true if the parameter is annotated with ModelAttribute or, if in default resolution mode, for any method parameter that is not a simple type.
public boolean
supportsReturnType(MethodParameter
the method return type to check
returnType
)

Implements org.springframework.web.method.support.HandlerMethodReturnValueHandler.supportsReturnType.

Return true if there is a method-level @ModelAttribute or, in default resolution mode, for any return value type that is not a simple type.
protected void
validateIfApplicable(WebDataBinder
the DataBinder to be used
binder
,
MethodParameter
the method parameter declaration
parameter
)

Validate the model attribute if applicable.

private static Object
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

annotationNotRequiredback to summary
private final boolean annotationNotRequired
loggerback to summary
protected final Log logger

Constructor Detail

ModelAttributeMethodProcessorback to summary
public ModelAttributeMethodProcessor(boolean annotationNotRequired)

Class constructor.

Parameters
annotationNotRequired:boolean

if "true", non-simple method arguments and return values are considered model attributes with or without a @ModelAttribute annotation

Method Detail

bindRequestParametersback to summary
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request)

Extension point to bind the request to the target object via setters/fields.

Parameters
binder:WebDataBinder

the data binder instance to use for the binding

request:NativeWebRequest

the current request

constructAttributeback to summary
protected void constructAttribute(WebDataBinder binder, NativeWebRequest request)

Extension point to create the attribute, binding the request to constructor args.

Parameters
binder:WebDataBinder

the data binder instance to use for the binding

request:NativeWebRequest

the current request

Since
6.1
createAttributeback to summary
protected Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception

Extension point to create the model attribute if not found in the model, with subsequent parameter binding through bean properties (unless suppressed).

By default, as of 6.1 this method returns null in which case org.springframework.validation.DataBinder#construct is used instead to create the model attribute. The main purpose of this method then is to allow to create the model attribute in some other, alternative way.

Parameters
attributeName:String

the name of the attribute (never null)

parameter:MethodParameter

the method parameter declaration

binderFactory:WebDataBinderFactory

for creating WebDataBinder instance

request:NativeWebRequest

the current request

Returns:Object

the created model attribute, or null

Annotations
@Nullable
handleReturnValueback to summary
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception

Implements org.springframework.web.method.support.HandlerMethodReturnValueHandler.handleReturnValue.

Add non-null return values to the ModelAndViewContainer.

Parameters
returnValue:Object

Doc from org.springframework.web.method.support.HandlerMethodReturnValueHandler.handleReturnValue.

the value returned from the handler method

returnType:MethodParameter

Doc from org.springframework.web.method.support.HandlerMethodReturnValueHandler.handleReturnValue.

the type of the return value. This type must have previously been passed to supportsReturnType which must have returned true.

mavContainer:ModelAndViewContainer

Doc from org.springframework.web.method.support.HandlerMethodReturnValueHandler.handleReturnValue.

the ModelAndViewContainer for the current request

webRequest:NativeWebRequest

Doc from org.springframework.web.method.support.HandlerMethodReturnValueHandler.handleReturnValue.

the current request

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.web.method.support.HandlerMethodReturnValueHandler.handleReturnValue.

if the return value handling results in an error

isBindExceptionRequiredback to summary
protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter)

Whether to raise a fatal bind exception on validation errors.

The default implementation delegates to isBindExceptionRequired(MethodParameter).

Parameters
binder:WebDataBinder

the data binder used to perform data binding

parameter:MethodParameter

the method parameter declaration

Returns:boolean

true if the next method parameter is not of type Errors

See Also
isBindExceptionRequired(MethodParameter)
isBindExceptionRequiredback to summary
protected boolean isBindExceptionRequired(MethodParameter parameter)

Whether to raise a fatal bind exception on validation errors.

Parameters
parameter:MethodParameter

the method parameter declaration

Returns:boolean

true if the next method parameter is not of type Errors

Since
5.0
resolveArgumentback to summary
public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception

Implements org.springframework.web.method.support.HandlerMethodArgumentResolver.resolveArgument.

Resolve the argument from the model or if not found instantiate it with its default if it is available. The model attribute is then populated with request values via data binding and optionally validated if @java.validation.Valid is present on the argument.

Parameters
parameter:MethodParameter

Doc from org.springframework.web.method.support.HandlerMethodArgumentResolver.resolveArgument.

the method parameter to resolve. This parameter must have previously been passed to supportsParameter which must have returned true.

mavContainer:ModelAndViewContainer

Doc from org.springframework.web.method.support.HandlerMethodArgumentResolver.resolveArgument.

the ModelAndViewContainer for the current request

webRequest:NativeWebRequest

Doc from org.springframework.web.method.support.HandlerMethodArgumentResolver.resolveArgument.

the current request

binderFactory:WebDataBinderFactory

Doc from org.springframework.web.method.support.HandlerMethodArgumentResolver.resolveArgument.

a factory for creating WebDataBinder instances

Returns:Object

Doc from org.springframework.web.method.support.HandlerMethodArgumentResolver.resolveArgument.

the resolved argument value, or null if not resolvable

Annotations
@Override
@Nullable
Exceptions
Exception:
if WebDataBinder initialization fails
BindException:
if data binding and validation result in an error and the next method parameter is not of type Errors
supportsParameterback to summary
public boolean supportsParameter(MethodParameter parameter)

Implements org.springframework.web.method.support.HandlerMethodArgumentResolver.supportsParameter.

Returns true if the parameter is annotated with ModelAttribute or, if in default resolution mode, for any method parameter that is not a simple type.

Parameters
parameter:MethodParameter

Doc from org.springframework.web.method.support.HandlerMethodArgumentResolver.supportsParameter.

the method parameter to check

Returns:boolean

Doc from org.springframework.web.method.support.HandlerMethodArgumentResolver.supportsParameter.

true if this resolver supports the supplied parameter; false otherwise

Annotations
@Override
supportsReturnTypeback to summary
public boolean supportsReturnType(MethodParameter returnType)

Implements org.springframework.web.method.support.HandlerMethodReturnValueHandler.supportsReturnType.

Return true if there is a method-level @ModelAttribute or, in default resolution mode, for any return value type that is not a simple type.

Parameters
returnType:MethodParameter

Doc from org.springframework.web.method.support.HandlerMethodReturnValueHandler.supportsReturnType.

the method return type to check

Returns:boolean

Doc from org.springframework.web.method.support.HandlerMethodReturnValueHandler.supportsReturnType.

true if this handler supports the supplied return type; false otherwise

Annotations
@Override
validateIfApplicableback to summary
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter)

Validate the model attribute if applicable.

The default implementation checks for @jakarta.validation.Valid, Spring's org.springframework.validation.annotation.Validated, and custom annotations whose name starts with "Valid".

Parameters
binder:WebDataBinder

the DataBinder to be used

parameter:MethodParameter

the method parameter declaration

See Also
WebDataBinder#validate(Object...), SmartValidator#validate(Object, Errors, Object...)
wrapAsOptionalIfNecessaryback to summary
private static Object wrapAsOptionalIfNecessary(MethodParameter parameter, Object target)
Annotations
@Nullable