隨筆 - 312, 文章 - 14, 評論 - 1393, 引用 - 0
          數(shù)據(jù)加載中……

          Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法

          本文為原創(chuàng),如需轉(zhuǎn)載,請注明作者和出處,謝謝!

          上一篇:Struts2教程5:使用Validation框架驗證數(shù)據(jù)


              在struts1.x Action類的execute方法中,有四個參數(shù),其中兩個就是responserequest。而在Struts2中,并沒有任何參數(shù),因此,就不能簡單地從execute方法獲得HttpServletResponseHttpServletRequest對象了。

              但在Struts2 Action類中仍然有很多方法可以獲得這些對象。下面就列出四種獲得這些對象的方法。

          【方法1】使用Struts2 Aware攔截器

           

              這種方法需要Action類實現(xiàn)相應(yīng)的攔截器接口。如我們要獲得HttpServletResponse對象,需要實現(xiàn)org.apache.struts2.interceptor.ServletResponseAware接口,代碼如下:

          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;
              
          // 獲得HttpServletResponse對象
              public void setServletResponse(HttpServletResponse response)
              {
                  
          this.response = response;
              }    
              
          public String execute() throws Exception
              {    
                  response.getWriter().write(
          "實現(xiàn)ServletResponseAware接口");
              }
          }

          在上面的代碼中,MyAction實現(xiàn)了一個ServletResponseAware接口,并且實現(xiàn)了setServletResponse方法。如果一個動作類實現(xiàn)了ServletResponseAware接口,Struts2在調(diào)用execute方法之前,就會先調(diào)用setServletResponse方法,并將response參數(shù)傳入這個方法。如果想獲得HttpServletRequestHttpSessionCookie等對象,動作類可以分別實現(xiàn)ServletRequestAwareSessionAwareCookiesAware等接口。這些接口都在org.apache.struts2.interceptor包中。

          如果要獲得請求參數(shù),動作類可以實現(xiàn)org.apache.struts2.interceptor. ParameterAware接口,但如果只想判斷某個參數(shù)是否存在,也可以實現(xiàn)com.opensymphony.xwork2.interceptor. ParameterNameAware接口。這個接口有一個acceptableParameterName方法,當(dāng)Struts2獲得一個請求參數(shù)時,就會調(diào)用一次。讀者可以在這個方法中將所有的請求參數(shù)記錄下來,以便以后使用。這個方法的定義如下:

          boolean acceptableParameterName(String parameterName);

          【方法2】使用RequestAware攔截器

              這種方法和第1種方法類似。動作類需要實現(xiàn)一個org.apache.struts2.interceptor.RequestAware接口。所不同的是RequestAware將獲得一個com.opensymphony.xwork2.util.OgnlValueStack對象,這個對象可以獲得responserequest及其他的一些信息。代碼如下所示:


          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();
                  
          // 枚舉所有的key值。實際上只有一個key:struts.valueStack
                  for(String key: keys)
                      System.out.println(key);
                  
          // 獲得OgnlValueStack 對象
                  OgnlValueStack stack = (OgnlValueStack)request.get("struts.valueStack");
                  
          // 獲得HttpServletResponse對象
                  response = (HttpServletResponse)stack.getContext().get(StrutsStatics.HTTP_RESPONSE);
                  response.getWriter().write(
          "實現(xiàn)RequestAware 接口");
              }
          }

              我們也可以使用StrutsStatics.HTTP_REQUESTStrutsStatics.PAGE_CONTEXT來獲得HttpServletRequestPageContext對象。這種方法有些麻煩,一般很少用,讀者可以作為一個參考。

          【方法3】使用ActionContext

              這種方法比較簡單,我們可以通過org.apache.struts2.ActionContext類的get方法獲得相應(yīng)的對象。代碼如下:

              HttpServletResponse response = (HttpServletResponse)

          ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);

          HttpServletRequest request = (HttpServletRequest)

          ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);

          【方法4】使用ServletActionContext

              Struts2為我們提供了一種最簡單的方法獲得HttpServletResponse及其他對象。這就是org.apache.struts2.ServletActionContext類。我們可以直接使用ServletActionContext類的getRequestgetResponse方法來獲得HttpServletRequestHttpServletResponse對象。代碼如下:

              HttpServletResponse response = ServletActionContext.getResponse()

              response.getWriter().write("hello world");

              從這四種方法來看,最后一種是最簡單的,讀者可以根據(jù)自己的需要和要求來選擇使用哪一種方法來獲得這些對象。


          下一篇:Struts2教程7:上傳任意多個文件




          Android開發(fā)完全講義(第2版)(本書版權(quán)已輸出到臺灣)

          http://product.dangdang.com/product.aspx?product_id=22741502



          Android高薪之路:Android程序員面試寶典 http://book.360buy.com/10970314.html


          新浪微博:http://t.sina.com.cn/androidguy   昵稱:李寧_Lining

          posted on 2008-04-22 18:06 銀河使者 閱讀(37249) 評論(16)  編輯  收藏 所屬分類: Struts2 原創(chuàng)

          評論

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法  回復(fù)  更多評論   

          好像真比struts1.0麻煩
          2008-04-23 17:40 | 懶人

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法  回復(fù)  更多評論   

          那要看如何用了。一般情況下,在action類中不需要使用request和response。而只是做一跳轉(zhuǎn)的動作。這樣一來。就顯得struts1.x的execute的四個參數(shù)有些多余。因為大多數(shù)時候用不著。
          2008-04-23 18:58 | 銀河使者

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法  回復(fù)  更多評論   

          現(xiàn)在市場上主流的java開發(fā)技術(shù)是什么?struts1.0還是2.0,雖然2.0剛起步,但是我總覺得還是1.0用起來比較穩(wěn)定。
          2008-04-24 10:28 | 懶人

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法  回復(fù)  更多評論   

          to: 懶人
          沒實際開發(fā)過就別亂說struts2不好,好不.麻煩是因為切面切的. 這樣解耦解的多好.

          現(xiàn)在我們自己的項目里面struts和struts2一起用,除了獲取request和response麻煩些之外起碼都要比1要好用的多,尤其標(biāo)簽.

          ps:struts2里不要的地方有2處.
          1:2.09以上的版本標(biāo)簽和資源文件不支持EL表達式了.
          2:struts的標(biāo)簽有地方支持ognl表達式有的地方就不支持,就抵換成EL表達式了.
          2008-04-24 14:38 | 皮蛋

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法  回復(fù)  更多評論   

          我就是感覺標(biāo)簽太多記不太住,可能我沒經(jīng)常用習(xí)慣吧。用習(xí)慣了自然就好了
          2008-04-24 15:36 | 懶人

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法[未登錄]  回復(fù)  更多評論   

          用多了就好了,其實2比1在配置一些東西時簡單一些。
          標(biāo)簽就不說了,2比1好太多了,而其就一個,不像1有3個
          2008-04-24 16:19 | jarry

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法  回復(fù)  更多評論   

          實際上,struts1或struts2的標(biāo)簽根本不用記,只要知道大概有什么樣的標(biāo)簽就可以了,有很多IDE(如MyEclipse)都會將這些了標(biāo)簽自動列出來的,包括它們的屬性。你要知道的就是這些屬性和標(biāo)簽都起什么作用就可以了,至于它們的名子,基本上不用記,頂多知道前幾個字母就可以了。

          哈哈,我想沒人用記事本來編寫java程序吧(練習(xí)除外)。
          2008-04-24 20:21 | 銀河使者

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法  回復(fù)  更多評論   

          現(xiàn)在不考研的沒有那么較真的吧呵呵
          2008-04-25 10:45 | 懶人

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法[未登錄]  回復(fù)  更多評論   

          引用:http://www.cnblogs.com/Bruce_H21/archive/2008/05/22/1204649.html
          2008-05-22 11:35 | neo0820

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法  回復(fù)  更多評論   

          方法三里的HttpServletResponse response(HttpServletResponse) =

          ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
          好像寫錯了吧,我無法通過驗證呢
          會不會是HttpServletResponse response =(HttpServletResponse)

          ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
          還是另有玄機?
          2008-07-12 17:38 | 上善

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法  回復(fù)  更多評論   

          @上善
          是的,一個筆誤,已經(jīng)改過來了,多謝提醒。如果還有什么地方寫的有錯誤,歡迎大家指正。
          2008-07-13 14:04 | 銀河使者

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法[未登錄]  回復(fù)  更多評論   

          樓主寫的很好,支持一下:)

          另外有個小小的筆誤:
          // 獲得OgnlValueStack 對象
          OgnlValueStack stack = (OgnlValueStack)myRequest.get("struts.valueStack");

          這里的myRequest 應(yīng)該是 request.
          2009-02-02 16:21 | Sam

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法  回復(fù)  更多評論   

          @Sam
          是的,感謝提醒,已經(jīng)改過來了。
          2009-02-02 16:54 | 銀河使者

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法[未登錄]  回復(fù)  更多評論   

          版主用心良苦,寫得比培訓(xùn)教材還要詳細、全面。版主的共享精神值得學(xué)習(xí)。
          2011-05-04 15:54 | ahome

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法[未登錄]  回復(fù)  更多評論   

          內(nèi)容很好,一看就懂!
          2013-04-30 12:08 | abc

          # re: Struts2教程6:在Action類中獲得HttpServletResponse對象的四種方法[未登錄]  回復(fù)  更多評論   

          great!
          2013-05-01 09:07 | abc
          主站蜘蛛池模板: 确山县| 新化县| 玉屏| 双桥区| 永顺县| 富宁县| 贵港市| 宁强县| 西宁市| 彩票| 米泉市| 新乐市| 永济市| 敦化市| 景德镇市| 建宁县| 海安县| 九龙城区| 师宗县| 湖南省| 钦州市| 鄂尔多斯市| 太和县| 富平县| 兴化市| 基隆市| 涡阳县| 那曲县| 辽阳县| 阿拉尔市| 禹城市| 博湖县| 河北省| 桃源县| 临沧市| 酒泉市| 木里| 武威市| 昂仁县| 桐梓县| 通海县|