类 ModelFactory
- java.lang.Object
- org.springframework.web.method.annotation.ModelFactory
public final class ModelFactory extends Object
Assist with initialization of theModelbefore 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
@ModelAttributemethods.On update model attributes are synchronized with the session and also
BindingResultattributes are added if missing.- 从以下版本开始:
- 3.1
- 作者:
- Rossen Stoyanchev
构造器概要
构造器 构造器 说明 ModelFactory(List<InvocableHandlerMethod> handlerMethods, WebDataBinderFactory binderFactory, SessionAttributesHandler attributeHandler)Create a new instance with the given@ModelAttributemethods.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 static StringgetNameForParameter(MethodParameter parameter)Derive the model attribute name for the given method parameter based on a@ModelAttributeparameter annotation (if present) or falling back on parameter type based conventions.static StringgetNameForReturnValue(Object returnValue, MethodParameter returnType)Derive the model attribute name for the given return value.voidinitModel(NativeWebRequest request, ModelAndViewContainer container, HandlerMethod handlerMethod)Populate the model in the following order: Retrieve "known" session attributes listed as@SessionAttributes.voidupdateModel(NativeWebRequest request, ModelAndViewContainer container)Promote model attributes listed as@SessionAttributesto the session.
构造器详细资料
ModelFactory
public ModelFactory(@Nullable List<InvocableHandlerMethod> handlerMethods, WebDataBinderFactory binderFactory, SessionAttributesHandler attributeHandler)
Create a new instance with the given@ModelAttributemethods.- 参数:
handlerMethods- the@ModelAttributemethods to invokebinderFactory- for preparation ofBindingResultattributesattributeHandler- for access to session attributes
方法详细资料
initModel
public void initModel(NativeWebRequest request, ModelAndViewContainer container, HandlerMethod handlerMethod) throws Exception
Populate the model in the following order:- Retrieve "known" session attributes listed as
@SessionAttributes. - Invoke
@ModelAttributemethods - Find
@ModelAttributemethod arguments also listed as@SessionAttributesand ensure they're present in the model raising an exception if necessary.
- 参数:
request- the current requestcontainer- a container with the model to be initializedhandlerMethod- the method for which the model is initialized- 抛出:
Exception- may arise from@ModelAttributemethods
- Retrieve "known" session attributes listed as
updateModel
public void updateModel(NativeWebRequest request, ModelAndViewContainer container) throws Exception
Promote model attributes listed as@SessionAttributesto the session. AddBindingResultattributes where necessary.- 参数:
request- the current requestcontainer- contains the model to update- 抛出:
Exception- if creating BindingResult attributes fails
getNameForParameter
public static String getNameForParameter(MethodParameter parameter)
Derive the model attribute name for the given method parameter based on a@ModelAttributeparameter annotation (if present) or falling back on parameter type based conventions.- 参数:
parameter- a descriptor for the method parameter- 返回:
- the derived name
- 另请参阅:
Conventions.getVariableNameForParameter(MethodParameter)
getNameForReturnValue
public static String getNameForReturnValue(@Nullable Object returnValue, MethodParameter returnType)
Derive the model attribute name for the given return value. Results will be based on:- the method
ModelAttributeannotation value - the declared return type if it is more specific than
Object - the actual return value type
- 参数:
returnValue- the value returned from a method invocationreturnType- a descriptor for the return type of the method- 返回:
- the derived name (never
nullor empty String)
- the method