类 ModelAttributeMethodProcessor
- java.lang.Object
- org.springframework.web.method.annotation.ModelAttributeMethodProcessor
public class ModelAttributeMethodProcessor extends Object implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler
Resolve@ModelAttributeannotated method arguments and handle return values from@ModelAttributeannotated 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
@javax.validation.Valid. or Spring's own@org.springframework.validation.annotation.Validated.When this handler is created with
annotationNotRequired=trueany non-simple type argument and return value is regarded as a model attribute with or without the presence of an@ModelAttribute.- 从以下版本开始:
- 3.1
- 作者:
- Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
构造器概要
构造器 构造器 说明 ModelAttributeMethodProcessor(boolean annotationNotRequired)Class constructor.
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 protected voidbindRequestParameters(WebDataBinder binder, NativeWebRequest request)Extension point to bind the request to the target object.protected ObjectconstructAttribute(Constructor<?> ctor, String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest webRequest)Construct a new attribute instance with the given constructor.protected ObjectconstructAttribute(Constructor<?> ctor, String attributeName, WebDataBinderFactory binderFactory, NativeWebRequest webRequest)已过时。protected ObjectcreateAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest webRequest)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.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.protected voidvalidateValueIfApplicable(WebDataBinder binder, MethodParameter parameter, Class<?> targetType, String fieldName, Object value)Validate the specified candidate value if applicable.
构造器详细资料
ModelAttributeMethodProcessor
public ModelAttributeMethodProcessor(boolean annotationNotRequired)
Class constructor.- 参数:
annotationNotRequired- if "true", non-simple method arguments and return values are considered model attributes with or without a@ModelAttributeannotation
方法详细资料
supportsParameter
public boolean supportsParameter(MethodParameter parameter)
Returnstrueif the parameter is annotated withModelAttributeor, if in default resolution mode, for any method parameter that is not a simple type.- 指定者:
supportsParameter在接口中HandlerMethodArgumentResolver- 参数:
parameter- the method parameter to check- 返回:
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.- 指定者:
resolveArgument在接口中HandlerMethodArgumentResolver- 参数:
parameter- the method parameter to resolve. This parameter must have previously been passed toHandlerMethodArgumentResolver.supportsParameter(org.springframework.core.MethodParameter)which must have returnedtrue.mavContainer- the ModelAndViewContainer for the current requestwebRequest- the current requestbinderFactory- a factory for creatingWebDataBinderinstances- 返回:
- the resolved argument value, or
nullif not resolvable - 抛出:
BindException- if data binding and validation result in an error and the next method parameter is not of typeErrorsException- if WebDataBinder initialization fails
createAttribute
protected Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest webRequest) throws Exception
Extension point to create the model attribute if not found in the model, with subsequent parameter binding through bean properties (unless suppressed).The default implementation typically uses the unique public no-arg constructor if available but also handles a "primary constructor" approach for data classes: It understands the JavaBeans
ConstructorPropertiesannotation as well as runtime-retained parameter names in the bytecode, associating request parameters with constructor arguments by name. If no such constructor is found, the default constructor will be used (even if not public), assuming subsequent bean property bindings through setter methods.- 参数:
attributeName- the name of the attribute (nevernull)parameter- the method parameter declarationbinderFactory- for creating WebDataBinder instancewebRequest- the current request- 返回:
- the created model attribute (never
null) - 抛出:
BindException- in case of constructor argument binding failureException- in case of constructor invocation failure- 另请参阅:
constructAttribute(Constructor, String, MethodParameter, WebDataBinderFactory, NativeWebRequest),BeanUtils.findPrimaryConstructor(Class)
constructAttribute
protected Object constructAttribute(Constructor<?> ctor, String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest webRequest) throws Exception
Construct a new attribute instance with the given constructor.Called from
createAttribute(String, MethodParameter, WebDataBinderFactory, NativeWebRequest)after constructor resolution.- 参数:
ctor- the constructor to useattributeName- the name of the attribute (nevernull)binderFactory- for creating WebDataBinder instancewebRequest- the current request- 返回:
- the created model attribute (never
null) - 抛出:
BindException- in case of constructor argument binding failureException- in case of constructor invocation failure- 从以下版本开始:
- 5.1
constructAttribute
@Deprecated @Nullable protected Object constructAttribute(Constructor<?> ctor, String attributeName, WebDataBinderFactory binderFactory, NativeWebRequest webRequest) throws Exception
已过时。Construct a new attribute instance with the given constructor.- 抛出:
Exception- 从以下版本开始:
- 5.0
bindRequestParameters
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request)
Extension point to bind the request to the target object.- 参数:
binder- the data binder instance to use for the bindingrequest- the current request
validateIfApplicable
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter)
Validate the model attribute if applicable.The default implementation checks for
@javax.validation.Valid, Spring'sValidated, and custom annotations whose name starts with "Valid".- 参数:
binder- the DataBinder to be usedparameter- the method parameter declaration- 另请参阅:
DataBinder.validate(Object...),SmartValidator.validate(Object, Errors, Object...)
validateValueIfApplicable
protected void validateValueIfApplicable(WebDataBinder binder, MethodParameter parameter, Class<?> targetType, String fieldName, @Nullable Object value)
Validate the specified candidate value if applicable.The default implementation checks for
@javax.validation.Valid, Spring'sValidated, and custom annotations whose name starts with "Valid".- 参数:
binder- the DataBinder to be usedparameter- the method parameter declarationtargetType- the target typefieldName- the name of the fieldvalue- the candidate value- 从以下版本开始:
- 5.1
- 另请参阅:
validateIfApplicable(WebDataBinder, MethodParameter),SmartValidator.validateValue(Class, String, Object, Errors, Object...)
isBindExceptionRequired
protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter)
Whether to raise a fatal bind exception on validation errors.The default implementation delegates to
isBindExceptionRequired(MethodParameter).- 参数:
binder- the data binder used to perform data bindingparameter- the method parameter declaration- 返回:
trueif the next method parameter is not of typeErrors- 另请参阅:
isBindExceptionRequired(MethodParameter)
isBindExceptionRequired
protected boolean isBindExceptionRequired(MethodParameter parameter)
Whether to raise a fatal bind exception on validation errors.- 参数:
parameter- the method parameter declaration- 返回:
trueif the next method parameter is not of typeErrors- 从以下版本开始:
- 5.0
supportsReturnType
public boolean supportsReturnType(MethodParameter returnType)
Returntrueif there is a method-level@ModelAttributeor, in default resolution mode, for any return value type that is not a simple type.- 指定者:
supportsReturnType在接口中HandlerMethodReturnValueHandler- 参数:
returnType- the method return type to check- 返回:
trueif this handler supports the supplied return type;falseotherwise
handleReturnValue
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception
Add non-null return values to theModelAndViewContainer.- 指定者:
handleReturnValue在接口中HandlerMethodReturnValueHandler- 参数:
returnValue- the value returned from the handler methodreturnType- the type of the return value. This type must have previously been passed toHandlerMethodReturnValueHandler.supportsReturnType(org.springframework.core.MethodParameter)which must have returnedtrue.mavContainer- the ModelAndViewContainer for the current requestwebRequest- the current request- 抛出:
Exception- if the return value handling results in an error