0

I was looking for ways of implementing sessions in struts2. I found

1) Just use getSession() on apache

Map session = ActionContext.getContext().getSession();

2) Using SessionAware on javapoint
Instead of plain map, it uses SessionMap which is a part of struts2 and SessionAware which is too. Only thing that I dont get in tutorial, it uses SessionMap for one class and HttpSession for other class.


So the question is what is the most elegant way of using sessions with struts2 framework?

Edit: @Romann C, your answer, answers how to handle sessionMap, but I want to know which method suits most to me.

Anyone who has downvoted just now should at least leave a comment...

Varun Garg
  • 2,464
  • 23
  • 37

2 Answers2

1

Use Map session = ActionContext.getContext().getSession(); with Struts2

xrcwrn
  • 5,339
  • 17
  • 68
  • 129
1

In general you should favor the SessionAware interface. This makes testing much easier since you don't need to mock out the action context.

Regarding the tutorial; I see no reason why they used both mechanisms.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I really wanted to use that, but my code crashed (null pointer exception) as soon as I used get or set...I think it is because the class I was implementing this was not a action class, but called from it. – Varun Garg Jun 25 '15 at 18:35
  • Obviously it has to be an action class; you can't just use it on arbitrary artifacts Struts has no knowledge of. – Dave Newton Jun 25 '15 at 18:44