Top Description Inners Fields Constructors Methods
org.springframework.web.method.annotation

public final Class ModelFactory

extends Object
Class Inheritance
Imports
java.lang.reflect.Method, java.util.ArrayList, .Collection, .HashSet, .List, .Map, .Set, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.BeanUtils, org.springframework.core.Conventions, .GenericTypeResolver, .MethodParameter, org.springframework.lang.Nullable, org.springframework.ui.Model, .ModelMap, org.springframework.util.Assert, .StringUtils, org.springframework.validation.BindingResult, org.springframework.web.HttpSessionRequiredException, org.springframework.web.bind.WebDataBinder, org.springframework.web.bind.annotation.ModelAttribute, org.springframework.web.bind.support.WebDataBinderFactory, org.springframework.web.context.request.NativeWebRequest, org.springframework.web.method.HandlerMethod, org.springframework.web.method.support.InvocableHandlerMethod, .ModelAndViewContainer

Assist with initialization of the Model before controller method invocation and with updates to it after the invocation.

On initialization the model is populated with attributes temporarily stored in the session and through the invocation of @ModelAttribute methods.

On update model attributes are synchronized with the session and also BindingResult attributes are added if missing.

Author
Rossen Stoyanchev
Since
3.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final WebDataBinderFactory
private static final Log
private final List<ModelFactory.ModelMethod>
private final SessionAttributesHandler

Constructor Summary

AccessConstructor and Description
public
ModelFactory(List<InvocableHandlerMethod>
the @ModelAttribute methods to invoke
handlerMethods
,
WebDataBinderFactory
for preparation of BindingResult attributes
binderFactory
,
SessionAttributesHandler
for access to session attributes
attributeHandler
)

Create a new instance with the given @ModelAttribute methods.

Method Summary

Modifier and TypeMethod and Description
private List<String>
findSessionAttributeArguments(HandlerMethod handlerMethod)

Find @ModelAttribute arguments also listed as @SessionAttributes.

public static String

Returns:

the derived name
getNameForParameter
(MethodParameter
a descriptor for the method parameter
parameter
)

Derive the model attribute name for the given method parameter based on a @ModelAttribute parameter annotation (if present) or falling back on parameter type based conventions.

public static String

Returns:

the derived name (never null or empty String)
getNameForReturnValue
(Object
the value returned from a method invocation
returnValue
,
MethodParameter
a descriptor for the return type of the method
returnType
)

Derive the model attribute name for the given return value.

private ModelFactory.ModelMethod
public void
initModel(NativeWebRequest
the current request
request
,
ModelAndViewContainer
a container with the model to be initialized
container
,
HandlerMethod
the method for which the model is initialized
handlerMethod
)

Populate the model in the following order:

  1. Retrieve "known" session attributes listed as @SessionAttributes.
  2. Invoke @ModelAttribute methods
  3. Find @ModelAttribute method arguments also listed as @SessionAttributes and ensure they're present in the model raising an exception if necessary.
private void
invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer container)

Invoke model attribute methods to populate the model.

private boolean
isBindingCandidate(String attributeName, 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.

public void
updateModel(NativeWebRequest
the current request
request
,
ModelAndViewContainer
contains the model to update
container
)

Promote model attributes listed as @SessionAttributes to the session.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dataBinderFactoryback to summary
private final WebDataBinderFactory dataBinderFactory
loggerback to summary
private static final Log logger
modelMethodsback to summary
private final List<ModelFactory.ModelMethod> modelMethods
sessionAttributesHandlerback to summary
private final SessionAttributesHandler sessionAttributesHandler

Constructor Detail

ModelFactoryback to summary
public ModelFactory(List<InvocableHandlerMethod> handlerMethods, WebDataBinderFactory binderFactory, SessionAttributesHandler attributeHandler)

Create a new instance with the given @ModelAttribute methods.

Parameters
handlerMethods:List<InvocableHandlerMethod>

the @ModelAttribute methods to invoke

binderFactory:WebDataBinderFactory

for preparation of BindingResult attributes

attributeHandler:SessionAttributesHandler

for access to session attributes

Method Detail

findSessionAttributeArgumentsback to summary
private List<String> findSessionAttributeArguments(HandlerMethod handlerMethod)

Find @ModelAttribute arguments also listed as @SessionAttributes.

getNameForParameterback to summary
public static String getNameForParameter(MethodParameter parameter)

Derive the model attribute name for the given method parameter based on a @ModelAttribute parameter annotation (if present) or falling back on parameter type based conventions.

Parameters
parameter:MethodParameter

a descriptor for the method parameter

Returns:String

the derived name

See Also
Conventions#getVariableNameForParameter(MethodParameter)
getNameForReturnValueback to summary
public static String getNameForReturnValue(Object returnValue, MethodParameter returnType)

Derive the model attribute name for the given return value. Results will be based on:

  1. the method ModelAttribute annotation value
  2. the declared return type if it is more specific than Object
  3. the actual return value type
Parameters
returnValue:Object

the value returned from a method invocation

returnType:MethodParameter

a descriptor for the return type of the method

Returns:String

the derived name (never null or empty String)

getNextModelMethodback to summary
private ModelFactory.ModelMethod getNextModelMethod(ModelAndViewContainer container)
initModelback to summary
public void initModel(NativeWebRequest request, ModelAndViewContainer container, HandlerMethod handlerMethod) throws Exception

Populate the model in the following order:

  1. Retrieve "known" session attributes listed as @SessionAttributes.
  2. Invoke @ModelAttribute methods
  3. Find @ModelAttribute method arguments also listed as @SessionAttributes and ensure they're present in the model raising an exception if necessary.
Parameters
request:NativeWebRequest

the current request

container:ModelAndViewContainer

a container with the model to be initialized

handlerMethod:HandlerMethod

the method for which the model is initialized

Exceptions
Exception:
may arise from @ModelAttribute methods
invokeModelAttributeMethodsback to summary
private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer container) throws Exception

Invoke model attribute methods to populate the model. Attributes are added only if not already present in the model.

isBindingCandidateback to summary
private boolean isBindingCandidate(String attributeName, Object value)

Whether the given attribute requires a BindingResult in the model.

updateBindingResultback to summary
private void updateBindingResult(NativeWebRequest request, ModelMap model) throws Exception

Add BindingResult attributes to the model for attributes that require it.

updateModelback to summary
public void updateModel(NativeWebRequest request, ModelAndViewContainer container) throws Exception

Promote model attributes listed as @SessionAttributes to the session. Add BindingResult attributes where necessary.

Parameters
request:NativeWebRequest

the current request

container:ModelAndViewContainer

contains the model to update

Exceptions
Exception:
if creating BindingResult attributes fails
org.springframework.web.method.annotation back to summary

private Class ModelFactory.ModelMethod

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.web.method.annotation.ModelFactory.ModelMethod

Field Summary

Modifier and TypeField and Description
private final Set<String>
private final InvocableHandlerMethod

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
public InvocableHandlerMethod
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

dependenciesback to summary
private final Set<String> dependencies
handlerMethodback to summary
private final InvocableHandlerMethod handlerMethod

Constructor Detail

ModelMethodback to summary
public ModelMethod(InvocableHandlerMethod handlerMethod)

Method Detail

checkDependenciesback to summary
public boolean checkDependencies(ModelAndViewContainer mavContainer)
getHandlerMethodback to summary
public InvocableHandlerMethod getHandlerMethod()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override