1. Introduction to actioncontext
Actioncontext (COM. opensymphony. xwork. actioncontext is the context of Action execution. The context can be considered as a container (in fact, the container here is just a map ), it stores the objects required for action execution. For example, when webwork is used, our context contains Request Parameters and sessions) servlet context and locale.
Unlike strut1, actions in struts2 do not depend on any Web container, and they do not need to be associated with the complex requests and responses of the assumervlet. For request parameters (PARAM), you can use the interceptor framework to automatically call some get () and set () Methods to set them to the corresponding action fields. However, can retrieve request parameter values fully meet our functional requirements? No, in Web application development, in addition to automatically setting request parameters to the action field, we often need to directly obtain the request or session in the action) you even need to directly perform the HTTP request (httpservletrequest) and response (httpservletresponse) operations on the assumervlet.
Ii. thread security of actioncontext
A new actioncontext is created before each action is executed. actioncontext is thread-safe, that is, the attributes in actioncontext are unique in the same thread, in this way, my actions can be used in multiple threads.
Generally, our actioncontext is obtained through: actioncontext context = (actioncontext) actioncontext. Get.
Let's take a look at the creation of the actioncontext object: 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, it does not conflict with copies of other threads. In this way, the attributes in our actioncontext will only be visible in the corresponding current request thread to ensure its thread safety.
Actioncontext is stored in the current thread, and obtaining actioncontext is also obtained from threadlocal. Therefore, in the process of executing the interceptor, action, and result, because they are all executed in sequence in a thread, you can obtain the actioncontext in threadlocal at any time.
Iii. Operations on objects such as httpsession and httpservletrequest of action and assumervlet
1. Get httpsession
ActionContext context = ActionContext.getContext();Map<String, Object> session = context.getSession();session.put("str1", str1);
2. Get the httpservletrequest object
HttpServletRequest request = ServletActionContext. getRequest();
3. Get an array list of parameters
ActionContext context = ActionContext.getContext();Map params = context.getParameters();
The session we obtained is a map object. Why? Originally, our webwork framework repackaged many web-related objects. For example, here we repackaged the httpsession object into a map object for our actions, instead of directly dealing with the underlying httpsession. This is exactly the packaging of the framework, so that our actoion can be completely decoupled from the web layer.
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 relevant objects of assumervlet. Note: Do not use actioncontext in the action constructor. getcontext (), because some values in actioncontext may not be set at this time, and the value obtained through actioncontext may be null.
Iv. Introduction to servletactioncontext
Servletactioncontext (COM. opensymphony. webwork. servletactioncontext), this class directly inherits the actioncontext we introduced above, which provides the function of directly accessing servlet-related objects. The objects it can obtain include:
(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