Access Request, Session and Application in Struts2

We often need to pass some parameters to the JSP page in Action, which can be used to get parameters, but these parameters are limited to specific data. If we want to use request, Session and application in JSP, what should we do?
one,
We can obtain Map type objects such as Request through the ActionContext object for assignment and parameter transfer. The code reference is as follows:

   1:  public String execute(){
   2:      
   3:      ((Map)ActionContext.getContext().get("request")).put("r1", "r1");//Request   Map  
   4:      ActionContext.getContext().getSession().put("s1", "s1");// Session   Map  
   5:      ActionContext.getContext().getApplication().put("a1", "a1");//Application   Map          
   6:      return SUCCESS;
   7:  } 

By defining Action in the above way, parameters can be passed. For this, we have two ways to obtain parameters on the front JSP page:
One is to access parameters through tags:

   1:  
   
    value=
    "#request.r1"/>
   
   2:  
   
    value=
    "#session.s1"/>
   
   3:  
   
    value=
    "#application.a1"/>
   

This access method and adding a # in front of the value parameter, because these are the keys in the Stack Context.
Another way to access parameters is that we are familiar with:

   1:  <%=request.getAttribute("r1") %>
   2:  <%=session.getAttribute("s1") %>
   3:  <%=application.getAttribute("a1") %>

Maybe some people don't understand, Map-type Request, Session, and Application can be accessed in this way? ! Yes, Struts2 did it, and it should have done some conversions internally, converting Map into HttpServletRequest, HttpSession and ServletContext.
This method is dependent on the Struts2 environment, because the ActionContext class is used.
 
two,
This method is achieved through Dependency Injection (DI, Dependency Injection), or Inversion of Control (IoC, Inverse of Control). This method is most commonly used.
The principle is to define Action to implement RequestAware, SessionAware, and ApplicationAware interfaces, implement the setXxxxx( Map xxx ) method defined in it, and then wait for Struts2 Context to inject request, session, application, etc. into our defined Action. (The understanding of control inversion can be considered that the variables of request, session, and application are controlled by Action itself. After implementing these Aware interfaces, the control is transferred to Struts2 Context.)
The Action code is as follows, and the other codes are as above:

   1:  package com.cdp.struts2;
   2:   
   3:  import java.util.Map;
   4:   
   5:  import org.apache.struts2.interceptor.ApplicationAware;
   6:  import org.apache.struts2.interceptor.RequestAware;
   7:  import org.apache.struts2.interceptor.SessionAware;
   8:   
   9:  import com.opensymphony.xwork2.ActionSupport;
  10:   
  11:  public class Action2 extends ActionSupport implements RequestAware,
  12:          SessionAware, ApplicationAware {
  13:      
  14:      private Map
   
     request;
   
  15:      private Map
   
     session;
   
  16:      private Map
   
     application;
   
  17:   
  18:      public String execute() {
  19:   
  20:          request.put("r1", "r1");
  21:          session.put("s1", "s1");
  22:          application.put("a1", "a1");
  23:          return SUCCESS;
  24:      }
  25:   
  26:      public void setRequest(Map
   
     request) {
   
  27:          this.request = request;
  28:   
  29:      }
  30:   
  31:      public void setSession(Map
   
     session) {
   
  32:          this.session = session;
  33:   
  34:      }
  35:   
  36:      public void setApplication(Map
   
     application) {
   
  37:          this.application = application;
  38:   
  39:      }
  40:   
  41:  }

It should be noted that this method is the most commonly used.
three,
The third way is to directly create the real request , session , and application objects that are not of type Map, as follows:

   1:  package com.cdp.struts2;
   2:   
   3:  import javax.faces.application.Application;
   4:  import javax.servlet.ServletContext;
   5:  import javax.servlet.http.HttpServletRequest;
   6:  import javax.servlet.http.HttpSession;
   7:   
   8:  import org.apache.struts2.ServletActionContext;
   9:   
  10:  import com.opensymphony.xwork2.ActionSupport;
  11:   
  12:  public class Action3 extends ActionSupport{
  13:   
  14:      private HttpServletRequest request;
  15:      private HttpSession session;
  16:      private ServletContext application;
  17:      
  18:      public String execute(){
  19:          request=ServletActionContext.getRequest();
  20:          session=request.getSession();
  21:          application=session.getServletContext();
  22:          request.setAttribute("r1", "r1");
  23:          session.setAttribute("s1", "s1");
  24:          application.setAttribute("a1", "a1");
  25:          return SUCCESS;
  26:      }
  27:      
  28:  }

Four,
The last one is also implemented by dependency injection. Action implements the ServletRequestAware interface, so that the session can be obtained through the request, and the application can be obtained through the session:

   1:  package com.cdp.struts2;
   2:   
   3:  import javax.faces.application.Application;
   4:  import javax.servlet.ServletContext;
   5:  import javax.servlet.http.HttpServletRequest;
   6:  import javax.servlet.http.HttpSession;
   7:   
   8:  import org.apache.struts2.ServletActionContext;
   9:  import org.apache.struts2.interceptor.ServletRequestAware;
  10:   
  11:  import com.opensymphony.xwork2.ActionSupport;
  12:   
  13:  public class Action4 extends ActionSupport implements ServletRequestAware{
  14:   
  15:      private HttpServletRequest request;
  16:      private HttpSession session;
  17:      private ServletContext application;
  18:      
  19:      public String execute(){
  20:          request.setAttribute("r1", "r1");
  21:          session.setAttribute("s1", "s1");
  22:          application.setAttribute("a1", "a1");
  23:          return SUCCESS;
  24:      }
  25:   
  26:      public void setServletRequest(HttpServletRequest request) {
  27:          this.request=request;
  28:          this.session=request.getSession();
  29:          this.application=session.getServletContext();
  30:      }
  31:      
  32:  }

좋은 웹페이지 즐겨찾기