Three ways to access Servlet API in Struts2

Content from: Three ways to access the Servlet API in Struts2

 

Sometimes we need to use the Request, Response, Session, Page, ServletContext objects that we used to use, so how to use these objects in Struts2, there are usually three ways.

 

1. Completely decoupled method
If this method is used, the Struts2 framework provides a class, the ActionContext class, which provides some methods to obtain the Servlet API through methods.
Some commonly used methods are as follows:

// private ActionContext context ; // This method can also get the value stack (getValueStack()), use push (obj) to put data into the value stack     
    private Map request;  
     private Map session ;    
     private Map application ;  
     public LoginAction1(){  
       request = (Map)ActionContext.getContext().get("request");   //获取request  
       session = ActionContext.getContext().getSession();          //取得session  
       application = ActionContext.getContext().getApplication();   //取得application  
       request.put("r1","r1");  
       session.put("i1","i1");  
       application.put("a1","a1");  
       return SUCCESS;
     }

Second, use the static method in the ServletActionContext to directly access the Servlet API
Struts2 provides a static class, the methods in it can get our HttpServletResponse, HttpServletRequest.
The specific methods of ServletActionContext are as follows
        * getPageContext();
        * getRequest()
        * getResponse();
        * getServletContext(); //That is, the application object
Note that there is no getSession() here, we can get the session object through the request object

private HttpServletRequest request;  
private HttpSession session;  
private ServletContext application;  
public LoginAction(){  
request = ServletActionContext.getRequest(); // Get the request object 
session = request.getSession();   // Get the session from the request 
application = session.getServletContext();   // Get the application from the session 
}  

3. The method of using interface injection
Although Struts2 provides ActionContext to access the Servlet API, this method cannot directly obtain the Servelt API instance after all. In order to directly access the Servlet API in Action, Struts2 also provides a series of interfaces.
ServletContextAware After implementing this interface, you can get ServletContext
ServletRequestAware After implementing this interface, you can get HttpServletRequest
ServletResponseAware After implementing this interface, you can get HttpServletResponse
SessionAware After implementing this interface, you can get HttpSession, note that this is a bit special, what is obtained is a Map<String , Object> session, the interceptor is responsible for parsing the keys and values ​​stored in the session, and one-to-one correspondence. 
So our usual approach is:

public class BaseAction implements ServletResponseAware, ServletRequestAware,
   SessionAware {
protected HttpServletResponse response;
protected HttpServletRequest request;
protected Map<String, Object> session;
//省略了get和set方法

// The following can directly use the above response, request and session

}
}

In order for BaseAction to have the function of verification and cannot be instantiated, we will do this in development:

public abstract class BaseAction extends ActionSupport implements ServletResponseAware, ServletRequestAware, SessionAware 

Then let the Action of each module inherit this BaseAction class, and then we can use Servelt's API directly in the Action.

Guess you like

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