Search code examples
javasessionstruts2struts

How can I get stored session values in multiple Struts 2 action classes?


How can I get stored session values in multiple Struts 2 action classes?

I do not want to use SessionAware interface in all the actions.


Solution

  • You have three options:

    1. Do it right and use SessionAware.
    2. Do it right and use a base action class that uses SessionAware.
    3. Do it wrong and use ActionContext:
    Map attibutes = ActionContext.getContext().getSession();
    

    Documented on the Struts 2 wiki under How do we get access to the session.

    Why wouldn't you want to use SessionAware and make your actions more-easily testable?