Spring 框架与 Struts 框架集成有三个方案 1.使用spring的ActionSupport类集成Struts 采用这种方式须要修改Struts中的action,使struts的action继承Spring的ActionSupport,这样action中定义的业务逻辑对象可经过getBean()方法从Spring的应用上下文WebApplicationContext中得到,从而达到集成的效果。不过缺点就是使struts的Action和Spring紧密的耦合在一块儿。 下面介绍此方案集成的步骤 (1).加载applicationContext.xml 有两种方法 1、经过struts-config.xml文件来加载 <plug-in className="org.springframework.web.struts.ContextLoaderPlugin"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"> </set-property> </plug-in> 2、经过web.xml中配置 <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> (2).修改Action 以前继承的父类是Action,须要改为Spring的ActionSupport类,这样就能使用上下文对象获取applicationContext.xml配置文件里的getBean()方法 ApplicationContext context = this.getWebApplicationContext(); 业务逻辑对象 = context.getBean("idName"); 这样就不须要new来建立对象,把建立的细节很好的封装起来,彻底由spring容器plicationContext.xml 来注入管理 2.使用spring的Action代理集成Struts 采用此方法须要创建一个Spring的Action代理,代理Struts的Action,当Struts执行Action时执行这个代理。代理会在Spring应用上下文找到真正的StrutsAction,而后就交给这个Action处理,下面看看步骤 (1).加载applicationContext.xml 有两种方法 1、经过struts-config.xml文件来加载 <plug-in className="org.springframework.web.struts.ContextLoaderPlugin"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"> </set-property> </plug-in> 2、经过web.xml中配置 <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> (2)修改Action 为所须要的业务逻辑对象定义属性并增长setXXX()方法,这样作目的是为了使用spring的依赖注入。 (3)修改struts-config.xml文件 修改的内容是<action>元素的type属性值。修改内容为 type="org.springframework.web.struts.DelegatingActionProxy" (4).配置applicationContext.xml 3.最后一种有点郁闷,目前还不理解,等学成归来在写进去吧!!! (*^__^*) 嘻嘻……学成归来了 修改Struts控制器来继承struts 为了更直观的在Struts配置文件中显示出Action对应的处理类,能够将Struts配置文件中<action>元素的type属性直接显示为Struts的Action类,执行的时候还会到Spring的上下文中去查找对应的StrutsAction类。 完成这样的过程须要修改controller,将struts-config.xml中的controller改为org.springframework.web.struts.DelegatingTilesRequestProcessor若是Struts中使用了Tiles组件,则可使用org.springframework.web.struts.DelegatingTilesRequestProcessor来做为controller。若是Struts没有使用Tiles组件,则可使用org.springframework.web.struts.DelegatingRequestProcessor来做为controller。 下面说说步骤: (1).加载WebApplicationContext.上下文同以上方法的第一步骤。 (2).新建一个类 EncodingProcessor 并继承DelegatingRequestProcessor 实现 process()方法同时为了解决乱码问题必须在process()方法上添加两行代码: request.setCharacterEncoding(UTF-8); response.setContentType("text/html;charset=UTF-8"); (3).修改struts-config.xml文件,即修改Struts的RequestProcessor控制器 使用自定义的控制器controller须要在Struts-config.xml文件中对<controller>元素进行配置,代码以下 <controller processorClass="EncodingProcessor的类路劲"/> 总结:从这3中方法能够说是逐步优化,使得Struts和Spring集成变得耦合度愈来愈低,并且愈来愈直观;