July 5, 2011

Session Management in Struts2

In order to set session-scoped attributes within a Struts2 action class, get hold of the session map either through the ActionContext object or by implementing the SessionAware interface as instructed in this example

To set an attribute from within an action class:

sessionMap.put("attributeName", "attributeValue");

To get an attribute from within an action class:

sessionMap.get("attributeName");

To set an attribute from within a result JSP:

<s:set name="attributeName" scope="session" value="attributeValue" />

To get an attribute from within a result JSP:

<s:property value="%{#session.attributeName}"/>

To programmatically invalidate a session:

sessionMap.invalidate();

To configuratively invalidate a session, refer to this example

No comments:

Post a Comment