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

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

          本文為原創,如需轉載,請注明作者和出處,謝謝!

          上一篇:Struts2教程5:使用Validation框架驗證數據


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

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

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

           

              這種方法需要Action類實現相應的攔截器接口。如我們要獲得HttpServletResponse對象,需要實現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(
          "實現ServletResponseAware接口");
              }
          }

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

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

          boolean acceptableParameterName(String parameterName);

          【方法2】使用RequestAware攔截器

              這種方法和第1種方法類似。動作類需要實現一個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(
          "實現RequestAware 接口");
              }
          }

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

          【方法3】使用ActionContext

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

              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");

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


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




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

          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 原創

          評論

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

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

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

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

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

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

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

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

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

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

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

          我就是感覺標簽太多記不太住,可能我沒經常用習慣吧。用習慣了自然就好了
          2008-04-24 15:36 | 懶人

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

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

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

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

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

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

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

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

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

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

          方法三里的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對象的四種方法  回復  更多評論   

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

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

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

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

          這里的myRequest 應該是 request.
          2009-02-02 16:21 | Sam

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

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

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

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

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

          內容很好,一看就懂!
          2013-04-30 12:08 | abc

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

          great!
          2013-05-01 09:07 | abc
          主站蜘蛛池模板: 同江市| 三明市| 新余市| 连云港市| 武陟县| 辽阳市| 定边县| 温州市| 巩义市| 灵丘县| 广饶县| 长葛市| 略阳县| 辛集市| 贵德县| 南开区| 论坛| 东光县| 镇沅| 彩票| 凌云县| 丹江口市| 虞城县| 新巴尔虎左旗| 合阳县| 巫溪县| 习水县| 张家港市| 凉山| 长泰县| 呈贡县| 尚义县| 安乡县| 漾濞| 四平市| 承德县| 二连浩特市| 屏山县| 林州市| 广汉市| 陆良县|