Example usage for org.springframework.web HttpSessionRequiredException HttpSessionRequiredException

List of usage examples for org.springframework.web HttpSessionRequiredException HttpSessionRequiredException

Introduction

In this page you can find the example usage for org.springframework.web HttpSessionRequiredException HttpSessionRequiredException.

Prototype

public HttpSessionRequiredException(String msg, String expectedAttribute) 

Source Link

Document

Create a new HttpSessionRequiredException.

Usage

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

/**
 * Populate the model in the following order:
 * <ol>//from w w w.j ava2  s  .c o  m
 * <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);
        }
    }
}