第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( )方法;但是該方法已經不贊成使用了。
]

|
如果你直接從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的例外處理。