In struts2, action is used to manually obtain the Session and jsp page parameters.
1. ActionContext
In Struts2 development, in addition to automatically setting Request parameters to the Action field, we often need to directly obtain information about the Request or Session in the Action,
You even need to directly request the assumervlet Http (HttpServletRequest) and respond to (HttpServletResponse) operations.
We need to obtain the value of the request parameter "username" in the Action:
ActionContext context = ActionContext. getContext ();
Map params = context. getParameters ();
String username = (String) params. get ("username ");
In the context of execution, context can be considered as a container (in fact, the container here is only a Map), which stores the objects required for Action execution.
Generally, our ActionContext is obtained through: ActionContext context = (ActionContext) actionContext. get.
Let's take a look at the creation of the actionContext object here:
Static ThreadLocal actionContext = new ActionContextThreadLocal ();
ActionContextThreadLocal is an internal class implementing ThreadLocal.
ThreadLocal can be named "Thread Local variable". It provides a copy of the variable value for every thread that uses this variable, so that each thread can change its own copy independently,
This will not conflict with the copies of other threads. In this way, the attributes in our ActionContext will only be visible in the corresponding current request thread to ensure thread safety.
HttpSession: Map session = ActionContext. getContext (). getSession ();
2. ServletActionContext
ServletActionContext (com. opensymphony. webwork. ServletActionContext), this class directly inherits the ActionContext we introduced above,
It provides the ability to access Servlet-related objects directly. The following objects can be obtained:
(1) javax. servlet. http. HttpServletRequest: HTTPservlet request object
(2) javax. servlet. http. HttpServletResponse: corresponding object of HTTPservlet
(3) javax. servlet. ServletContext: Servlet context information
(4) javax. servlet. ServletConfig: Servlet configuration object
(5) javax. servlet. jsp. PageContext: Http Page Context
How to obtain Servlet-related objects from ServletActionContext:
<1> get the HttpServletRequest object: HttpServletRequest request = ServletActionContext. getRequest ();
<2> obtain the HttpSession object: HttpSession session = ServletActionContext. getRequest (). getSession ();
3. Contact ServletActionContext and ActionContext
ServletActionContext and ActionContext have some repeated functions. In our actions, how should we choose?
The principle we follow is: If ActionContext can implement our functions, it is best not to use ServletActionContext, so that our actions do not directly access Servlet-related objects.
Note: Do not use ActionContext. getContext () in the Action constructor, because some values in ActionContext may not be set at this time,
The value obtained through ActionContext may be null;
Similarly, HttpServletRequest req = ServletActionContext. getRequest () should not be placed in the constructor, nor should req be directly assigned to it as a class variable.
As for the reason, I think it is because of the static ThreadLocal actionContext = new ActionContextThreadLocal () mentioned above. here we can see that ActionContext is thread-safe,
While ServletActionContext inherits from ActionContext, so ServletActionContext is also thread-safe. thread security requires every thread to be executed independently. Therefore, req creation must also be performed independently,
Therefore, the sentence ServletActionContext. getRequest () should not be placed in the constructor or directly in the class,
Instead, it should be placed in each specific method body (eg: login (), queryAll (), insert (), etc.), so as to ensure that each time an object is generated, a req is created independently.
4. GET request, response, and session in struts2
(1) Non-IoC Mode
Method 1: Use the org. apache. struts2.ActionContext class to obtain the context object of the current Action through its static method getContext.
ActionContext ctx = ActionContext. getContext ();
Ctx. put ("liuwei", "andy"); // request. setAttribute ("liuwei", "andy ");
Map session = ctx. getSession (); // session
HttpServletRequest request = ctx. get (org. apache. struts2.StrutsStatics. HTTP_REQUEST );
HttpServletResponse response = ctx. get (org. apache. struts2.StrutsStatics. HTTP_RESPONSE );
Careful friends can find that the session here is a Map object, and the underlying sessions in Struts2 are encapsulated into the Map type.
We can directly operate on this Map object to write and read sessions, instead of directly operating on the HttpSession object.
Method 2: Use the org. apache. struts2.ServletActionContext class
Public class UserAction extends ActionSupport {
// Other code snippets
Private HttpServletRequest req;
// Private HttpServletRequest req = ServletActionContext. getRequest (); this statement is incorrect at this position. It is also wrong to put this statement in the constructor.
Public String login (){
Req = ServletActionContext. getRequest (); // obtain req must be implemented in the specific method
User = new User ();
User. setUid (uid );
User. setPassword (password );
If (userDAO. isLogin (user )){
Req. getSession (). setAttribute ("user", user );
Return SUCCESS;
}
Return LOGIN;
}
Public String queryAll (){
Req = ServletActionContext. getRequest (); // obtain req must be implemented in the specific method
UList = userDAO. queryAll ();
Req. getSession (). setAttribute ("uList", uList );
Return SUCCESS;
}
// Other code snippets
}
(2) IoC (that is, Struts2 Aware interceptor)
To use the IoC method, we must first tell the IoC Container (Container) The willingness to obtain an object and implement the corresponding interface to achieve this.
Public class UserAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
Private HttpServletRequest request;
Private HttpServletResponse response;
Public void setServletRequest (HttpServletRequest request ){
This. request = request;
}
Public void setServletResponse (HttpServletResponse response ){
This. response = response;
}
Public String execute (){
HttpSession session = request. getSession ();
Return SUCCESS;
}