Apache Struts 2 Documentation > Home > FAQs > How do we get access to the session |
You can obtain the session attributes by asking the ActionContext or implementing SessionAware. Implementing SessionAware is preferred.
The session attributes are available on the ActionContext instance, which is made available via ThreadLocal.
Map attibutes = ActionContext.getContext().getSession();
_Preferred_
Map parameters = this.getSession();
When the servlet-config Interceptor sees that an Action implements ParameterAware, it passes a Map of the session attributes to the Action's setParameters method. Changes made to the Map are reflected in the runtime HttpSessionRequest. |
To unit test a SessionAware Action, create your own Map with the pertinent session attributes and call setSession as part of the test's setUp method. |
@see struts-default.xml
@see org.apache.struts.acton2.interceptor.SessionAware
@see org.apache.struts.acton2.interceptor.Servlet Config Interceptor