org.springframework.web.method.annotation

## Class SessionAttributesHandler

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

    public class SessionAttributesHandler
    extends Object

Manages controller-specific session attributes declared via `@SessionAttributes`. Actual storage is delegated to a `SessionAttributeStore` instance.

When a controller annotated with `@SessionAttributes` adds attributes to its model, those attributes are checked against names and types specified via `@SessionAttributes`. Matching model attributes are saved in the HTTP session and remain there until the controller calls `SessionStatus.setComplete()`.

Since:
  3.1
Author:
  Rossen Stoyanchev

### Constructor Summary

Constructors
Constructor and Description
`SessionAttributesHandler(Class<?> handlerType, SessionAttributeStore sessionAttributeStore)` Create a new instance for a controller type.

### Method Summary

Methods
`void` `cleanupAttributes(WebRequest request)` Remove "known" attributes from the session, i.e.
`boolean` `hasSessionAttributes()` Whether the controller represented by this instance has declared any session attributes through an `SessionAttributes` annotation.
`boolean` `isHandlerSessionAttribute(String attributeName, Class<?> attributeType)` Whether the attribute name or type match the names and types specified via `@SessionAttributes` in underlying controller.
`Map<String,Object>` `retrieveAttributes(WebRequest request)` Retrieve "known" attributes from the session, i.e.
`void` `storeAttributes(WebRequest request, Map<String,?> attributes)` Store a subset of the given attributes in the session.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### SessionAttributesHandler

    public SessionAttributesHandler(Class<?> handlerType,
                            SessionAttributeStore sessionAttributeStore)

Create a new instance for a controller type. Session attribute names and types are extracted from the `@SessionAttributes` annotation, if present, on the given type.

Parameters:
  `handlerType` - the controller type
  `sessionAttributeStore` - used for session access

### Method Detail

#### hasSessionAttributes

    public boolean hasSessionAttributes()

Whether the controller represented by this instance has declared any session attributes through an `SessionAttributes` annotation.

#### isHandlerSessionAttribute

    public boolean isHandlerSessionAttribute(String attributeName,
                                    Class<?> attributeType)

Whether the attribute name or type match the names and types specified via `@SessionAttributes` in underlying controller.

Attributes successfully resolved through this method are "remembered" and subsequently used in `retrieveAttributes(WebRequest)` and `cleanupAttributes(WebRequest)`.

Parameters:
  `attributeName` - the attribute name to check, never `null`
  `attributeType` - the type for the attribute, possibly `null`

#### storeAttributes

    public void storeAttributes(WebRequest request,
                       Map<String,?> attributes)

Store a subset of the given attributes in the session. Attributes not declared as session attributes via `@SessionAttributes` are ignored.

Parameters:
  `request` - the current request
  `attributes` - candidate attributes for session storage

#### retrieveAttributes

    public Map<String,Object> retrieveAttributes(WebRequest request)

Retrieve "known" attributes from the session, i.e. attributes listed by name in `@SessionAttributes` or attributes previously stored in the model that matched by type.

Parameters:
  `request` - the current request
Returns:
  a map with handler session attributes, possibly empty

#### cleanupAttributes

    public void cleanupAttributes(WebRequest request)

Remove "known" attributes from the session, i.e. attributes listed by name in `@SessionAttributes` or attributes previously stored in the model that matched by type.

Parameters:
  `request` - the current request