Struts2的 ActionContext && ServletActionContext

1. ActionContext

在 Struts2開發中,除了將請求參數自動設置到Action的字段中,我們往往也需要在Action裏直接獲取請求(Request)或會話 (Session)的一些信息,甚至需要直接對JavaServlet Http的請求(HttpServletRequest),響應(HttpServletResponse)操作. 我們需要在Action中取得request請求參數"username"的值:

  1. ActionContext context = ActionContext.getContext();
  2. Map params = context.getParameters();
  3. String username = (String) params.get("username");

ActionContext(com.opensymphony.xwork.ActionContext)是Action執行時的上下文,上下文可以看作是一個容器(其 實我們這裏的容器就是一個Map而已),它存放的是Action在執行時需要用到的對象. 一般情況, 我們的ActionContext都是通過: ActionContext context = (ActionContext) actionContext.get();來獲取的.我們再來看看這裏的actionContext對象的創建:

static ThreadLocal actionContext = new ActionContextThreadLocal();

ActionContextThreadLocal 是實現ThreadLocal的一個內部類.ThreadLocal可以命名爲"線 程局部變量",它爲每一個使用該變量的線程都提供一個變量值的副本,使每一個線程都可以獨立地改變自己的副本,而不會和其它線程的副本衝突.這樣,我們 ActionContext裏的屬性只會在對應的當前請求線程中可見,從而保證它是線程安全的.

通過ActionContext取得HttpSession: Map session = ActionContext.getContext().getSession();

2. ServletActionContext

ServletActionContext(com.opensymphony.webwork. ServletActionContext),這個類直接繼承了我們上面介紹的ActionContext,它提供了直接與Servlet相關對象訪問的 功能,它可以取得的對象有:

(1)javax.servlet.http.HttpServletRequest : HTTPservlet請求對象

(2)javax.servlet.http.HttpServletResponse : HTTPservlet相應對象

(3)javax.servlet.ServletContext : Servlet上下文信息

(4)javax.servlet.ServletConfig : Servlet配置對象

(5)javax.servlet.jsp.PageContext : Http頁面上下文

如何從ServletActionContext裏取得Servlet的相關對象:

<1>取得HttpServletRequest對象: HttpServletRequest request = ServletActionContext. getRequest();

<2>取得HttpSession對象: HttpSession session = ServletActionContext. getRequest().getSession();

3. ServletActionContext和ActionContext聯繫

ServletActionContext和ActionContext有着一些重複的功能,在我們的Action中,該如何去抉擇呢?我們遵循的原則是:如果ActionContext能夠實現我們的功能,那最好就不要使用ServletActionContext,讓我們的Action儘量不要直接去訪問Servlet的相關對象.

注意:在使用ActionContext時有一點要注意: 不要在Action的構造函數裏使用ActionContext.getContext(),因爲這個時候ActionContext裏的一些值也許沒有設置,這時通過ActionContext取得的值也許是null;同樣,HttpServletRequest req = ServletActionContext.getRequest()也不要放在構造函數中,也不要直接將req作爲類變量給其賦值。至 於原因,我想是因爲前面講到的static ThreadLocal actionContext = new ActionContextThreadLocal(),從這裏我們可以看出ActionContext是線程安全的,而 ServletActionContext繼承自ActionContext,所以ServletActionContext也線程安全,線程安全要求每 個線程都獨立進行,所以req的創建也要求獨立進行,所以ServletActionContext.getRequest()這句話不要放在構造函數 中,也不要直接放在類中,而應該放在每個具體的方法體中(eg:login()、queryAll()、insert()等),這樣才能保證每次產生對象 時獨立的建立了一個req。

4. struts2中獲得request、response和session

(1)非IoC方式

方法一:使用org.apache.struts2.ActionContext類,通過它的靜態方法getContext()獲取當前Action的上下文對象。

 

  1. ActionContext ctx = ActionContext.getContext();
  2. ctx.put("liuwei", "andy"); //request.setAttribute("liuwei", "andy");
  3. Map session = ctx.getSession(); //session
  4. HttpServletRequest request = ctx.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
  5. HttpServletResponse response = ctx.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);

細心的朋友可以發現這裏的session是個Map對象, 在Struts2中底層的session都被封裝成了Map類型. 我們可以直接操作這個Map對象進行對session的寫入和讀取操作, 而不用去直接操作HttpSession對象.

方法二:使用org.apache.struts2.ServletActionContext類

  1. public class UserAction extends ActionSupport {
  2.     
  3.     //其他代碼片段
  4.     
  5.     private HttpServletRequest req; 
  6. //  private HttpServletRequest req = ServletActionContext.getRequest(); 這條語句放在這個位置是錯誤的,同樣把這條語句放在構造方法中也是錯誤的。
  7.     public String login() {
  8.         req = ServletActionContext.getRequest(); //req的獲得必須在具體的方法中實現
  9.         user = new User();
  10.         user.setUid(uid);
  11.         user.setPassword(password);
  12.         if (userDAO.isLogin(user)) {
  13.             req.getSession().setAttribute("user", user);
  14.             return SUCCESS;
  15.         }
  16.         return LOGIN;
  17.     }
  18.     public String queryAll() {
  19.         req = ServletActionContext.getRequest(); //req的獲得必須在具體的方法中實現
  20.         uList = userDAO.queryAll();
  21.         req.getSession().setAttribute("uList", uList);
  22.         return SUCCESS;
  23.     }
  24.     
  25.     //其他代碼片段
  26. }

(2)IoC方式(即使用Struts2 Aware攔截器)

要使用IoC方式,我們首先要告訴IoC容器(Container)想取得某個對象的意願,通過實現相應的接口做到這點。

  1. public class UserAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
  2.     private HttpServletRequest request; 
  3.     private HttpServletResponse response; 
  4.     public void setServletRequest(HttpServletRequest request) {
  5.         this.request = request; 
  6.     }
  7.     public void setServletResponse(HttpServletResponse response) {
  8.         this.response = response; 
  9.     }
  10.     public String execute() { 
  11.         HttpSession session = request.getSession(); 
  12.         return SUCCESS; 
  13.     }
  14. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章