类 ModelHandler

java.lang.Object
cn.taketoday.web.handler.method.ModelHandler

final class ModelHandler extends Object
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.

从以下版本开始:
4.0 2022/4/8 23:00
作者:
Rossen Stoyanchev, Harry Yang
  • 字段详细资料

    • log

      private static final cn.taketoday.logging.Logger log
    • methodResolver

      final ControllerMethodResolver methodResolver
  • 构造器详细资料

    • ModelHandler

      public ModelHandler(ControllerMethodResolver methodResolver)
      Create a new instance with the given @ModelAttribute methods.
  • 方法详细资料

    • initModel

      public void initModel(RequestContext request, BindingContext container, HandlerMethod handlerMethod) throws Throwable
      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.
      参数:
      request - the current request
      container - a container with the model to be initialized
      handlerMethod - the method for which the model is initialized
      抛出:
      Exception - may arise from @ModelAttribute methods
      Throwable
    • getModelMethods

      @Nullable private ArrayList<ModelHandler.ModelMethod> getModelMethods(HandlerMethod handlerMethod)
    • invokeModelAttributeMethods

      private void invokeModelAttributeMethods(RequestContext request, BindingContext container, ArrayList<ModelHandler.ModelMethod> modelMethods) throws Throwable
      Invoke model attribute methods to populate the model. Attributes are added only if not already present in the model.
      抛出:
      Throwable
    • getNextModelMethod

      private ModelHandler.ModelMethod getNextModelMethod(BindingContext container, ArrayList<ModelHandler.ModelMethod> modelMethods)
    • findSessionAttributeArguments

      private List<String> findSessionAttributeArguments(SessionAttributesHandler sessionAttrHandler, HandlerMethod handlerMethod)
      Find @ModelAttribute arguments also listed as @SessionAttributes.
    • updateModel

      public void updateModel(RequestContext request, BindingContext container, Class<?> handlerMethod) throws Throwable
      Promote model attributes listed as @SessionAttributes to the session. Add BindingResult attributes where necessary.
      参数:
      request - the current request
      container - contains the model to update
      抛出:
      Exception - if creating BindingResult attributes fails
      Throwable
    • updateBindingResult

      private void updateBindingResult(RequestContext request, BindingContext bindingContext, cn.taketoday.ui.ModelMap model, SessionAttributesHandler sessionAttributesHandler) throws Throwable
      Add BindingResult attributes to the model for attributes that require it.
      抛出:
      Throwable
    • isBindingCandidate

      private boolean isBindingCandidate(String attributeName, Object value, SessionAttributesHandler sessionAttrHandler)
      Whether the given attribute requires a BindingResult in the model.
    • getNameForParameter

      public static String getNameForParameter(cn.taketoday.core.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.
      参数:
      parameter - a descriptor for the method parameter
      返回:
      the derived name
      另请参阅:
      • Conventions.getVariableNameForParameter(MethodParameter)
    • getNameForReturnValue

      public static String getNameForReturnValue(@Nullable Object returnValue, HandlerMethod handler)
      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
      参数:
      returnValue - the value returned from a method invocation
      handler - a descriptor for the method
      返回:
      the derived name (never null or empty String)