org.springframework.web.method.annotation
Class ModelAttributeMethodProcessor

java.lang.Object
  extended by org.springframework.web.method.annotation.ModelAttributeMethodProcessor
All Implemented Interfaces:
HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler
Direct Known Subclasses:
ServletModelAttributeMethodProcessor

public class ModelAttributeMethodProcessor
extends Object
implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler

Resolves method arguments annotated with @ModelAttribute and handles return values from methods annotated with @ModelAttribute.

Model attributes are obtained from the model or if not found possibly created with a default constructor if it is available. Once created, the attributed is populated with request data via data binding and also validation may be applied if the argument is annotated with @javax.validation.Valid.

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

Field Summary
protected  Log logger
           
 
Constructor Summary
ModelAttributeMethodProcessor(boolean annotationNotRequired)
           
 
Method Summary
protected  void bindRequestParameters(WebDataBinder binder, NativeWebRequest request)
          Extension point to bind the request to the target object.
protected  Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request)
          Extension point to create the model attribute if not found in the model.
 void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
          Add non-null return values to the ModelAndViewContainer.
protected  boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter)
          Whether to raise a BindException on bind or validation errors.
 Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest request, WebDataBinderFactory binderFactory)
          Resolve the argument from the model or if not found instantiate it with its default if it is available.
 boolean supportsParameter(MethodParameter parameter)
          Whether the given method parameter is supported by this resolver.
 boolean supportsReturnType(MethodParameter returnType)
          Return true if there is a method-level @ModelAttribute or if it is a non-simple type when annotationNotRequired=true.
protected  void validateIfApplicable(WebDataBinder binder, MethodParameter parameter)
          Validate the model attribute if applicable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected Log logger
Constructor Detail

ModelAttributeMethodProcessor

public ModelAttributeMethodProcessor(boolean annotationNotRequired)
Parameters:
annotationNotRequired - if "true", non-simple method arguments and return values are considered model attributes with or without a @ModelAttribute annotation.
Method Detail

supportsParameter

public boolean supportsParameter(MethodParameter parameter)
Description copied from interface: HandlerMethodArgumentResolver
Whether the given method parameter is supported by this resolver.

Specified by:
supportsParameter in interface HandlerMethodArgumentResolver
Parameters:
parameter - the method parameter to check
Returns:
true if the parameter is annotated with ModelAttribute or in default resolution mode also if it is not a simple type.

resolveArgument

public final Object resolveArgument(MethodParameter parameter,
                                    ModelAndViewContainer mavContainer,
                                    NativeWebRequest request,
                                    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.Valid is present on the argument.

Specified by:
resolveArgument in interface HandlerMethodArgumentResolver
Parameters:
parameter - the method parameter to resolve. This parameter must have previously been passed to HandlerMethodArgumentResolver.supportsParameter(org.springframework.core.MethodParameter) and it must have returned true
mavContainer - the ModelAndViewContainer for the current request
request - the current request
binderFactory - a factory for creating WebDataBinder instances
Returns:
the resolved argument value, or null.
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

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. The default implementation uses the default constructor.

Parameters:
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, never null
Throws:
Exception

bindRequestParameters

protected void bindRequestParameters(WebDataBinder binder,
                                     NativeWebRequest request)
Extension point to bind the request to the target object.

Parameters:
binder - the data binder instance to use for the binding
request - 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.

Parameters:
binder - the DataBinder to be used
parameter - the method parameter

isBindExceptionRequired

protected boolean isBindExceptionRequired(WebDataBinder binder,
                                          MethodParameter parameter)
Whether to raise a BindException on bind or validation errors. The default implementation returns true if the next method argument is not of type Errors.

Parameters:
binder - the data binder used to perform data binding
parameter - the method argument

supportsReturnType

public boolean supportsReturnType(MethodParameter returnType)
Return true if there is a method-level @ModelAttribute or if it is a non-simple type when annotationNotRequired=true.

Specified by:
supportsReturnType in interface HandlerMethodReturnValueHandler
Parameters:
returnType - the method return type to check
Returns:
true if this handler supports the supplied return type; false otherwise

handleReturnValue

public void handleReturnValue(Object returnValue,
                              MethodParameter returnType,
                              ModelAndViewContainer mavContainer,
                              NativeWebRequest webRequest)
                       throws Exception
Add non-null return values to the ModelAndViewContainer.

Specified by:
handleReturnValue in 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) and it 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