The difference between ActionContext and ServletActionContext and three methods for action to access servlet API

1. ActionContext

In the development of Struts2, in addition to automatically setting the request parameters to the fields of the Action, we often need to directly obtain some information of the request (Request) or session (Session) in the Action, and even directly request the JavaServlet Http (HttpServletRequest) , the response (HttpServletResponse) operation. We need to get the value of the request request parameter "username" in the Action:

ActionContext context = ActionContext.getContext(); Map params = context.getParameters(); String username = (String) params.get("username"); ActionContext(com.opensymphony.xwork.ActionContext) is the context of Action execution, The context can be regarded as a container (in fact, the container here is just a Map), which stores the objects that the Action needs to use when executing. In general, our ActionContext is passed through: ActionContext context = (ActionContext) actionContext .get(); to get it. Let's take a look at the creation of the actionContext object here:

static ThreadLocal actionContext = new ActionContextThreadLocal();

ActionContextThreadLocal is an internal class that implements ThreadLocal. ThreadLocal can be named "thread local variable", which provides a copy of the variable value for each thread that uses the variable, so that each thread can change its own copy independently, while It will not conflict with copies of other threads. In this way, the properties in our ActionContext will only be visible in the corresponding current request thread, thus ensuring that it is thread-safe.

通过ActionContext取得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 function of directly accessing Servlet-related objects, and the objects it can obtain are:

(1)javax.servlet.http.HttpServletRequest : HTTPservlet请求对象

(2)javax.servlet.http.HttpServletResponse : 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 get Servlet related objects from ServletActionContext:

<1>取得HttpServletRequest对象: HttpServletRequest request = ServletActionContext. getRequest();

<2>取得HttpSession对象: HttpSession session = ServletActionContext. getRequest().getSession();

  3. ServletActionContext and ActionContext contact

ServletActionContext and ActionContext have some repetitive functions. In our Action, how should we choose? The principle we follow is: if ActionContext can realize our function, then it is best not to use ServletActionContext, and let our Action not be as direct as possible. To access the related objects of the servlet.

Note: One thing to pay attention to when using ActionContext: Do not use ActionContext.getContext() in the constructor of Action, because some values ​​in ActionContext may not be set at this time, and 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 as a class variable. As for the reason, I think it is because of the static ThreadLocal actionContext = new ActionContextThreadLocal() mentioned earlier, from here we can see that ActionContext is thread-safe, and ServletActionContext inherits from ActionContext, so ServletActionContext is also thread-safe, and thread-safety requires each thread They are all carried out independently, so the creation of req also requires to be carried out independently, so the sentence ServletActionContext.getRequest() should not be placed in the constructor or directly in the class, but should be placed in each specific method body (eg : login(), queryAll(), insert(), etc.), so as to ensure that a req is independently established each time an object is generated.

  4. Get request, response and session in struts2

(1) Non-IoC method

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, in Struts2 The middle and bottom sessions are encapsulated into Map type. We can directly operate the Map object to write and read the session without directly operating 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 wrong in this position, and it is also wrong to put this statement in the constructor of.

    public String login() { req = ServletActionContext.getRequest(); //The acquisition of req must be implemented in a 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(); //req must be obtained Implement uList in a specific method = userDAO.queryAll(); req.getSession().setAttribute("uList", uList); return SUCCESS; } //Other code snippets }

(2) IoC method (that is, using Struts2 Aware interceptor)

To use the IoC method, we must first tell the IoC container (Container) that it wants to obtain an object, and do this by implementing the corresponding interface.

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;     } }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325548569&siteId=291194637