org.springframework.web.method.annotation

## Class ModelFactory

- java.lang.Object
- - org.springframework.web.method.annotation.ModelFactory

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

  Since:
    3.1
  Author:
    Rossen Stoyanchev

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ModelFactory(List<InvocableHandlerMethod> handlerMethods, WebDataBinderFactory binderFactory, SessionAttributesHandler attributeHandler)` Create a new instance with the given `@ModelAttribute` methods.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `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.
    `static String` | `getNameForReturnValue(Object returnValue, MethodParameter returnType)` Derive the model attribute name for the given return value.
    `void` | `initModel(NativeWebRequest request, ModelAndViewContainer container, HandlerMethod handlerMethod)` Populate the model in the following order: Retrieve "known" session attributes listed as `@SessionAttributes`.
    `void` | `updateModel(NativeWebRequest request, ModelAndViewContainer container)` Promote model attributes listed as `@SessionAttributes` to 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(@Nullable
                              List<InvocableHandlerMethod> handlerMethods,
                              WebDataBinderFactory binderFactory,
                              SessionAttributesHandler attributeHandler)

      Create a new instance with the given `@ModelAttribute` methods.
      Parameters:
        `handlerMethods` - the `@ModelAttribute` methods to invoke
        `binderFactory` - for preparation of `BindingResult` attributes
        `attributeHandler` - for access to session attributes

  - ### Method Detail

    - #### initModel

          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` - the current request
        `container` - a container with the model to be initialized
        `handlerMethod` - the method for which the model is initialized
      Throws:
        `Exception` - may arise from `@ModelAttribute` methods

    - #### updateModel

          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` - the current request
        `container` - contains the model to update
      Throws:
        `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 `@ModelAttribute` parameter annotation (if present) or falling back on parameter type based conventions.
      Parameters:
        `parameter` - a descriptor for the method parameter
      Returns:
        the derived name
      See Also:
        `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:
      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` - the value returned from a method invocation
        `returnType` - a descriptor for the return type of the method
      Returns:
        the derived name (never `null` or empty String)