Should I get a session through SessionAware or ActionContext?

After reading the differences between getting a session map through ActionContext.getContext().getSession() and entering it through SessionAware I was wondering which one is preferred, and why?

The API recommends using SessionAware , and I read on the Internet that using SessionAware makes it easy to test an application - SessionAware it check the only problem?

Can someone do a little work on this issue or point to links explaining this?

+4
source share
2 answers

I already answered the same in your previous question. You can use any method or you can even access the session in other ways.

one way

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

But if you use this and your action class is directly tied to an ActionContext , which is ActionContext 's special way. One of the main goals of Struts2 is to disable Action classes from the main HTTP context, as well as with other direct dependencies. Also writing test cases for a simple POJO is easier and better than in another.

By implementing the SessionAware interface, you SessionAware that you want the session to be a simple map object, it not only makes the code very loose, but it is easy to maintain and test.

I hope someone else comes along with better points on this

+2
source

SessionAware is a dependency injection approach, while ActionContext.getContext().getSession() is not. Otherwise they are identical. Both of these approaches return Map<String, Object> , unlike HttpSession , which is part of the servlet API.

0
source

All Articles