First, Actioncontext
In STRUTS2 development, in addition to automatically setting the request parameters to the Action field, it is often necessary to get some information from the action directly (request) or session, even the request to Javaservlet HTTP directly ( HttpServletRequest) and Response (HttpServletResponse) operations.
Actioncontext (Com.opensymphony.xwork.ActionContext) is the context in which the action executes, and the context can be thought of as a container (in fact, our container here is a map). It holds the object that the action needs to use when it executes. In general, our Actioncontext are obtained through: actioncontext context = (Actioncontext) actioncontext.get ().
The creation of the Actioncontext object is static ThreadLocal Actioncontext = new Actioncontextthreadlocal ().
Actioncontextthreadlocal is an inner class that implements Threadlocal. Threadlocal can be named "Thread-local variable", which provides a copy of the value of a variable for each thread that uses the variable, so that each thread can independently change its copy without conflicting with the other thread's copy. In this way, the properties in the Actioncontext are only visible in the corresponding current request thread, ensuring that it is thread-safe.
Get related object through Actioncontext
gets session:map session = Actioncontext.getcontext (). getsession ();
Get Request:map Request = (MAP) actioncontext.getcontext (). Get ("request"); The
obtains the value of the request parameter "username" in action:
If there is a submitted username, then the Param.get ("username") can be used to take the value. It is worth noting that Param.get ("username") is a string array, which is designed by struts. Since it is a string array, you need to take this value:
String value[] = (string[]) param.get ("username");
String username = "";
for (int i=0;i<value.length;i++)
{
username +=value[i];
}
Second, Servletactioncontext
Servletactioncontext (Com.opensymphony.webwork.ServletActionContext), which directly inherits Actioncontext's ability to provide direct access to servlet-related objects, So Servletactioncontext is also thread safe. Available objects:
1 Javax.servlet.http.HttpServletRequest:HTTPservlet Request Object
2 Javax.servlet.http.HttpServletResponse:HTTPservlet Response Object
3 Javax.servlet.ServletContext:Servlet Contextual Information
4 Javax.servlet.ServletConfig:Servlet Configuration Object
5 Javax.servlet.jsp.PageContext:Http Page Context
Get the servlet related objects from Servletactioncontext
Get HttpServletRequest object: HttpServletRequest request = Servletactioncontext. Getrequest ();
Get HttpSession object: HttpSession session = Servletactioncontext. Getrequest (). GetSession ();
Iii. Contact Servletactioncontext and Actioncontext
Servletactioncontext and Actioncontext have some repetitive functions, how to choose.
The principle is: Actioncontext can achieve our function, it is best not to use Servletactioncontext, let the action try not to directly access the servlet related objects.
Iv. Points of attention
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. Also httpservletrequest req = Servletactioncontext.getrequest () do not place in the constructor, and do not directly assign the Req as a class variable.
The reason is that static ThreadLocal Actioncontext = new Actioncontextthreadlocal () can see that Actioncontext is thread-safe, And servletactioncontext inherit from Actioncontext so servletactioncontext also thread safety. Thread safety requires each thread to be independent, so the creation of Req also requires independent, so servletactioncontext.getrequest () do not put in the constructor, do not put directly in the class, but should be placed in each concrete method body such as login (), Queryall (), insert (), etc., to ensure that each time the object is created independently of the establishment of a req.
Original address: http://blog.sina.com.cn/s/blog_680e2f5b01015bkq.html