类 ModelAttributeMethodProcessor
@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 Framework's own @cn.taketoday.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.
- 从以下版本开始:
- 4.0 2022/4/26 16:25
- 作者:
- Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze, Vladislav Kisel, Harry Yang
-
字段概要
字段从接口继承的字段 cn.taketoday.web.ReturnValueHandler
NONE_RETURN_VALUE -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected voidbindRequestParameters(WebDataBinder binder, RequestContext request) Extension point to bind the request to the target object.protected voidconstructAttribute(WebDataBinder binder, RequestContext request) Extension point to create the attribute, binding the request to constructor args.protected ObjectcreateAttribute(String attributeName, cn.taketoday.core.MethodParameter parameter, BindingContext bindingContext, RequestContext request) Extension point to create the model attribute if not found in the model, with subsequent parameter binding through bean properties (unless suppressed).protected ObjectcreateAttributeFromRequestValue(String sourceValue, String attributeName, cn.taketoday.core.MethodParameter parameter, BindingContext binderFactory, RequestContext request) Create a model attribute from a String request value (e.g.protected StringgetRequestValueForAttribute(String attributeName, RequestContext request) Obtain a value from the request that may be used to instantiate the model attribute through type conversion from String to the target type.getUriVariables(RequestContext request) voidhandleHandlerMethodReturnValue(RequestContext context, HandlerMethod handler, Object returnValue) Add non-null return values to theBindingContext.protected booleanisBindExceptionRequired(cn.taketoday.core.MethodParameter parameter) Whether to raise a fatal bind exception on validation errors.protected booleanisBindExceptionRequired(WebDataBinder binder, cn.taketoday.core.MethodParameter parameter) Whether to raise a fatal bind exception on validation errors.resolveArgument(RequestContext context, ResolvableMethodParameter resolvable) Resolve the argument from the model or if not found instantiate it with its default if it is available.booleansupportsHandlerMethod(HandlerMethod handler) Returntrueif there is a method-level@ModelAttributeor, in default resolution mode, for any return value type that is not a simple type.booleansupportsParameter(ResolvableMethodParameter resolvable) Returnstrueif the parameter is annotated withModelAttributeor, if in default resolution mode, for any method parameter that is not a simple type.protected voidvalidateIfApplicable(WebDataBinder binder, cn.taketoday.core.MethodParameter parameter) Validate the model attribute if applicable.private static ObjectwrapAsOptionalIfNecessary(cn.taketoday.core.MethodParameter parameter, Object target) 从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait从接口继承的方法 cn.taketoday.web.handler.result.HandlerMethodReturnValueHandler
handleReturnValue, supportsHandler从接口继承的方法 cn.taketoday.web.ReturnValueHandler
supportsReturnValue
-
字段详细资料
-
annotationNotRequired
private final boolean annotationNotRequired
-
-
构造器详细资料
-
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
Returnstrueif the parameter is annotated withModelAttributeor, if in default resolution mode, for any method parameter that is not a simple type.- 指定者:
supportsParameter在接口中ParameterResolvingStrategy
-
resolveArgument
@Nullable public Object resolveArgument(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable 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在接口中ParameterResolvingStrategy- 参数:
context- Current request contextresolvable- resolvable parameter- 返回:
- method parameter instance
- 抛出:
cn.taketoday.validation.BindException- if data binding and validation result in an error and the next method parameter is not of typeErrorsException- if WebDataBinder initialization failsThrowable- if anyExceptionoccurred- 另请参阅:
-
wrapAsOptionalIfNecessary
-
createAttribute
@Nullable protected Object createAttribute(String attributeName, cn.taketoday.core.MethodParameter parameter, BindingContext bindingContext, RequestContext request) throws Throwable Extension point to create the model attribute if not found in the model, with subsequent parameter binding through bean properties (unless suppressed).By default this method returns
nullin which caseDataBinder.construct(cn.taketoday.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.- 参数:
attributeName- the name of the attribute (nevernull)parameter- the method parameter declarationbindingContext- for creating WebDataBinder instancerequest- the current request- 返回:
- the created model attribute, or
null - 抛出:
Throwable
-
getRequestValueForAttribute
@Nullable protected String getRequestValueForAttribute(String attributeName, RequestContext request) Obtain a value from the request that may be used to instantiate the model attribute through type conversion from String to the target type.The default implementation looks for the attribute name to match a URI variable first and then a request parameter.
- 参数:
attributeName- the model attribute namerequest- the current request- 返回:
- the request value to try to convert, or
nullif none
-
getUriVariables
-
createAttributeFromRequestValue
@Nullable protected Object createAttributeFromRequestValue(String sourceValue, String attributeName, cn.taketoday.core.MethodParameter parameter, BindingContext binderFactory, RequestContext request) throws Throwable Create a model attribute from a String request value (e.g. URI template variable, request parameter) using type conversion.The default implementation converts only if there a registered
Converterthat can perform the conversion.- 参数:
sourceValue- the source value to create the model attribute fromattributeName- the name of the attribute (nevernull)parameter- the method parameterbinderFactory- for creating WebDataBinder instancerequest- the current request- 返回:
- the created model attribute, or
nullif no suitable conversion found - 抛出:
Throwable
-
constructAttribute
Extension point to create the attribute, binding the request to constructor args.- 参数:
binder- the data binder instance to use for the bindingrequest- the current request
-
bindRequestParameters
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, cn.taketoday.core.MethodParameter parameter) Validate the model attribute if applicable.The default implementation checks for
@jakarta.validation.Valid, InfraValidated, 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...)
-
isBindExceptionRequired
protected boolean isBindExceptionRequired(WebDataBinder binder, cn.taketoday.core.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
protected boolean isBindExceptionRequired(cn.taketoday.core.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
-
supportsHandlerMethod
Returntrueif there is a method-level@ModelAttributeor, in default resolution mode, for any return value type that is not a simple type.- 指定者:
supportsHandlerMethod在接口中HandlerMethodReturnValueHandler- 返回:
trueif this handler supports the supplied return type;falseotherwise- 另请参阅:
-
handleHandlerMethodReturnValue
public void handleHandlerMethodReturnValue(RequestContext context, HandlerMethod handler, @Nullable Object returnValue) Add non-null return values to theBindingContext.- 指定者:
handleHandlerMethodReturnValue在接口中HandlerMethodReturnValueHandler- 参数:
context- Current HTTP request contexthandler- HandlerMethodreturnValue- Handler execution result OrHandlerExceptionHandlerreturn value
-