鐵手劍譜

          上善若水
          數據加載中……
          Struts秘籍之起式:第1.3式:遷移至Struts 1.1

          第1.3式. 從Struts 1.0遷移至Struts 1.1

          問題

          你需要將一個基于Struts 1.0的應用遷移到Struts 1.1.

          動作分解

          使用Struts1.1中對應的文件替換Struts 1.0 JAR 文件、標簽庫描述符(TLD) 文件、以及XML DTD 文件。如果你有使用Struts標簽庫絕對URI的JSP 頁面,你需要修改它們。使用新的標簽庫重新編譯和構建你的應用,解決兼容性錯誤。

          最后,你需要將原來使用不贊成API的代碼修改為使用新的Struts 1.1 API。

          變化

          Struts 1.1 在Struts 1.0基礎上作了較大變化,從功能上講,基于 Struts 1.0 的應用可以通過使用Struts1.1中的對應文件來替換Struts 1.0 的JAR 和TLD文件來進行遷移,這沒什么大的困難。你需要修改所使用的標簽庫的URI,因為它們在Struta1.1中已經改變;這一般來說需要修改你的 web.xml部署描述符。如果你在JSP中使用絕對URI,這些值也需要修改。Table 1-3列出了標簽庫URI的改變。

          Table 1-3. Struts標簽庫URI

          Struts 1.0.2 Taglib URI

          Struts 1.1 Taglib URI

          http://jakarta.apache.org/struts/tags-bean-1.0.2

          http://jakarta.apache.org/struts/tags-bean

          http://jakarta.apache.org/struts/tags-html-1.0.2

          http://jakarta.apache.org/struts/tags-html

          http://jakarta.apache.org/struts/tags-logic-1.0.2

          http://jakarta.apache.org/struts/tags-logic

          http://jakarta.apache.org/struts/tags-template-1.0.2

          http://jakarta.apache.org/struts/tags-template

          Not Available with Struts 1.0.2

          http://jakarta.apache.org/struts/tags-tiles

          Not Available with Struts 1.0.2

          http://jakarta.apache.org/struts/tags-nested

           

          Struts1.1中最明顯的改變是Struts 的ActionServlet (org.apache.action.ActionServlet) 和Action類(org.apache.struts.Action)。Struts 1.1 也引入了請求處理器RequestProcessor (org.apache.struts.action.RequestProcessor)的概念。ActionServlet將請求處理委托給請求處理器。在Struts 1.1中,你不再需要一定要擴展ActionServlet來進行定制化;相反,你應該子類化RequestProcessor。如果一個基于 Struts 1.0的應用沒有擴展ActionServlet,那么不需要做任何修改就能使用RequestProcessor。如果ActionServlet被子類化了,你卻應該擴展RequestProcessor。

          另一個主要的增強是Struts的Action。Struts 1.1 引入了一個新方法execute( ), 即其子類應該實現這個方法,而不是原來的perform()方法。Example 1-1展示了一個實現perform()方法的簡單Action例子。

          Example 1-1. Struts 1.0 Action

           

          package org.apache.struts.webapp.example;

          import java.io.IOException;
          import javax.servlet.
          *;
          import javax.servlet.http.
          *;
          import org.apache.struts.action.
          *;

          public final class ExampleAction extends Action {
              
          public ActionForward perform(ActionMapping mapping,
                           ActionForm form,
                           HttpServletRequest request,
                           HttpServletResponse response)
                      throws IOException, ServletException 
          {

                  
          try {
                      ExampleService service 
          = new ExampleService( );
                      Service.doService( );
                  }

                  
          catch (ServiceException ex) {
                      
          throw new ServletException( ex );
                  }

                  
          return (mapping.findForward("success"));
              }

          }

           

          Example 1-2則是使用Struts1.1的同一個例子。

          Example 1-2. Struts 1.1 Action

           

          package org.apache.struts.webapp.example;

          import java.io.IOException;
          import javax.servlet.
          *;
          import javax.servlet.http.
          *;
          import org.apache.struts.action.
          *;

          public final class ExampleAction extends Action {
              
          public ActionForward execute (ActionMapping mapping,
                           ActionForm form,
                           HttpServletRequest request,
                           HttpServletResponse response)
                      throws Exception 
          {

                      ExampleService service 
          = new ExampleService( );
                      Service.doService( );

                      
          return (mapping.findForward("success"));
              }

          }

           

          如你所見,基于Struts 1.1的Action, 例外處理不再需要在方法中執行。Struts 1.1 現在支持將例外處理作為框架的一部分。我們將在第9.1式練習這個招數。

          你并不是一定要修改你的Actions 來使用execute( )方法,因為Struts 1.1 仍舊支持perform( )方法;但是該方法已經不贊成使用了。

          ]

          1

          如果你直接從Struts 1.0 遷移至Struts 1.2, Struts 1.1 中的不贊成因素,比如perform( )方法,已經從Struts 1.2 API中刪除了。

           

          雖然這個方法將繼續發揮作用,但是我們還是建議你盡可能的將你的代碼修改來使用execute( )方法。這樣可以減少進一步升級到Struts 1.2的難度和工作。更明顯的是,這可以使你得到Struts 1.1 的例外處理能力的優勢。

          參見

          第9.1 式Struts 1.1的例外處理。

           

          posted on 2005-04-27 09:55 鐵手 閱讀(1665) 評論(2)  編輯  收藏 所屬分類: JavaStruts系列

          評論

          # re: Struts秘籍之起式:第1.3式:遷移至Struts 1.1 2005-05-01 18:30 wri1982

          不錯!!!
            回復  更多評論    

          # Struts 秘籍(CookBook)[TrackBack] 2005-11-12 18:29 阿泠

          本系列源改編自O'Reily的Strus Cookbook
          [引用提示]阿泠引用了該文章, 地址: http://blog.donews.com/inclear/archive/2005/11/12/624363.aspx
            回復  更多評論    
          主站蜘蛛池模板: 安图县| 铁岭市| 高台县| 沙洋县| 湘潭县| 密山市| 奎屯市| 句容市| 古田县| 南陵县| 西峡县| 清新县| 南江县| 陆河县| 方城县| 祁门县| 普兰店市| 贵南县| 保山市| 古蔺县| 上高县| 鄯善县| 关岭| 云浮市| 乌鲁木齐县| 株洲市| 津南区| 浏阳市| 东莞市| 博湖县| 五峰| 清水河县| 沙洋县| 五大连池市| 阿巴嘎旗| 建德市| 西城区| 普洱| 泸定县| 南充市| 禹州市|