org.springframework.web.method.annotation.support
Class ModelAttributeMethodProcessor

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

public class ModelAttributeMethodProcessor
extends Object
implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler

Resolves method arguments annotated with @ModelAttribute. Or if created in default resolution mode, resolves any non-simple type argument even without an @ModelAttribute. See the constructor for details.

A model attribute argument is obtained from the model or otherwise is created with a default constructor. Data binding and validation are applied through a WebDataBinder instance. Validation is applied only when the argument is also annotated with @Valid.

Also handles return values from methods annotated with an @ModelAttribute. The return value is added to the ModelAndViewContainer.

Since:
3.1
Author:
Rossen Stoyanchev

Constructor Summary
ModelAttributeMethodProcessor(boolean useDefaultResolution)
           
 
Method Summary
protected  void doBind(WebDataBinder binder, NativeWebRequest request)
          Bind the request to the target object contained in the provided binder instance.
protected  boolean failOnError(WebDataBinder binder, MethodParameter parameter)
          Whether to raise a BindException in case of data binding or validation errors.
 void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
          Handle the given return value by adding attributes to the model, setting the view (or view name), or by writing to the response.
 Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory)
          Resolves the argument to a model attribute looking up the attribute in the model or instantiating it using its default constructor.
protected  boolean shouldValidate(WebDataBinder binder, MethodParameter parameter)
          Whether to validate the target object of the given WebDataBinder instance.
 boolean supportsParameter(MethodParameter parameter)
          Whether the given method parameter is supported by this resolver.
 boolean supportsReturnType(MethodParameter returnType)
          Whether the given method return type is supported by this handler.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ModelAttributeMethodProcessor

public ModelAttributeMethodProcessor(boolean useDefaultResolution)
Parameters:
useDefaultResolution - in default resolution mode a method argument that isn't a simple type, as defined in BeanUtils.isSimpleProperty(Class), is treated as a model attribute even if it doesn't have an @ModelAttribute annotation with its name derived from the model attribute type.
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 if it is a simple type without any annotations.

resolveArgument

public final Object resolveArgument(MethodParameter parameter,
                                    ModelAndViewContainer mavContainer,
                                    NativeWebRequest webRequest,
                                    WebDataBinderFactory binderFactory)
                             throws Exception
Resolves the argument to a model attribute looking up the attribute in the model or instantiating it using its default constructor. Data binding and optionally validation is then applied through a WebDataBinder instance. Validation is invoked optionally when the method parameter is annotated with an @Valid.

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
webRequest - the current request
binderFactory - a factory for creating WebDataBinder instances
Returns:
the resolved argument value, or null.
Throws:
Exception - if a WebDataBinder could not be created or if data binding and validation result in an error and the next method parameter is not of type Errors or BindingResult.

doBind

protected void doBind(WebDataBinder binder,
                      NativeWebRequest request)
Bind the request to the target object contained in the provided binder instance.

Parameters:
binder - the binder with the target object to apply request values to
request - the current request

shouldValidate

protected boolean shouldValidate(WebDataBinder binder,
                                 MethodParameter parameter)
Whether to validate the target object of the given WebDataBinder instance.

Parameters:
binder - the data binder containing the validation candidate
parameter - the method argument for which data binding is performed
Returns:
true if DataBinder.validate() should be invoked, false otherwise.

failOnError

protected boolean failOnError(WebDataBinder binder,
                              MethodParameter parameter)
Whether to raise a BindException in case of data binding or validation errors.

Parameters:
binder - the binder on which validation is to be invoked
parameter - the method argument for which data binding is performed
Returns:
true if the binding or validation errors should result in a BindException, false otherwise.

supportsReturnType

public boolean supportsReturnType(MethodParameter returnType)
Description copied from interface: HandlerMethodReturnValueHandler
Whether the given method return type is supported by this handler.

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
Description copied from interface: HandlerMethodReturnValueHandler
Handle the given return value by adding attributes to the model, setting the view (or view name), or by writing to the response. HandlerMethodReturnValueHandler implementations should also consider whether to set ModelAndViewContainer.setResolveView(boolean), which is set to true by default and therefore needs to be set to false explicitly if view resolution is to be bypassed.

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