org.springframework.web.method.annotation
Class ModelFactory

java.lang.Object
  extended by org.springframework.web.method.annotation.ModelFactory

public final class ModelFactory
extends java.lang.Object

Provides methods to initialize the Model before controller method invocation and to update it afterwards. On initialization, the model is populated with attributes from the session or by invoking @ModelAttribute methods. On update, model attributes are synchronized with the session -- either adding or removing them. Also BindingResult attributes where missing.

Since:
3.1
Author:
Rossen Stoyanchev

Field Summary
private  java.util.List<InvocableHandlerMethod> attributeMethods
           
private  WebDataBinderFactory binderFactory
           
private  SessionAttributesHandler sessionAttributesHandler
           
 
Constructor Summary
ModelFactory(java.util.List<InvocableHandlerMethod> attributeMethods, WebDataBinderFactory binderFactory, SessionAttributesHandler sessionAttributesHandler)
          Create a new instance with the given @ModelAttribute methods.
 
Method Summary
private  java.util.List<java.lang.String> findSessionAttributeArguments(HandlerMethod handlerMethod)
          Return all @ModelAttribute arguments declared as session attributes via @SessionAttributes.
static java.lang.String getNameForParameter(MethodParameter parameter)
          Derives the model attribute name for a method parameter based on: The parameter @ModelAttribute annotation value The parameter type
static java.lang.String getNameForReturnValue(java.lang.Object returnValue, MethodParameter returnType)
          Derive the model attribute name for the given return value using one of the following: The method ModelAttribute annotation value The declared return type if it is other than Object The actual return value type
 void initModel(NativeWebRequest request, ModelAndViewContainer mavContainer, HandlerMethod handlerMethod)
          Populate the model in the following order: Retrieve "known" session attributes -- i.e.
private  void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer mavContainer)
          Invoke model attribute methods to populate the model.
private  boolean isBindingCandidate(java.lang.String attributeName, java.lang.Object value)
          Whether the given attribute requires a BindingResult in the model.
private  void updateBindingResult(NativeWebRequest request, ModelMap model)
          Add BindingResult attributes to the model for attributes that require it.
 void updateModel(NativeWebRequest request, ModelAndViewContainer mavContainer)
          Synchronize model attributes with the session.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

attributeMethods

private final java.util.List<InvocableHandlerMethod> attributeMethods

binderFactory

private final WebDataBinderFactory binderFactory

sessionAttributesHandler

private final SessionAttributesHandler sessionAttributesHandler
Constructor Detail

ModelFactory

public ModelFactory(java.util.List<InvocableHandlerMethod> attributeMethods,
                    WebDataBinderFactory binderFactory,
                    SessionAttributesHandler sessionAttributesHandler)
Create a new instance with the given @ModelAttribute methods.

Parameters:
attributeMethods - for model initialization
binderFactory - for adding BindingResult attributes
sessionAttributesHandler - for access to session attributes
Method Detail

initModel

public void initModel(NativeWebRequest request,
                      ModelAndViewContainer mavContainer,
                      HandlerMethod handlerMethod)
               throws java.lang.Exception
Populate the model in the following order:
  1. Retrieve "known" session attributes -- i.e. attributes listed via @SessionAttributes and previously stored in the in the model at least once
  2. Invoke @ModelAttribute methods
  3. Find method arguments eligible as session attributes and retrieve them if they're not already present in the model

Parameters:
request - the current request
mavContainer - contains the model to be initialized
handlerMethod - the method for which the model is initialized
Throws:
java.lang.Exception - may arise from @ModelAttribute methods

invokeModelAttributeMethods

private void invokeModelAttributeMethods(NativeWebRequest request,
                                         ModelAndViewContainer mavContainer)
                                  throws java.lang.Exception
Invoke model attribute methods to populate the model. Attributes are added only if not already present in the model.

Throws:
java.lang.Exception

findSessionAttributeArguments

private java.util.List<java.lang.String> findSessionAttributeArguments(HandlerMethod handlerMethod)
Return all @ModelAttribute arguments declared as session attributes via @SessionAttributes.


getNameForReturnValue

public static java.lang.String getNameForReturnValue(java.lang.Object returnValue,
                                                     MethodParameter returnType)
Derive the model attribute name for the given return value using one of the following:
  1. The method ModelAttribute annotation value
  2. The declared return type if it is other than Object
  3. The actual return value type

Parameters:
returnValue - the value returned from a method invocation
returnType - the return type of the method
Returns:
the model name, never null nor empty

getNameForParameter

public static java.lang.String getNameForParameter(MethodParameter parameter)
Derives the model attribute name for a method parameter based on:
  1. The parameter @ModelAttribute annotation value
  2. The parameter type

Returns:
the derived name; never null or an empty string

updateModel

public void updateModel(NativeWebRequest request,
                        ModelAndViewContainer mavContainer)
                 throws java.lang.Exception
Synchronize model attributes with the session. Add BindingResult attributes where necessary.

Parameters:
request - the current request
mavContainer - contains the model to update
Throws:
java.lang.Exception - if creating BindingResult attributes fails

updateBindingResult

private void updateBindingResult(NativeWebRequest request,
                                 ModelMap model)
                          throws java.lang.Exception
Add BindingResult attributes to the model for attributes that require it.

Throws:
java.lang.Exception

isBindingCandidate

private boolean isBindingCandidate(java.lang.String attributeName,
                                   java.lang.Object value)
Whether the given attribute requires a BindingResult in the model.