鐵手劍譜

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

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

          問題

          你需要將一個(gè)基于Struts 1.0的應(yīng)用遷移到Struts 1.1.

          動(dòng)作分解

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

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

          變化

          Struts 1.1 在Struts 1.0基礎(chǔ)上作了較大變化,從功能上講,基于 Struts 1.0 的應(yīng)用可以通過使用Struts1.1中的對(duì)應(yīng)文件來替換Struts 1.0 的JAR 和TLD文件來進(jìn)行遷移,這沒什么大的困難。你需要修改所使用的標(biāo)簽庫(kù)的URI,因?yàn)樗鼈冊(cè)赟truta1.1中已經(jīng)改變;這一般來說需要修改你的 web.xml部署描述符。如果你在JSP中使用絕對(duì)URI,這些值也需要修改。Table 1-3列出了標(biāo)簽庫(kù)URI的改變。

          Table 1-3. Struts標(biāo)簽庫(kù)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 也引入了請(qǐng)求處理器RequestProcessor (org.apache.struts.action.RequestProcessor)的概念。ActionServlet將請(qǐng)求處理委托給請(qǐng)求處理器。在Struts 1.1中,你不再需要一定要擴(kuò)展ActionServlet來進(jìn)行定制化;相反,你應(yīng)該子類化RequestProcessor。如果一個(gè)基于 Struts 1.0的應(yīng)用沒有擴(kuò)展ActionServlet,那么不需要做任何修改就能使用RequestProcessor。如果ActionServlet被子類化了,你卻應(yīng)該擴(kuò)展RequestProcessor。

          另一個(gè)主要的增強(qiáng)是Struts的Action。Struts 1.1 引入了一個(gè)新方法execute( ), 即其子類應(yīng)該實(shí)現(xiàn)這個(gè)方法,而不是原來的perform()方法。Example 1-1展示了一個(gè)實(shí)現(xiàn)perform()方法的簡(jiǎn)單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的同一個(gè)例子。

          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, 例外處理不再需要在方法中執(zhí)行。Struts 1.1 現(xiàn)在支持將例外處理作為框架的一部分。我們將在第9.1式練習(xí)這個(gè)招數(shù)。

          你并不是一定要修改你的Actions 來使用execute( )方法,因?yàn)镾truts 1.1 仍舊支持perform( )方法;但是該方法已經(jīng)不贊成使用了。

          ]

          1

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

           

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

          參見

          第9.1 式Struts 1.1的例外處理。

           

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

          評(píng)論

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

          不錯(cuò)!!!

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

          本系列源改編自O(shè)'Reily的Strus Cookbook
          [引用提示]阿泠引用了該文章, 地址: http://blog.donews.com/inclear/archive/2005/11/12/624363.aspx
          主站蜘蛛池模板: 迁安市| 三原县| 昌邑市| 宁远县| 石城县| 彭州市| 海林市| 湘潭市| 南川市| 寿光市| 尉氏县| 罗山县| 抚顺市| 古交市| 保亭| 通许县| 甘孜| 田阳县| 通化市| 延长县| 微山县| 扶风县| 公主岭市| 鄯善县| 沈阳市| 谷城县| 美姑县| 双辽市| 永泰县| 秭归县| 苍溪县| 大方县| 双流县| 通渭县| 湾仔区| 仁布县| 苍溪县| 登封市| 阿拉善左旗| 陆良县| 浦县|