org.springframework.web.method.annotation
Class ModelFactory

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

public final class ModelFactory
extends 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

Constructor Summary
ModelFactory(List<InvocableHandlerMethod> attributeMethods, WebDataBinderFactory binderFactory, SessionAttributesHandler sessionAttributesHandler)
          Create a new instance with the given @ModelAttribute methods.
 
Method Summary
static String getNameForParameter(MethodParameter parameter)
          Derives the model attribute name for a method parameter based on: The parameter @ModelAttribute annotation value The parameter type
static String getNameForReturnValue(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.
 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
 

Constructor Detail

ModelFactory

public ModelFactory(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 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:
Exception - may arise from @ModelAttribute methods

getNameForReturnValue

public static String getNameForReturnValue(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 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 Exception
Synchronize model attributes with the session. Add BindingResult attributes where necessary.

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