Top Description Fields Constructors Methods
org.springframework.web.method.annotation

public Class SessionAttributesHandler

extends Object
Class Inheritance
Imports
java.util.Arrays, .Collections, .HashMap, .HashSet, .Map, .Set, java.util.concurrent.ConcurrentHashMap, org.springframework.core.annotation.AnnotatedElementUtils, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils, org.springframework.web.bind.annotation.SessionAttributes, org.springframework.web.bind.support.SessionAttributeStore, .SessionStatus, org.springframework.web.context.request.WebRequest

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().

Authors
Rossen Stoyanchev, Juergen Hoeller
Since
3.1

Field Summary

Modifier and TypeField and Description
private final Set<String>
private final Set<Class<?>>
private final Set<String>
public static final String
SESSION_KNOWN_ATTRIBUTE

Key for known-attribute-names storage (a String array) as a session attribute.

private final SessionAttributeStore

Constructor Summary

AccessConstructor and Description
public
SessionAttributesHandler(Class<?>
the controller type
handlerType
,
SessionAttributeStore
used for session access
sessionAttributeStore
)

Create a new session attributes handler.

Method Summary

Modifier and TypeMethod and Description
public void
cleanupAttributes(WebRequest
the current request
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.

public boolean
hasSessionAttributes()

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

public boolean
isHandlerSessionAttribute(String
the attribute name to check
attributeName
,
Class<?>
the type for the attribute
attributeType
)

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

pack-priv Object

Returns:

the attribute value, or null if none
retrieveAttribute
(WebRequest
the current request
request
,
String
the name of the attribute of interest
attributeName
)

A pass-through call to the underlying SessionAttributeStore.

public Map<String, Object>

Returns:

a map with handler session attributes, possibly empty
retrieveAttributes
(WebRequest
the current request
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.

public void
storeAttributes(WebRequest
the current request
request
,
Map<String, ?>
candidate attributes for session storage
attributes
)

Store a subset of the given attributes in the session.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

attributeNamesback to summary
private final Set<String> attributeNames
attributeTypesback to summary
private final Set<Class<?>> attributeTypes
knownAttributeNamesback to summary
private final Set<String> knownAttributeNames
SESSION_KNOWN_ATTRIBUTEback to summary
public static final String SESSION_KNOWN_ATTRIBUTE

Key for known-attribute-names storage (a String array) as a session attribute.

This is necessary for consistent handling of type-based session attributes in distributed session scenarios where handler methods from the same class may get invoked on different servers.

Since
6.1.4
sessionAttributeStoreback to summary
private final SessionAttributeStore sessionAttributeStore

Constructor Detail

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

Create a new session attributes handler. Session attribute names and types are extracted from the @SessionAttributes annotation, if present, on the given type.

Parameters
handlerType:Class<?>

the controller type

sessionAttributeStore:SessionAttributeStore

used for session access

Method Detail

cleanupAttributesback to summary
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:WebRequest

the current request

hasSessionAttributesback to summary
public boolean hasSessionAttributes()

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

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

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

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

Parameters
attributeName:String

the attribute name to check

attributeType:Class<?>

the type for the attribute

retrieveAttributeback to summary
pack-priv Object retrieveAttribute(WebRequest request, String attributeName)

A pass-through call to the underlying SessionAttributeStore.

Parameters
request:WebRequest

the current request

attributeName:String

the name of the attribute of interest

Returns:Object

the attribute value, or null if none

Annotations
@Nullable
retrieveAttributesback to summary
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:WebRequest

the current request

Returns:Map<String, Object>

a map with handler session attributes, possibly empty

storeAttributesback to summary
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:WebRequest

the current request

attributes:Map<String, ?>

candidate attributes for session storage