spring-framework / org.springframework.web.method.annotation / ModelFactory

ModelFactory

class ModelFactory

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

Constructors

<init>

ModelFactory(handlerMethods: MutableList<InvocableHandlerMethod>, binderFactory: WebDataBinderFactory, attributeHandler: SessionAttributesHandler)

Create a new instance with the given @ModelAttribute methods.

Functions

getNameForParameter

static fun getNameForParameter(parameter: MethodParameter): String

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.

getNameForReturnValue

static fun getNameForReturnValue(returnValue: Any, returnType: MethodParameter): String

Derive the model attribute name for the given return value 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

initModel

fun initModel(request: NativeWebRequest, container: ModelAndViewContainer, handlerMethod: HandlerMethod): Unit

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.

updateModel

fun updateModel(request: NativeWebRequest, container: ModelAndViewContainer): Unit

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