在上篇文章中,我们已经从较高层解释了整个框架的结构,请求流程的基础,配置方式和Struts2和Struts1的不同之处。了解这些后从Struts 应用 迁移到Struts 2 不再是难事。
在这篇文章中,我们将会更详细地讲述如何由Struts 的action转为Struts 2的action。
一个应用的例子
这个例子选择了大家都熟悉的- weblog. 简单地介绍下这例子的功能需求:
增加一个新的日志
察看一个日志
修改一个日志
删除一个日志
列出所有日至
增删修改(CRUD),是项目中最为普遍的应用。
业务逻辑类在Struts 和Struts2 应用都是可共用的。如:
public class BlogService ...{
private static List public List public Blog create(Blog blog) ...{ ... } public void update(Blog blog) ...{ ... } public void delete(int id) ...{ ... } public Blog findById(int id) ...{ ... } } BlogService 只是个简单的业务逻辑类,并不是接口,Struts 和Struts2 的action皆可调用其实例。虽然这样设计在实际项目中会带来不必要的耦合,但我们的例子只是集中在讨论web层上,所以无关重要。 QUOTE: 工具箱: 在第一篇文章中,我们谈论了在Struts2 actions中的依赖注入的接口注入方式。这个是servlet 相关类(HttpServletRequest, HttpServletResponse, PrincipalProxy, 等.)的主要注入方式,但这并不是唯一的方式。 Struts2 可以使用Spring框架作为默认的容器时,依赖注入的setter方法就可用了。通过在action中加入setter方法(如下演示),Struts2 框架将能从Spring框架中获得正确的信息,并通过setter加载在action中。 public void setBlogService(BlogService service) ...{ this.blogService = service; } 和接口注入方式类似,我们需要一个拦截器来帮助我们完成任务,这就是ActionAutowiringInterceptor 拦截器。这样我们的业务逻辑类就通过Spring框架管理自动在action被调用之前注入到Struts2得action中。有多种的配置参数(如by name, by type 或automatically)可供选择,可以让对象和setter匹配的注入的方式根据你的需要而定。 Struts 应用中的代码 我们首先从Struts讲起。在Struts中,最普遍的做法是,对于每个需求用例(如save,update,remove,list)来说都会有对应的action类,同时也会有相应的action form类。在我们的应用中的这个方式或许不是最佳的实现方式(其他的解决方案包括使用dynamic form或者使用request来分发action),但我们例子中的做法是所有Struts开发者最熟悉的一种形式。了解了这种简单的实现方法,你有能力在迁移到Struts2时,使用更为优秀的方法。 在第一篇文章中我们谈过Struts 和Struts2 中action的区别。现在我们从UML中再次看看他们的差别。一般来说form在Struts action中的表现形式是: 下图image1.jpg 这action form将会在多个action中使用,让我们来看看它: public class BlogForm extends ActionForm ...{ private String id; private String title; private String entry; private String created; // public setters and getters for all properties } 如UML中展示的那样,其中一个限制就是必须继承ActionForm类,另外一个限制就是form中所有属性都必须是String类型,所以所有的getter和setter都必须只能接受String参数和返回String结果。 然后我们来看看action。我们这个例子中的action有view, create 和update action。 The View Action: The Create Action: public class ViewBlogAction extends Action ...{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{ BlogService service = new BlogService(); String id = request.getParameter("id"); request.setAttribute("blog",service.findById(Integer.parseInt(id))); return (mapping.findForward("success")); } } public class SaveBlogEntryAction extends Action ...{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{ BlogService service = new BlogService(); BlogForm blogForm = (BlogForm) form; Blog blog = new Blog(); BeanUtils.copyProperties( blog, blogForm ); service.create( blog ); return (mapping.findForward("success")); } } public class UpdateBlogEntryAction extends Action ...{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{ BlogService service = new BlogService(); BlogForm blogForm = (BlogForm) form; Blog blog = serv