strutsspring:深入探讨 Spring 和 Struts 的集成方案来源: 发布时间:星期四, 2008年12月18日 浏览:3次 评论:0
=a14c id=zoom>
Spring是个轻量级(大小和系统开支角度)IoC和AOP容器.它力图简化J2EE开发即J2EE without EJB.而且作为帮助企业级开发核心支柱,Spring为模型层(OR持久层:Hibernate、JDO、iBatis等)服务层(EJB、JNDI、WebService)以及表现层(Struts、JSF、Velocity)都提供了良好支持和集成方案. 访问Spring官方站
Jakarta-Struts是Apache软件Software组织提供个开源项目.它为Java Web应用提供了基于Model-View-ControllerMVC框架,尤其适用于开发大型可扩展Web应用.尽管基于JavaMVC框架层出不穷,事实上SpringMVC模型也提供了驱动应用系统Web层能力,但Jakarta-Struts仍然是所有这些框架中佼佼者. 下面,将如何整合这两个J2EE领域经典项目给出两套详尽集成方案. 1.首先我们来看个Spring-Struts整合应用下控制器Action类源代码. public CourceAction extends Action { private CourceService courceService; public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Set allCources = courceService.getAllCources; //..........the other statements request.Attribute("cources", allCources); mapping.findForward("jspView"); } } 分析:CourceService为个业务实现接口,此接口声明了系列业务处理思路方法.该思路方法实现配置为Spring上下问个Bean.由此看来,我们大家都可能会产生个疑问:Struts action如何取得个包含在Spring上下文中Bean呢?为了回答这个问题,Spring提供了两种和Struts集成方式: (1).从个知晓Spring上下文基类派生我们自己Struts Action类.然后,在派生类中就可以使用super.XX思路方法来获得个对Spring受控Bean引用. (2).将请求委托给作为Spring Bean管理Struts Action来处理. 2.注册Spring插件:为了使Struts Action能够访问由Spring管理Bean,我们就必须要注册个知道Spring应用上下文Struts插件.可以在struts-config.xml中通过如下方式来完成注册. < plug-in name="org.springframework.web.struts.ContextLoadPlugin"> < -property value="WEB-INF/Yhcip.xml,......" property="contextConfigLocation"> < /PLUG-IN> ContextLoadPlugin负责装载个Spring应用上下文.(具体说:是个WebApplicationContext).value属性值为要加载配置Spring受控Beanxml文件URI. 3.完成第种集成方案:实现个知晓SpringAction基类. 这种集成方案是从个公共能够访问Spring应用上下文基类中派生所有Struts Action,但值得庆幸是:我们不用自己去编写这个知晓Spring应用上下文基类,Spring已经提供了org.springframework.web.struts.ActionSupport:个org.apache.struts.action.Action抽象实现.它重载了Servlet思路方法以从ContextLoaderPlugin中获取WebapplicationContext.因此,任何时候我们只需要super.getBean思路方法即可获得Spring上下文中个Bean引用. 我们再来看段Action源代码: public CourceAction extends ActionSupport { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //取得Spring上下文 ApplicationContext context = super.getWebApplicationContext; //取得CourceService Bean CourseService courseService = (CourseService) context.getBean("courseService"); Set allCources = courceService.getAllCources; request.Attribute("cources", allCources); //..........the other statements. mapping.findForward("jspView"); }} 分析:这个Action类由ActionSupport派生,当CourceAction需要个Spring受控Bean时:它首先基类getWebApplicationContext思路方法以取得个Spring应用上下文引用;接着它getBean思路方法来获取由Spring管理courceService Bean个引用. 小结 至此,我们已经用第种方案圆满完成了Spring和Struts集成工作.这种集成方式好处在于直观简洁容易上手.除了需要从ActionSupport中派生,以及需要从应用上下文中获取Bean的外,其他都和在非SpringStruts中编写和配置Action思路方法相似.但这种集成方案也有不利面.最为显著是:我们Action类将直接使用Spring提供特定类,这样会使我们Struts Action(即控制层)代码和Spring紧密耦合在起.这是我们不情愿看到.另外,Action类也负责查找由Spring管理Bean,这违背了反向控制(IoC)原则. 4.实现第 2种集成方案:代理和委托Action. 这种集成方案要求我们编写个Struts Action,但它只不过是个包含在Spring应用上下文中真正Struts Action个代理.该代理Action从Struts插件ContextLoaderPlugIn中获取应用上下文,从中查找真正Struts Action,然后将处理委托给真正Struts Action.这个思路方法幽雅的处在于:只有代理action才会包含Spring特定处理.真正Action可以作为org.apache.struts.Action子类来编写. 下面我们来看段在的中集成方式下Struts Action源代码: public CourceAction extends Action { private CourceService courceService; public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Set allCources = courceService.getAllCources; request.Attribute("cources", allCources); //..........the other statements. mapping.findForward("jspView"); } /* 注入CourceService */ public void CourceService(CourceService courceService) { this.courceService = courceService; }} 分析:大家可以看到,在这种方式的下,我们Struts Action类和Spring是低耦合,它仅仅依赖了Spring提供反向控制(IoC)机制把CourceService注入到了我们Action中.到此,大家肯定会有个疑问:那就是Spring到底是如何提供IoC反向控制呢?回答这个问题,我们需要完成两个步骤配置: (1).在struts-config.xml中注册Struts Action.但要注意是我们在这里注册是代理Action.幸运是,我们不必亲自编写这个类.Spring已经通过org.springframework.web.struts.DelegatingActionProxy提供了这个代理Action.具体配置思路方法如下: < action type="org.springframework.web.struts.DelegatingActionProxy" path="/listCourses"> (2)将真正Struts Action作为个Spring Bean并在Spring上下文配置文件中作为个Bean注册的.并将Action所要引用courceService注入给它. < bean ="com.eRedCIP.web.CourceAction" name="/listCourses"> < property name=""> < ref bean="courseService"> < /property> < /bean> 注意:name属性值是非常重要,它必须和struts-config.xml中path属性完全致.这是DelegatingActionProxy会使用path属性值在Spring上下文中查找真正Action.使用DelegatingActionProxy好处在于我们可以不使用任何Spring特定类来编写Struts Action.同时,Struts动作能够利用IoC取得和他合作对象.唯不足的处就是不太直观,配置相对复杂.为了使action委托显得更为直观些,我们可对这种集成方案做进步改进:使用请求委托. 5.使用请求委托. 为了使action委托看上去更为直观些,Spring提供了DelegatingRequestProcessor,另种专门用于Spring请求处理器.需要在struts-config.xml中做如下配置: < controller processor="org.springframework.web.struts.DelegatingRequestProcessor"> 这样,DelegatingRequestProcessor将告诉Struts自动将动作请求委托给Spring上下文中Action来处理.这使得我们可以在struts-config.xml中用struts action真正类型来声明它们.例如: < action type="com.eRedCIP.web.CourceAction" path="/listCourses"> 当接受到个针对/listCourses请求时,DelegatingRequestProcessor会自动从Spring上下文配置文件中查找个名为/listCoursesBean(实为个Struts Action)类. < action type="com.eRedCIP.web.CourceAction" path="/listCourses"> 0
相关文章
读者评论
发表评论 |