Example usage for org.springframework.web.method.support ModelAndViewContainer mergeAttributes

List of usage examples for org.springframework.web.method.support ModelAndViewContainer mergeAttributes

Introduction

In this page you can find the example usage for org.springframework.web.method.support ModelAndViewContainer mergeAttributes.

Prototype

public ModelAndViewContainer mergeAttributes(@Nullable Map<String, ?> attributes) 

Source Link

Document

Copy attributes in the supplied Map with existing objects of the same name taking precedence (i.e.

Usage

From source file:org.springframework.web.method.annotation.ModelFactory.java

/**
 * Populate the model in the following order:
 * <ol>/*from   w w  w.  jav  a2  s.c om*/
 * <li>Retrieve "known" session attributes listed as {@code @SessionAttributes}.
 * <li>Invoke {@code @ModelAttribute} methods
 * <li>Find {@code @ModelAttribute} method arguments also listed as
 * {@code @SessionAttributes} and ensure they're present in the model raising
 * an exception if necessary.
 * </ol>
 * @param request the current request
 * @param container a container with the model to be initialized
 * @param handlerMethod the method for which the model is initialized
 * @throws Exception may arise from {@code @ModelAttribute} methods
 */
public void initModel(NativeWebRequest request, ModelAndViewContainer container, HandlerMethod handlerMethod)
        throws Exception {

    Map<String, ?> sessionAttributes = this.sessionAttributesHandler.retrieveAttributes(request);
    container.mergeAttributes(sessionAttributes);
    invokeModelAttributeMethods(request, container);

    for (String name : findSessionAttributeArguments(handlerMethod)) {
        if (!container.containsAttribute(name)) {
            Object value = this.sessionAttributesHandler.retrieveAttribute(request, name);
            if (value == null) {
                throw new HttpSessionRequiredException("Expected session attribute '" + name + "'", name);
            }
            container.addAttribute(name, value);
        }
    }
}