In the execute method of the struts1.xAction class, there are four parameters, two of which are response and request. Struts2 does not have any parameters. Therefore, you cannot simply obtain the HttpServletResponse or HttpServletRequest object from the execute method.
However, there are still many methods in the Struts2 Action class to obtain these objects. The following lists four methods to obtain these objects.
Method 1: Use the Struts2 Aware interceptor
This method requires the Action class to implement the corresponding interceptor interface. To obtain the HttpServletResponse object, implement the org. apache. struts2.interceptor. ServletResponseAware interface. The Code is as follows:
Package action;
Import com. opensymphony. xwork2.ActionSupport;
Import javax. servlet. http .*;
Import org. apache. struts2.interceptor .*;
Public class MyAction extends ActionSupport implements ServletResponseAware
{
Private javax. servlet. http. HttpServletResponse response;
// Obtain the HttpServletResponse object
Public void setServletResponse (HttpServletResponse response)
{
This. response = response;
}
Public String execute () throws Exception
{
Response. getWriter (). write ("Implementing the ServletResponseAware interface ");
}
}
In the code above, MyAction implements a ServletResponseAware interface and implements the setServletResponse method. If a listener class implements the ServletResponseAware interface, Struts2 will first call the setServletResponse method before calling the execute method, and pass the response parameter into this method. To obtain objects such as HttpServletRequest, HttpSession, and Cookie, the listener class can implement interfaces such as ServletRequestAware, SessionAware, and CookiesAware. These interfaces are all in the org. apache. struts2.interceptor package.
To obtain the request parameters, the handler class can implement the org. apache. struts2.interceptor. ParameterAware interface. But if you only want to determine whether a parameter exists, you can also implement the com. opensymphony. xwork2.interceptor. ParameterNameAware interface. This interface has an acceptableParameterName method. It is called once when Struts2 obtains a request parameter. You can record all request parameters in this method for future use. The method is defined as follows:
Boolean acceptableParameterName (String parameterName );
[Method 2] use the RequestAware interceptor
This method is similar to 1st methods. The handler class must 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 some information about response, request, and others. The Code is as follows:
Package action;
Import java. util. Map;
Import org. apache. struts2 .*;
Import com. opensymphony. xwork2.ActionSupport;
Import javax. servlet. http .*;
Import com. opensymphony. xwork2.util .*;
Import org. apache. struts2.interceptor .*;
Public class FirstAction extends ActionSupport implements RequestAware
{
Private Map request;
Private HttpServletResponse response;
Public void setRequest (Map request)
{
This. request = request;
}
Public String execute () throws Exception
{
Java. util. Set <String> keys = request. keySet ();
// Enumerate all key values. Actually, there is only one key: struts. valueStack
For (String key: keys)
System. out. println (key );
// Obtain the OgnlValueStack object
OgnlValueStack stack = (OgnlValueStack) request. get ("struts. valueStack ");
// Obtain the HttpServletResponse object
Response = (HttpServletResponse) stack. getContext (). get (StrutsStatics. HTTP_RESPONSE );
Response. getWriter (). write ("implement the RequestAware interface ");
}
}
You can also use StrutsStatics. HTTP_REQUEST and StrutsStatics. PAGE_CONTEXT to obtain the HttpServletRequest and PageContext objects. This method is troublesome and rarely used. Readers can use it as a reference.
Method 3: Use the ActionContext class
This method is relatively simple. We can get the corresponding object through the get method of the org. apache. struts2.ActionContext class. The Code is as follows:
HttpServletResponse response = (HttpServletResponse)
ActionContext. getContext (). get (org. apache. struts2.StrutsStatics. HTTP_RESPONSE );
HttpServletRequest request = (HttpServletRequest)
ActionContext. getContext (). get (org. apache. struts2.StrutsStatics. HTTP_REQUEST );
Method 4: Use the ServletActionContext class
Struts2 provides us with the simplest way to get HttpServletResponse and other objects. This is the org. apache. struts2.ServletActionContext class. We can directly use the getRequest and getResponse methods of the ServletActionContext class to obtain the HttpServletRequest and HttpServletResponse objects. The Code is as follows:
HttpServletResponse response = ServletActionContext. getResponse ()
Response. getWriter (). write ("hello world ");
From the perspective of the four methods, the last one is the simplest. Readers can choose which method to use to obtain these objects according to their own needs and requirements.