org.springframework.web.servlet.mvc.method.annotation

## Class ServletModelAttributeMethodProcessor

- java.lang.Object
- - org.springframework.web.method.annotation.ModelAttributeMethodProcessor
  - - org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor

- All Implemented Interfaces:
    HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler

      public class ServletModelAttributeMethodProcessor
      extends ModelAttributeMethodProcessor

  A Servlet-specific `ModelAttributeMethodProcessor` that applies data binding through a WebDataBinder of type `ServletRequestDataBinder`.

  Also adds a fall-back strategy to instantiate the model attribute from a URI template variable or from a request parameter if the name matches the model attribute name and there is an appropriate type conversion strategy.

  Since:
    3.1
  Author:
    Rossen Stoyanchev, Juergen Hoeller

- - ### Field Summary

    - ### Fields inherited from class org.springframework.web.method.annotation.ModelAttributeMethodProcessor

      `logger`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ServletModelAttributeMethodProcessor(boolean annotationNotRequired)` Class constructor.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `bindRequestParameters(WebDataBinder binder, NativeWebRequest request)` This implementation downcasts `WebDataBinder` to `ServletRequestDataBinder` before binding.
    `protected Object` | `createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request)` Instantiate the model attribute from a URI template variable or from a request parameter if the name matches to the model attribute name and if there is an appropriate type conversion strategy.
    `protected Object` | `createAttributeFromRequestValue(String sourceValue, String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request)` Create a model attribute from a String request value (e.g.
    `protected String` | `getRequestValueForAttribute(String attributeName, NativeWebRequest 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.
    `protected Map<String,String>` | `getUriTemplateVariables(NativeWebRequest request)`
    `Object` | `resolveConstructorArgument(String paramName, Class<?> paramType, NativeWebRequest request)`

    - ### Methods inherited from class org.springframework.web.method.annotation.ModelAttributeMethodProcessor

      `constructAttribute, handleReturnValue, isBindExceptionRequired, isBindExceptionRequired, resolveArgument, supportsParameter, supportsReturnType, validateIfApplicable, validateValueIfApplicable`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ServletModelAttributeMethodProcessor

          public ServletModelAttributeMethodProcessor(boolean annotationNotRequired)

      Class constructor.
      Parameters:
        `annotationNotRequired` - if "true", non-simple method arguments and return values are considered model attributes with or without a `@ModelAttribute` annotation

  - ### Method Detail

    - #### createAttribute

          protected final Object createAttribute(String attributeName,
                                                 MethodParameter parameter,
                                                 WebDataBinderFactory binderFactory,
                                                 NativeWebRequest request)
                                          throws Exception

      Instantiate the model attribute from a URI template variable or from a request parameter if the name matches to the model attribute name and if there is an appropriate type conversion strategy. If none of these are true delegate back to the base class.
      Overrides:
        `createAttribute` in class `ModelAttributeMethodProcessor`
      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 (never `null`)
      Throws:
        `BindException` - in case of constructor argument binding failure
        `Exception` - in case of constructor invocation failure
      See Also:
        `createAttributeFromRequestValue(java.lang.String, java.lang.String, org.springframework.core.MethodParameter, org.springframework.web.bind.support.WebDataBinderFactory, org.springframework.web.context.request.NativeWebRequest)`

    - #### getRequestValueForAttribute

          @Nullable
          protected String getRequestValueForAttribute(String attributeName,
                                                                 NativeWebRequest 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.

      Parameters:
        `attributeName` - the model attribute name
        `request` - the current request
      Returns:
        the request value to try to convert, or `null` if none

    - #### getUriTemplateVariables

          protected final Map<String,String> getUriTemplateVariables(NativeWebRequest request)

    - #### createAttributeFromRequestValue

          @Nullable
          protected Object createAttributeFromRequestValue(String sourceValue,
                                                                     String attributeName,
                                                                     MethodParameter parameter,
                                                                     WebDataBinderFactory binderFactory,
                                                                     NativeWebRequest request)
                                                              throws Exception

      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 is a registered `Converter` that can perform the conversion.

      Parameters:
        `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
      Returns:
        the created model attribute, or `null` if no suitable conversion found
      Throws:
        `Exception`

    - #### bindRequestParameters

          protected void bindRequestParameters(WebDataBinder binder,
                                               NativeWebRequest request)

      This implementation downcasts `WebDataBinder` to `ServletRequestDataBinder` before binding.
      Overrides:
        `bindRequestParameters` in class `ModelAttributeMethodProcessor`
      Parameters:
        `binder` - the data binder instance to use for the binding
        `request` - the current request
      See Also:
        `ServletRequestDataBinderFactory`

    - #### resolveConstructorArgument

          @Nullable
          public Object resolveConstructorArgument(String paramName,
                                                             Class<?> paramType,
                                                             NativeWebRequest request)
                                                      throws Exception

      Overrides:
        `resolveConstructorArgument` in class `ModelAttributeMethodProcessor`
      Throws:
        `Exception`