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

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

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

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

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

          在啟動Tomcat后,可通過如下的URL進行測試:

             
          顯示英文頁面:

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

             
          顯示法文頁面:

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

          顯示中文頁面(默認頁面):

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

          雖然上面的代碼可以很好地調用相應的Action方法,但在一些情況時,如請求參數language指定的Action方法不存在時,就會拋出異常。那么如果我們想在非正常情況下都調用默認的處理Action動作的方法(也就是unspecificed方法)該怎么辦呢?

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

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

          從上面的代碼可知,只有當namenull時才會調用unspecified方法。這個name值實際上也就是language參數的值。也就是說,只有當language參數不存在時,name才會為null。如果在language的參數值所指的Action方法不存在時或者name為空串的情況下都將name設為null,那么就可以達到我們的目的。

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

          protected ActionForward dispatchMethod(ActionMapping mapping,
                      ActionForm form, HttpServletRequest request,
                      HttpServletResponse response, String name) 
          throws Exception
          {
                ActionForward af 
          = null;
                
          // 在language參數值為空串的情況下,將方法名賦為null
                if (name != null)  // name表示Action方法名,也是language的參數值     
                   
          if (name.equals(""))
                        name 
          = null;
                
          try
                {
                       af 
          = super.dispatchMethod(mapping, form, request, response, name);
                }
                
          catch(NoSuchMethodException e)  // 處理Action方法未找到的情況
                {
                     
          // 在language的參數值沒有對應的Action方法時,將方法名賦為null
                    name = null;            
                    af 
          = super.dispatchMethod(mapping, form, request, response, name);
                }
                
          return af;
          }
            
          // 當language的參數值為execute或perfore時,必須將方法名賦為null,否則會出現遞歸調用
          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,直接將方法名設為null
                return methodName;
          }

            現在我們可以用任何請求參數來訪問locale動作,只要未找到Action方法,就會調用默認的unspecified方法。讀者可以使用如下的URL來實驗一下:

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

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


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





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

          評論

          # re: Struts1.x系列教程(18):使用DispatchAction類調用多個Action方法  回復  更多評論   

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

          # re: Struts1.x系列教程(18):使用DispatchAction類調用多個Action方法[未登錄]  回復  更多評論   

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

          # re: Struts1.x系列教程(18):使用DispatchAction類調用多個Action方法  回復  更多評論   

          哈哈,struts 1.x快發完了,完事統一做pdf
          2009-02-21 15:34 | 銀河使者

          # re: Struts1.x系列教程(18):使用DispatchAction類調用多個Action方法  回復  更多評論   

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

          # re: Struts1.x系列教程(18):使用DispatchAction類調用多個Action方法  回復  更多評論   

          我頂啊
          2009-12-30 22:15 | 樓主說得好
          主站蜘蛛池模板: 鹤壁市| 新密市| 大兴区| 屏南县| 邓州市| 芦山县| 临汾市| 新田县| 亳州市| 全州县| 邯郸市| 珲春市| 罗平县| 鲁山县| 烟台市| 沙洋县| 达孜县| 碌曲县| 镇原县| 黑水县| 开远市| 六盘水市| 清涧县| 简阳市| 泗水县| 伊春市| 秦皇岛市| 绥宁县| 闵行区| 工布江达县| 扎囊县| 屯留县| 高陵县| 衡水市| 永胜县| 富平县| 阿巴嘎旗| 基隆市| 平潭县| 建宁县| 沅陵县|