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

          Struts1.x系列教程(18):使用DispatchAction類調(diào)用多個(gè)Action方法

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

          上一篇:Struts1.x系列教程(17):使用IncludeAction和ForwardAction類包含和轉(zhuǎn)入Web資源

              在使用Struts動(dòng)作時(shí),每一個(gè)Action都需要編寫一個(gè)類,并且要在struts-config.xml進(jìn)行配置。這對(duì)于一個(gè)擁有很多ActionWeb程序來說,工作量是非常大的。為此,Struts提供了DispatchAction類,這個(gè)類允許將一個(gè)Action作為一個(gè)方法來調(diào)用。在Web瀏覽器中通過請(qǐng)求參數(shù)來指定要調(diào)用的動(dòng)作。
              雖然
          DispatchAction類是一個(gè)抽象類,但其中卻沒有一個(gè)抽象方法。因此,DisplatchAction的子類不用實(shí)現(xiàn)任何DisplatchAction類中的方法。但如果要處理Action代碼,就必須根據(jù)相應(yīng)的Action來編寫Action方法。一個(gè)Action方法除了方法名和execute方法不一樣外,其他的都和execute方法完全一樣。但編寫Action方法時(shí)要注意,Action方法名必須和用于指定動(dòng)作的請(qǐng)求參數(shù)值一致(大小寫也必須一致)。在下面的例子中演示了通過DispatchAction類實(shí)現(xiàn)方法和Action的對(duì)應(yīng)。在這個(gè)例子中,有三個(gè)方法:frenunspecificed。其中fren是兩個(gè)Action方法,分別用于將當(dāng)前頁面轉(zhuǎn)發(fā)到法文和英文頁面,而當(dāng)用于指定Action的請(qǐng)求參數(shù)不存在時(shí),則調(diào)用unspecificed方法(在這個(gè)方法中將當(dāng)前頁面轉(zhuǎn)發(fā)到中文頁面)。在這個(gè)程序中使用的用于指定Action的請(qǐng)求參數(shù)為language(讀者可以指定自己的請(qǐng)求參數(shù))。
              在
          <samples工程目錄>\src\action目錄建立一個(gè)MyDispatchAction.java文件,代碼如下:

            package action;
            
            
          import javax.servlet.RequestDispatcher;
            
          import javax.servlet.http.*;
            
          import org.apache.struts.action.*;
            
          import org.apache.struts.actions.*;
            
            
          public class MyDispatchAction extends DispatchAction
            {
                
          // forward到法文頁面
                public ActionForward fr(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response)
                {
                    
          try
                    {
                        RequestDispatcher rd 
          = request.getRequestDispatcher("/newGlobal.jsp?language=fr");
                        rd.forward(request, response);
                    }
                    
          catch (Exception e)
                    {
                    }
                    
          return null;
                }
                
          // forward到英文頁面
                public ActionForward en(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response)
                {
                    
          try
                    {
                        RequestDispatcher rd 
          = request.getRequestDispatcher("/newGlobal.jsp?language=en");
                        rd.forward(request, response);
                    }
                    
          catch (Exception e)
                    {
                    }
                    
          return null;
                }
                
          // 在未使用language=fr和language=en作為訪問參數(shù)的情況時(shí)調(diào)用這個(gè)方法
                protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response)
                        
          throws Exception
                {
                    
          try
                    {
                         
          // forward到中文頁面
                        RequestDispatcher rd = request.getRequestDispatcher("/newGlobal.jsp?language=zh");
                        rd.forward(request, response);
                    }
                    
          catch (Exception e)
                    {  
                    }
                    
          return null;
                }
            }

              在struts-config.xml文件中加入如下的配置代碼來配置MyDispatchAction

          <action path="/locale" type="action.MyDispatchAction" parameter="language"/>

          其中parameter參數(shù)表示用于指定Action的請(qǐng)求參數(shù)名。

          在啟動(dòng)Tomcat后,可通過如下的URL進(jìn)行測(cè)試:

             
          顯示英文頁面:

          http://localhost:8080/samples/locale.do?language=en

             
          顯示法文頁面:

          http://localhost:8080/samples/locale.do?language=fr

          顯示中文頁面(默認(rèn)頁面):

          http://localhost:8080/samples/locale.do

          雖然上面的代碼可以很好地調(diào)用相應(yīng)的Action方法,但在一些情況時(shí),如請(qǐng)求參數(shù)language指定的Action方法不存在時(shí),就會(huì)拋出異常。那么如果我們想在非正常情況下都調(diào)用默認(rèn)的處理Action動(dòng)作的方法(也就是unspecificed方法)該怎么辦呢?

          實(shí)現(xiàn)上,實(shí)現(xiàn)這個(gè)功能也非常簡單,只要我們知道在什么條件下調(diào)用unspecified方法,然后在非正常情況下,都將條件設(shè)為調(diào)用unspecified方法的條件就可實(shí)現(xiàn)這個(gè)功能。在查看DispatchAction類的源代碼后,可找到如下的代碼片段:

          if (name == null)   // name表示Action方法名
          {
              return this.unspecified(mapping, form, request, response);
          }

          從上面的代碼可知,只有當(dāng)namenull時(shí)才會(huì)調(diào)用unspecified方法。這個(gè)name值實(shí)際上也就是language參數(shù)的值。也就是說,只有當(dāng)language參數(shù)不存在時(shí),name才會(huì)為null。如果在language的參數(shù)值所指的Action方法不存在時(shí)或者name為空串的情況下都將name設(shè)為null,那么就可以達(dá)到我們的目的。

          DispatchAction類中有一個(gè)dispatchMethod方法,可以在這個(gè)方法中處理請(qǐng)求參數(shù)值為空串(也就是當(dāng)“language=”時(shí)將方法名設(shè)為null)和Action方法未找到的情況。在Action類中有兩個(gè)特殊方法:executeperform。如果調(diào)用了這兩個(gè)方法,將會(huì)出現(xiàn)遞歸調(diào)用的情況。因此,在調(diào)用這兩個(gè)方法時(shí)也需要將方法名設(shè)為null。這個(gè)工作可以在DispatchAction類的getMethodName方法中實(shí)現(xiàn)。為了完成這個(gè)功能,需要將上面的代碼放到MyDispatchAction類中。 

          protected ActionForward dispatchMethod(ActionMapping mapping,
                      ActionForm form, HttpServletRequest request,
                      HttpServletResponse response, String name) 
          throws Exception
          {
                ActionForward af 
          = null;
                
          // 在language參數(shù)值為空串的情況下,將方法名賦為null
                if (name != null)  // name表示Action方法名,也是language的參數(shù)值     
                   
          if (name.equals(""))
                        name 
          = null;
                
          try
                {
                       af 
          = super.dispatchMethod(mapping, form, request, response, name);
                }
                
          catch(NoSuchMethodException e)  // 處理Action方法未找到的情況
                {
                     
          // 在language的參數(shù)值沒有對(duì)應(yīng)的Action方法時(shí),將方法名賦為null
                    name = null;            
                    af 
          = super.dispatchMethod(mapping, form, request, response, name);
                }
                
          return af;
          }
            
          // 當(dāng)language的參數(shù)值為execute或perfore時(shí),必須將方法名賦為null,否則會(huì)出現(xiàn)遞歸調(diào)用
          protected String getMethodName(ActionMapping mapping, ActionForm form,
                    HttpServletRequest request, HttpServletResponse response,
                    String parameter) 
          throws Exception
          {
                String methodName 
          = super.getMethodName(mapping, form, request, response, parameter);
                
          if ("execute".equals(methodName) || "perform".equals(methodName))
                     
          return null;  // 如果訪問的是execute和perform,直接將方法名設(shè)為null
                return methodName;
          }

            現(xiàn)在我們可以用任何請(qǐng)求參數(shù)來訪問locale動(dòng)作,只要未找到Action方法,就會(huì)調(diào)用默認(rèn)的unspecified方法。讀者可以使用如下的URL來實(shí)驗(yàn)一下:

          http://localhost:8080/samples/locale.do?language=

          http://localhost:8080/samples/locale.do?language=unknown


          下一篇:Struts1.x系列教程(19):LookupDispatchAction類處理一個(gè)form多個(gè)submit





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

          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 2009-02-20 14:01 銀河使者 閱讀(5829) 評(píng)論(5)  編輯  收藏 所屬分類: javaweb 原創(chuàng)struts1.x

          評(píng)論

          # re: Struts1.x系列教程(18):使用DispatchAction類調(diào)用多個(gè)Action方法  回復(fù)  更多評(píng)論   

          這樣的BLOG才有用,比那些忽悠的好多了。
          2009-02-20 17:34 | bad

          # re: Struts1.x系列教程(18):使用DispatchAction類調(diào)用多個(gè)Action方法[未登錄]  回復(fù)  更多評(píng)論   

          強(qiáng)烈建議樓主制作PDF,提供下載
          2009-02-21 15:12 | L

          # re: Struts1.x系列教程(18):使用DispatchAction類調(diào)用多個(gè)Action方法  回復(fù)  更多評(píng)論   

          哈哈,struts 1.x快發(fā)完了,完事統(tǒng)一做pdf
          2009-02-21 15:34 | 銀河使者

          # re: Struts1.x系列教程(18):使用DispatchAction類調(diào)用多個(gè)Action方法  回復(fù)  更多評(píng)論   

          我一般不回帖的,強(qiáng)烈支持作者。
          2009-05-14 21:36 | Harold.Zhang

          # re: Struts1.x系列教程(18):使用DispatchAction類調(diào)用多個(gè)Action方法  回復(fù)  更多評(píng)論   

          我頂啊
          2009-12-30 22:15 | 樓主說得好
          主站蜘蛛池模板: 柘荣县| 台前县| 长春市| 拜城县| 吉木乃县| 明水县| 海城市| 韶关市| 府谷县| 崇义县| 涿鹿县| 任丘市| 义马市| 宁远县| 措勤县| 莎车县| 根河市| 阳泉市| 黑山县| 新竹县| 神木县| 北海市| 扶余县| 大港区| 贞丰县| 城固县| 内江市| 新巴尔虎右旗| 长顺县| 黄冈市| 霸州市| 淮阳县| 南开区| 新邵县| 中西区| 海淀区| 昌乐县| 肇庆市| 万山特区| 青海省| 垣曲县|