public final class ModelFactory
extends java.lang.Object
Model
before controller method
invocation and to update it afterwards.
On initialization, the model is populated with attributes from the session
and by invoking methods annotated with @ModelAttribute
.
On update, model attributes are synchronized with the session and also
BindingResult
attributes are added where missing.
Modifier and Type | Class and Description |
---|---|
private static class |
ModelFactory.ModelMethod |
Modifier and Type | Field and Description |
---|---|
private WebDataBinderFactory |
dataBinderFactory |
private static Log |
logger |
private java.util.List<ModelFactory.ModelMethod> |
modelMethods |
private SessionAttributesHandler |
sessionAttributesHandler |
Constructor and Description |
---|
ModelFactory(java.util.List<InvocableHandlerMethod> invocableMethods,
WebDataBinderFactory dataBinderFactory,
SessionAttributesHandler sessionAttributesHandler)
Create a new instance with the given
@ModelAttribute methods. |
Modifier and Type | Method and Description |
---|---|
private java.util.List<java.lang.String> |
findSessionAttributeArguments(HandlerMethod handlerMethod)
Find
@ModelAttribute arguments also listed as @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 method
ModelAttribute annotation value
The declared return type if it is more specific than Object
The actual return value type
|
private ModelFactory.ModelMethod |
getNextModelMethod(ModelAndViewContainer mavContainer) |
void |
initModel(NativeWebRequest request,
ModelAndViewContainer mavContainer,
HandlerMethod handlerMethod)
Populate the model in the following order:
Retrieve "known" session attributes listed as
@SessionAttributes . |
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)
Promote model attributes listed as
@SessionAttributes to the session. |
private static final Log logger
private final java.util.List<ModelFactory.ModelMethod> modelMethods
private final WebDataBinderFactory dataBinderFactory
private final SessionAttributesHandler sessionAttributesHandler
public ModelFactory(java.util.List<InvocableHandlerMethod> invocableMethods, WebDataBinderFactory dataBinderFactory, SessionAttributesHandler sessionAttributesHandler)
@ModelAttribute
methods.invocableMethods
- the @ModelAttribute
methods to invokedataBinderFactory
- for preparation of BindingResult
attributessessionAttributesHandler
- for access to session attributespublic void initModel(NativeWebRequest request, ModelAndViewContainer mavContainer, HandlerMethod handlerMethod) throws java.lang.Exception
@SessionAttributes
.
@ModelAttribute
methods
@ModelAttribute
method arguments also listed as
@SessionAttributes
and ensure they're present in the model raising
an exception if necessary.
request
- the current requestmavContainer
- a container with the model to be initializedhandlerMethod
- the method for which the model is initializedjava.lang.Exception
- may arise from @ModelAttribute
methodsprivate void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer mavContainer) throws java.lang.Exception
java.lang.Exception
private ModelFactory.ModelMethod getNextModelMethod(ModelAndViewContainer mavContainer)
private java.util.List<java.lang.String> findSessionAttributeArguments(HandlerMethod handlerMethod)
@ModelAttribute
arguments also listed as @SessionAttributes
.public static java.lang.String getNameForParameter(MethodParameter parameter)
@ModelAttribute
annotation value
null
or an empty stringpublic static java.lang.String getNameForReturnValue(java.lang.Object returnValue, MethodParameter returnType)
ModelAttribute
annotation value
Object
returnValue
- the value returned from a method invocationreturnType
- the return type of the methodnull
nor emptypublic void updateModel(NativeWebRequest request, ModelAndViewContainer mavContainer) throws java.lang.Exception
@SessionAttributes
to the session.
Add BindingResult
attributes where necessary.request
- the current requestmavContainer
- contains the model to updatejava.lang.Exception
- if creating BindingResult attributes failsprivate void updateBindingResult(NativeWebRequest request, ModelMap model) throws java.lang.Exception
BindingResult
attributes to the model for attributes that require it.java.lang.Exception
private boolean isBindingCandidate(java.lang.String attributeName, java.lang.Object value)
BindingResult
in the model.