铁手剑谱

上善若水
数据加载中……
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 铁手 阅读(1655) 评论(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
  回复  更多评论    

只有注册用户登录后才能发表评论。


网站导航: