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:
- publicclass MyAction extends ActionSupport implements ServletResponseAware
- {
- private javax.servlet.http.HttpServletResponse response;
- // Get the HttpServletResponse object
- @Override
- publicvoid setServletResponse(HttpServletResponse response)
- {
- this.response = response;
- }
- public String execute() throws Exception
- {
- response.getWriter().write( "implement ServletResponseAware interface" );
- }
- }
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 interceptorThis 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:
- publicclass FirstAction extends ActionSupport implements RequestAware {
- private Map request;
- private HttpServletResponse response;
- publicvoid setRequest(Map request) {
- this.request = request;
- }
- public String execute() throws Exception {
- java.util.Set<String> keys = request.keySet();
- // Enumerate all key values. There is actually only one key: struts.valueStack
- for (String key : keys)
- System.out.println(key);
- // Get the OgnlValueStack object
- OgnlValueStack stack = (OgnlValueStack) request
- .get("struts.valueStack");
- // Get the HttpServletResponse object
- response = (HttpServletResponse) stack.getContext().get(
- StrutsStatics.HTTP_RESPONSE);
- response.getWriter().write("实现RequestAware 接口");
- }
- }
方法三、 使用ActionContext类
这种方法比较简单,我们可以通过org.apache.struts2.ActionContext类的get方法获得相应的对象。代码如下:
- ActionContext ctx = ActionContext.getContext();
- HttpServletRequest request =(HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
- HttpServletResponse response = (HttpServletResponse)
- ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
方法四、 使用ServletActionContext类 Struts2为我们提供了一种最简单的方法获得HttpServletResponse及其他对象。这就是org.apache.struts2.ServletActionContext类。我们可以直接使用ServletActionContext类的getRequest、getResponse方法来获得HttpServletRequest、HttpServletResponse对象。代码如下:
- HttpServletRequest request = ServletActionContext.getRequest();
- HttpServletResponse response = ServletActionContext.getResponse();
从这四种方法来看,最后一种是最简单的,读者可以根据自己的需要和要求来选择使用哪一种方法来获得这些对象。
Reprinted from: https://blog.csdn.net/lzkkevin/article/details/6679798