Class ModelAttributeMethodProcessor
- All Implemented Interfaces:
- HandlerMethodArgumentResolver,- HandlerMethodReturnValueHandler
- Direct Known Subclasses:
- ServletModelAttributeMethodProcessor
@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.
- Since:
- 3.1
- Author:
- Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze, Vladislav Kisel
- 
Field SummaryFields
- 
Constructor SummaryConstructorsConstructorDescriptionModelAttributeMethodProcessor(boolean annotationNotRequired) Class constructor.
- 
Method SummaryModifier and TypeMethodDescriptionprotected voidbindRequestParameters(WebDataBinder binder, NativeWebRequest request) Extension point to bind the request to the target object via setters/fields.protected voidconstructAttribute(WebDataBinder binder, NativeWebRequest request) Extension point to create the attribute, binding the request to constructor args.protected ObjectcreateAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) Extension point to create the model attribute if not found in the model, with subsequent parameter binding through bean properties (unless suppressed).voidhandleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) Add non-null return values to theModelAndViewContainer.protected booleanisBindExceptionRequired(MethodParameter parameter) Whether to raise a fatal bind exception on validation errors.protected booleanisBindExceptionRequired(WebDataBinder binder, MethodParameter parameter) Whether to raise a fatal bind exception on validation errors.final ObjectresolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) Resolve the argument from the model or if not found instantiate it with its default if it is available.booleansupportsParameter(MethodParameter parameter) Returnstrueif the parameter is annotated withModelAttributeor, if in default resolution mode, for any method parameter that is not a simple type.booleansupportsReturnType(MethodParameter returnType) Returntrueif there is a method-level@ModelAttributeor, in default resolution mode, for any return value type that is not a simple type.protected voidvalidateIfApplicable(WebDataBinder binder, MethodParameter parameter) Validate the model attribute if applicable.
- 
Field Details- 
logger
 
- 
- 
Constructor Details- 
ModelAttributeMethodProcessorpublic ModelAttributeMethodProcessor(boolean annotationNotRequired) Class constructor.- Parameters:
- annotationNotRequired- if "true", non-simple method arguments and return values are considered model attributes with or without a- @ModelAttributeannotation
 
 
- 
- 
Method Details- 
supportsParameterReturnstrueif the parameter is annotated withModelAttributeor, if in default resolution mode, for any method parameter that is not a simple type.- Specified by:
- supportsParameterin interface- HandlerMethodArgumentResolver
- Parameters:
- parameter- the method parameter to check
- Returns:
- trueif this resolver supports the supplied parameter;- falseotherwise
 
- 
resolveArgument@Nullable public final Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception 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.Validis present on the argument.- Specified by:
- resolveArgumentin interface- HandlerMethodArgumentResolver
- Parameters:
- parameter- 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 request
- webRequest- the current request
- binderFactory- a factory for creating- WebDataBinderinstances
- Returns:
- the resolved argument value, or nullif not resolvable
- Throws:
- BindException- if data binding and validation result in an error and the next method parameter is not of type- Errors
- Exception- if WebDataBinder initialization fails
 
- 
createAttribute@Nullable 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 nullin which caseDataBinder.construct(org.springframework.validation.DataBinder.ValueResolver)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- the name of the attribute (never- null)
- parameter- the method parameter declaration
- binderFactory- for creating WebDataBinder instance
- request- the current request
- Returns:
- the created model attribute, or null
- Throws:
- Exception
 
- 
constructAttributeExtension point to create the attribute, binding the request to constructor args.- Parameters:
- binder- the data binder instance to use for the binding
- request- the current request
- Since:
- 6.1
 
- 
bindRequestParametersExtension point to bind the request to the target object via setters/fields.- Parameters:
- binder- the data binder instance to use for the binding
- request- the current request
 
- 
validateIfApplicableValidate the model attribute if applicable.The default implementation checks for @jakarta.validation.Valid, Spring'sValidated, and custom annotations whose name starts with "Valid".- Parameters:
- binder- the DataBinder to be used
- parameter- the method parameter declaration
- See Also:
 
- 
isBindExceptionRequiredWhether to raise a fatal bind exception on validation errors.The default implementation delegates to isBindExceptionRequired(MethodParameter).- Parameters:
- binder- the data binder used to perform data binding
- parameter- the method parameter declaration
- Returns:
- trueif the next method parameter is not of type- Errors
- See Also:
 
- 
isBindExceptionRequiredWhether to raise a fatal bind exception on validation errors.- Parameters:
- parameter- the method parameter declaration
- Returns:
- trueif the next method parameter is not of type- Errors
- Since:
- 5.0
 
- 
supportsReturnTypeReturntrueif there is a method-level@ModelAttributeor, in default resolution mode, for any return value type that is not a simple type.- Specified by:
- supportsReturnTypein interface- HandlerMethodReturnValueHandler
- Parameters:
- returnType- the method return type to check
- Returns:
- trueif this handler supports the supplied return type;- falseotherwise
 
- 
handleReturnValuepublic void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception Add non-null return values to theModelAndViewContainer.- Specified by:
- handleReturnValuein interface- HandlerMethodReturnValueHandler
- Parameters:
- returnValue- the value returned from the handler method
- returnType- 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 request
- webRequest- the current request
- Throws:
- Exception- if the return value handling results in an error
 
 
-