类 ModelAttributeMethodProcessor

java.lang.Object
cn.taketoday.web.handler.method.ModelAttributeMethodProcessor
所有已实现的接口:
ParameterResolvingStrategy, HandlerMethodReturnValueHandler, ReturnValueHandler

public class ModelAttributeMethodProcessor extends Object implements ParameterResolvingStrategy, HandlerMethodReturnValueHandler
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 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
  • 字段详细资料

    • 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 @ModelAttribute annotation
  • 方法详细资料

    • supportsParameter

      public boolean supportsParameter(ResolvableMethodParameter resolvable)
      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.
      指定者:
      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.Valid is present on the argument.
      指定者:
      resolveArgument 在接口中 ParameterResolvingStrategy
      参数:
      context - Current request context
      resolvable - 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 type Errors
      Exception - if WebDataBinder initialization fails
      Throwable - if any Exception occurred
      另请参阅:
    • wrapAsOptionalIfNecessary

      @Nullable private static Object wrapAsOptionalIfNecessary(cn.taketoday.core.MethodParameter parameter, @Nullable Object target)
    • 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 null in which case DataBinder.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 (never null)
      parameter - the method parameter declaration
      bindingContext - for creating WebDataBinder instance
      request - 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 name
      request - the current request
      返回:
      the request value to try to convert, or null if none
    • getUriVariables

      private Map<String,String> getUriVariables(RequestContext request)
    • 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 Converter that can perform the conversion.

      参数:
      sourceValue - the source value to create the model attribute from
      attributeName - the name of the attribute (never null)
      parameter - the method parameter
      binderFactory - for creating WebDataBinder instance
      request - the current request
      返回:
      the created model attribute, or null if no suitable conversion found
      抛出:
      Throwable
    • constructAttribute

      protected void constructAttribute(WebDataBinder binder, RequestContext request)
      Extension point to create the attribute, binding the request to constructor args.
      参数:
      binder - the data binder instance to use for the binding
      request - the current request
    • bindRequestParameters

      protected void bindRequestParameters(WebDataBinder binder, RequestContext request)
      Extension point to bind the request to the target object.
      参数:
      binder - the data binder instance to use for the binding
      request - 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, Infra Validated, and custom annotations whose name starts with "Valid".

      参数:
      binder - the DataBinder to be used
      parameter - 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 binding
      parameter - the method parameter declaration
      返回:
      true if the next method parameter is not of type Errors
      另请参阅:
    • isBindExceptionRequired

      protected boolean isBindExceptionRequired(cn.taketoday.core.MethodParameter parameter)
      Whether to raise a fatal bind exception on validation errors.
      参数:
      parameter - the method parameter declaration
      返回:
      true if the next method parameter is not of type Errors
    • supportsHandlerMethod

      public boolean supportsHandlerMethod(HandlerMethod handler)
      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.
      指定者:
      supportsHandlerMethod 在接口中 HandlerMethodReturnValueHandler
      返回:
      true if this handler supports the supplied return type; false otherwise
      另请参阅:
    • handleHandlerMethodReturnValue

      public void handleHandlerMethodReturnValue(RequestContext context, HandlerMethod handler, @Nullable Object returnValue)
      Add non-null return values to the BindingContext.
      指定者:
      handleHandlerMethodReturnValue 在接口中 HandlerMethodReturnValueHandler
      参数:
      context - Current HTTP request context
      handler - HandlerMethod
      returnValue - Handler execution result Or HandlerExceptionHandler return value