struts2中 ServletActionContext與ActionContext區別

轉載:http://blog.csdn.net/lushuaiyin/article/details/7017931java

1. ActionContextweb

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

  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();session

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

經過ActionContext取得HttpSession: Map session = ActionContext.getContext().getSession();函數

2. ServletActionContextui

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

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

(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的上下文對象。

 

[java]  view plain copy
  1.     <pre name="code" class="java">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);</pre><br>  
  6. <p></p>  
  7. <pre></pre>  
  8. <br>  
  9. <br>  
  10. <p></p>  
  11. <p><span style="font-size:16px">細心的朋友能夠發現這裏的session是個Map對象, 在Struts2中底層的session都被封裝成了Map類型.  
  12. <br>  
  13. </span></p>  
  14. <p><span style="font-size:16px">咱們能夠直接操做這個Map對象進行對session的寫入和讀取操做, 而不用去直接操做HttpSession對象.</span></p>  
  15. <p><span style="font-size:16px">方法二:使用org.apache.struts2.ServletActionContext類</span></p>  
  16. <p><strong></strong></p><pre name="code" class="java">    public class UserAction extends ActionSupport {  
  17.           
  18.         //其餘代碼片斷  
  19.           
  20.         private HttpServletRequest req;   
  21.     //  private HttpServletRequest req = ServletActionContext.getRequest(); 這條語句放在這個位置是錯誤的,一樣把這條語句放在構造方法中也是錯誤的。  
  22.         public String login() {  
  23.             req = ServletActionContext.getRequest(); //req的得到必須在具體的方法中實現  
  24.             user = new User();  
  25.             user.setUid(uid);  
  26.             user.setPassword(password);  
  27.             if (userDAO.isLogin(user)) {  
  28.                 req.getSession().setAttribute("user", user);  
  29.                 return SUCCESS;  
  30.             }  
  31.             return LOGIN;  
  32.         }  
  33.         public String queryAll() {  
  34.             req = ServletActionContext.getRequest(); //req的得到必須在具體的方法中實現  
  35.             uList = userDAO.queryAll();  
  36.             req.getSession().setAttribute("uList", uList);  
  37.             return SUCCESS;  
  38.         }  
  39.           
  40.         //其餘代碼片斷  
  41.     }</pre><br>  
  42. <br>  
  43. <p></p>  
  44. <p><span style="font-size:16px"><strong>(2)IoC方式(即便用Struts2 Aware攔截器)</strong></span></p>  
  45. <p>要使用IoC方式,咱們首先要告訴IoC容器(Container)想取得某個對象的意願,經過實現相應的接口作到這點。</p>  
  46. <pre name="code" class="java">    public class UserAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {  
  47.         private HttpServletRequest request;   
  48.         private HttpServletResponse response;   
  49.         public void setServletRequest(HttpServletRequest request) {  
  50.             this.request = request;   
  51.         }  
  52.         public void setServletResponse(HttpServletResponse response) {  
  53.             this.response = response;   
  54.         }  
  55.         public String execute() {   
  56.             HttpSession session = request.getSession();   
  57.             return SUCCESS;   
  58.         }  
  59.     }</pre><br>  
  60. <br>  
  61.      
1
相關文章
相關標籤/搜索