Several methods of obtaining Request and Response in Struts2

In Struts2, there is no need to write an execute() method in the Action class as in Struts1. It only needs to be a method that returns a string (sometimes no return value is required). In struts2, the original servlet The request parameter and the response parameter are encapsulated in the interceptor, but sometimes these two parameters are still needed when programming. Here are a few ways to get this parameter:

Method 1: Use Struts2 Aware Interceptor

This method requires the Action class to implement the corresponding interceptor interface. If we want to get the HttpServletResponse object, we need the org.apache.struts2.interceptor.ServletResponseAware interface, the code is as follows:

[java]  view plain copy  
  1. publicclass MyAction extends ActionSupport implements ServletResponseAware   
  2. {  
  3.     private javax.servlet.http.HttpServletResponse response;  
  4.     // Get the HttpServletResponse object  
  5.     @Override  
  6.     publicvoid setServletResponse(HttpServletResponse response)   
  7.     {  
  8.         this.response = response;  
  9.     }      
  10.     public String execute() throws Exception  
  11.     {      
  12.         response.getWriter().write( "implement ServletResponseAware interface" );  
  13.     }  
  14. }  

In the above code, MyAction implements a ServletResponseAware interface and implements the setServletResponse method. If an action class implements the ServletResponseAware interface, Struts2 will call the setServletResponse method and pass the response parameter into this method before calling the execute method. If you want to obtain objects such as HttpServletRequest , HttpSession and Cookie , the action class can implement interfaces such as ServletRequestAware , SessionAware and CookiesAware respectively . These interfaces are in the org.apache.struts2.interceptor package.

If you want to get the request parameters, the action class can implement the org.apache.struts2.interceptor.ParameterAware interface, but if you just want to judge whether a parameter exists, you can also implement the com.opensymphony.xwork2.interceptor.ParameterNameAware interface. This interface has an acceptableParameterName method, which is called once when Struts2 gets a request parameter. Readers can record all request parameters in this method for later use. This method is defined as follows:

boolean acceptableParameterName(String parameterName);

Method 2. Use RequestAware interceptor

This method is similar to the first method . Action classes need to implement an org.apache.struts2.interceptor.RequestAware interface. The difference is that RequestAware will get a com.opensymphony.xwork2.util.OgnlValueStack object, which can get response , request and some other information. The code looks like this:

[java]  view plain copy  
  1. publicclass FirstAction extends ActionSupport implements RequestAware {   
  2.     private Map request;  
  3.     private HttpServletResponse response;  
  4.   
  5.     publicvoid setRequest(Map request) {   
  6.         this.request = request;  
  7.     }  
  8.   
  9.     public String execute() throws Exception {  
  10.         java.util.Set<String> keys = request.keySet();  
  11.         // Enumerate all key values. There is actually only one key: struts.valueStack  
  12.         for (String key : keys)  
  13.             System.out.println(key);  
  14.         // Get the OgnlValueStack object  
  15.         OgnlValueStack stack = (OgnlValueStack) request  
  16.                 .get("struts.valueStack");  
  17.         // Get the HttpServletResponse object  
  18.         response = (HttpServletResponse) stack.getContext().get(  
  19.                 StrutsStatics.HTTP_RESPONSE);  
  20.         response.getWriter().write("实现RequestAware 接口");  
  21.     }  
  22. }  
我们也可以使用StrutsStatics.HTTP_REQUESTStrutsStatics.PAGE_CONTEXT来获得HttpServletRequestPageContext对象。这种方法有些麻烦,一般很少用,读者可以作为一个参考。
方法三、 使用ActionContext

这种方法比较简单,我们可以通过org.apache.struts2.ActionContext类的get方法获得相应的对象。代码如下:

[java]  view plain  copy
  1. ActionContext ctx = ActionContext.getContext();       
  2. HttpServletRequest request =(HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);    
  3. HttpServletResponse response = (HttpServletResponse)  
  4. ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);  

方法四、 使用ServletActionContext   Struts2为我们提供了一种最简单的方法获得HttpServletResponse及其他对象。这就是org.apache.struts2.ServletActionContext类。我们可以直接使用ServletActionContext类的getRequestgetResponse方法来获得HttpServletRequestHttpServletResponse对象。代码如下:

[java]  view plain  copy
  1. HttpServletRequest request = ServletActionContext.getRequest();  
  2. HttpServletResponse response = ServletActionContext.getResponse();  

从这四种方法来看,最后一种是最简单的,读者可以根据自己的需要和要求来选择使用哪一种方法来获得这些对象。


Reprinted from: https://blog.csdn.net/lzkkevin/article/details/6679798

Guess you like

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