public class ModelAttributeMethodProcessor extends Object implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler
@ModelAttribute and handles
return values from methods annotated with @ModelAttribute.
Model attributes are obtained from the model or if not found possibly
created with a default constructor if it is available. Once created, the
attributed is populated with request data via data binding and also
validation may be applied if the argument is annotated with
@javax.validation.Valid.
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.
| Constructor and Description |
|---|
ModelAttributeMethodProcessor(boolean annotationNotRequired) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
bindRequestParameters(WebDataBinder binder,
NativeWebRequest request)
Extension point to bind the request to the target object.
|
protected Object |
createAttribute(String attributeName,
MethodParameter parameter,
WebDataBinderFactory binderFactory,
NativeWebRequest request)
Extension point to create the model attribute if not found in the model.
|
void |
handleReturnValue(Object returnValue,
MethodParameter returnType,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest)
Add non-null return values to the
ModelAndViewContainer. |
protected boolean |
isBindExceptionRequired(WebDataBinder binder,
MethodParameter parameter)
Whether to raise a
BindException on validation errors. |
Object |
resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest request,
WebDataBinderFactory binderFactory)
Resolve the argument from the model or if not found instantiate it with
its default if it is available.
|
boolean |
supportsParameter(MethodParameter parameter)
Whether the given method parameter is
supported by this resolver.
|
boolean |
supportsReturnType(MethodParameter returnType)
Return
true if there is a method-level @ModelAttribute
or if it is a non-simple type when annotationNotRequired=true. |
protected void |
validateIfApplicable(WebDataBinder binder,
MethodParameter parameter)
Validate the model attribute if applicable.
|
protected Log logger
public ModelAttributeMethodProcessor(boolean annotationNotRequired)
annotationNotRequired - if "true", non-simple method arguments and
return values are considered model attributes with or without a
@ModelAttribute annotation.public boolean supportsParameter(MethodParameter parameter)
HandlerMethodArgumentResolversupportsParameter in interface HandlerMethodArgumentResolverparameter - the method parameter to checkModelAttribute
or in default resolution mode also if it is not a simple type.public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception
@java.validation.Valid is present on the argument.resolveArgument in interface HandlerMethodArgumentResolverparameter - the method parameter to resolve. This parameter must
have previously been passed to HandlerMethodArgumentResolver.supportsParameter(org.springframework.core.MethodParameter) which must
have returned true.mavContainer - the ModelAndViewContainer for the current requestrequest - the current requestbinderFactory - a factory for creating WebDataBinder instancesnullBindException - if data binding and validation result in an error
and the next method parameter is not of type Errors.Exception - if WebDataBinder initialization fails.protected Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception
attributeName - the name of the attribute, never nullparameter - the method parameterbinderFactory - for creating WebDataBinder instancerequest - the current requestnullExceptionprotected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request)
binder - the data binder instance to use for the bindingrequest - the current requestprotected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter)
The default implementation checks for @javax.validation.Valid.
binder - the DataBinder to be usedparameter - the method parameterprotected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter)
BindException on validation errors.binder - the data binder used to perform data bindingparameter - the method argumenttrue if the next method argument is not of type Errors.public boolean supportsReturnType(MethodParameter returnType)
true if there is a method-level @ModelAttribute
or if it is a non-simple type when annotationNotRequired=true.supportsReturnType in interface HandlerMethodReturnValueHandlerreturnType - the method return type to checktrue if this handler supports the supplied return type;
false otherwisepublic void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception
ModelAndViewContainer.handleReturnValue in interface HandlerMethodReturnValueHandlerreturnValue - the value returned from the handler methodreturnType - the type of the return value. This type must have
previously been passed to HandlerMethodReturnValueHandler.supportsReturnType(org.springframework.core.MethodParameter) which must
have returned true.mavContainer - the ModelAndViewContainer for the current requestwebRequest - the current requestException - if the return value handling results in an error