Class SessionAttributesHandler
java.lang.Object
org.springframework.web.method.annotation.SessionAttributesHandler
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, Juergen Hoeller
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringKey for known-attribute-names storage (a String array) as a session attribute. -
Constructor Summary
ConstructorsConstructorDescriptionSessionAttributesHandler(Class<?> handlerType, SessionAttributeStore sessionAttributeStore) Create a new session attributes handler. -
Method Summary
Modifier and TypeMethodDescriptionvoidcleanupAttributes(WebRequest request) Remove "known" attributes from the session, i.e.booleanWhether the controller represented by this instance has declared any session attributes through anSessionAttributesannotation.booleanisHandlerSessionAttribute(String attributeName, Class<?> attributeType) Whether the attribute name or type match the names and types specified via@SessionAttributeson the underlying controller.retrieveAttributes(WebRequest request) Retrieve "known" attributes from the session, i.e.voidstoreAttributes(WebRequest request, Map<String, ?> attributes) Store a subset of the given attributes in the session.
-
Field Details
-
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
-
-
Constructor Details
-
SessionAttributesHandler
Create a new session attributes handler. Session attribute names and types are extracted from the@SessionAttributesannotation, if present, on the given type.- Parameters:
handlerType- the controller typesessionAttributeStore- used for session access
-
-
Method Details
-
hasSessionAttributes
public boolean hasSessionAttributes()Whether the controller represented by this instance has declared any session attributes through anSessionAttributesannotation. -
isHandlerSessionAttribute
Whether the attribute name or type match the names and types specified via@SessionAttributeson the underlying controller.Attributes successfully resolved through this method are "remembered" and subsequently used in
retrieveAttributes(WebRequest)andcleanupAttributes(WebRequest).- Parameters:
attributeName- the attribute name to checkattributeType- the type for the attribute
-
storeAttributes
Store a subset of the given attributes in the session. Attributes not declared as session attributes via@SessionAttributesare ignored.- Parameters:
request- the current requestattributes- candidate attributes for session storage
-
retrieveAttributes
Retrieve "known" attributes from the session, i.e. attributes listed by name in@SessionAttributesor 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
Remove "known" attributes from the session, i.e. attributes listed by name in@SessionAttributesor attributes previously stored in the model that matched by type.- Parameters:
request- the current request
-