整合 Struts 和 Spring:三种方式:<o:p></o:p>
1:使用 Spring的ActionSupport
类整合 Structs2:用 Spring 的 DelegatingRequestProcessor
覆盖 Struts 的 RequestProcessor
3:
将 Struts Action 管理委托给 Spring 框架 <o:p></o:p>
无论您使用哪种技术,都需要使用 Spring 的 ContextLoaderPlugin 为 Struts 的 ActionServlet 装载 Spring 应用程序环境。就像添加任何其他插件一样,简单地向您的 struts-config.xml 文件添加该插件,通过加载吧spring的配置文件加载进来:如下所示:<o:p></o:p>
<plug-in className=<o:p></o:p>
"org.springframework.web.struts.ContextLoaderPlugIn"><o:p></o:p>
<set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/><o:p></o:p>
</plug-in><o:p></o:p>
|
窍门 1. 使用 Spring 的 ActionSupport(将struts和spring耦合在一起了)<o:p></o:p>
org.springframework.web.struts.ActionSupport
类提供了一个 getWebApplicationContext()
方法。您所做的只是从 Spring 的 ActionSupport
而不是 Struts Action
类扩展您的动作,如public class SearchSubmit extends ActionSupport { //下面execute方法里的内容,将struts和spring耦合在一起了<o:p></o:p>
ApplicationContext ctx = getWebApplicationContext(); <o:p></o:p>
BookService bookService = (BookService) ctx.getBean("bookService");<o:p></o:p>
}//不继承ActionSupport;这里换成直接自己通过各种各种ApplicationContext来读取spring的bean定义//文件(这种情况不需要上面的< plug-in>标签也可以)<o:p></o:p>
窍门 2. 覆盖 RequestProcessor<o:p></o:p>
org.springframework.web.struts.DelegatingRequestProcessor
类来覆盖 Struts 的 RequestProcessor
处理程序,<controller processorClass="org.springframework.web.struts. DelegatingRequestProcessor"/>下一步是在我的 Spring 配置文件中注册该动作,<o:p></o:p>
<beans><o:p></o:p>
<bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/><o:p></o:p>
<bean name="/searchSubmit" class="ca.nexcel.books.actions.SearchSubmit"> |(1)<o:p></o:p>
<property name="bookService"><o:p></o:p>
<ref bean="bookService"/><o:p></o:p>
</property><o:p></o:p>
</bean><o:p></o:p>
</beans><o:p></o:p>
注意:在 (1) 处,我使用名称属性注册了一个 bean,以匹配 struts-config 动作映射名称。SearchSubmit
动作揭示了一个 JavaBean 属性,允许 Spring 在运行时填充属性,SearchSubmit类就是Action里面定义了 private BookService bookService的get/set方法.<o:p></o:p>
缺点: 如果您使用一个不同的 RequestProcessor
,如tiles的processor。则需要手动整合 Spring 的 DelegatingRequestProcessor
。<o:p></o:p>
窍门 3. 将动作管理委托给 Spring<o:p></o:p>
将 Strut 动作管理委托给 Spring。您可以通过在 struts-config
动作映射中注册一个代理来实现。代理负责在 Spring 环境中查找 Struts 动作。由于动作在 Spring 的控制之下,所以它可以填充动作的 JavaBean 属性,并为应用诸如 Spring 的 AOP 拦截器之类的特性带来了可能。Action类和上面那种情况的类一样<o:p></o:p>
<action path="/searchSubmit" <o:p></o:p>
type="org.springframework.web.struts.DelegatingActionProxy" |(1)<o:p></o:p>
input="/searchEntry.do"<o:p></o:p>
validate="true"<o:p></o:p>
name="searchForm"><o:p></o:p>
<forward name="success" path="/WEB-INF/pages/detail.jsp"/><o:p></o:p>
<forward name="failure" path="/WEB-INF/pages/search.jsp"/><o:p></o:p>
</action> <o:p></o:p>
是一个典型的 struts-config.xml 文件,只有一个小小的差别。它注册 Spring 代理类的名称,而不是声明动作的类名,如(1)处所示。DelegatingActionProxy 类使用动作映射名称查找 Spring 环境中的动作。这就是我们使用 ContextLoaderPlugIn
声明的环境。<o:p></o:p>
<beans><o:p></o:p>
<bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/><o:p></o:p>
<bean name="/searchSubmit" class="ca.nexcel.books.actions.SearchSubmit"><o:p></o:p>
<property name="bookService"><o:p></o:p>
<ref bean="bookService"/><o:p></o:p>
</property><o:p></o:p>
</bean> <o:p></o:p>
*Spring Bean Name与Struts Action Path相关联,当Struts加载对应的Action时,DelegatingActionProxy就根据传入的path属性,在Spring Context寻找对应bean,并将其实例返回给Struts。<o:p></o:p>
动作委托的优点不止如此。一旦让 Spring 控制您的 Struts
分享到:
相关推荐
利用控制反转 (IOC) 设计模式把一个 Struts 应用程序注入 Spring 框架,您将亲身体会到 IOC 模式的强大。
ssh整合 struts hibernate spring ssh整合 struts hibernate spring ssh整合 struts hibernate spring
在weblogic下整合struts+spring+hibernate,实现struts portlet
Struts和Spring两种整合方式 Struts+Spring Struts和Spring两种整合方式 Struts+Spring
S2SH整合 struts2 spring4 hibernate4
用DelegatingRequestProcessor和DelegatingActionProxy两种方法实现struts和spring的整合,不同的地方就在struts-config.xml文件中,当前文件中DelegatingRequestProcessor是注释的。
Struts+Spring+Hibernet整合例子 Struts+Spring+Hibernet整合例子 Struts+Spring+Hibernet整合例子 Struts+Spring+Hibernet整合例子 Struts+Spring+Hibernet整合例子
只使用spring和struts进行整合
struts+spring+hibernate整合
整合Struts + Spring + Hibernate 整合Struts + Spring + Hibernate 整合Struts + Spring + Hibernate
struts和spring整合的2种方式
Spring 整合 Struts,很强大
本文并不想介绍Struts,Spring,Hibernate的原理系统架构等,本文地目的是通过一个较复杂地实例介绍如何整合Struts,Spring,Hibernate,网上现有的例子虽然也能达到目的,但功能都比较单一,复杂的例子时会有...
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
本光盘是《整合Struts+Hibernate+Spring应用开发详解》一书的配书光盘,书中的代码按章存放, 即第二章所使用的代码放在codes文件夹的02文件夹下,依次类推。 本光盘根目录下有11个文件夹,其内容和含义说明如下: ...
图解学习、搭建、整合Struts-Spring-Hibernate框架
ssh三大框架的整合步骤,非常详细。包含spring整合struts、spring整合hibernate、声明式事务说明等。
struts1 spring ibatis整合项目源码struts1 spring ibatis整合项目源码struts1 spring ibatis整合项目源码struts1 spring ibatis整合项目源码struts1 spring ibatis整合项目源码struts1 spring ibatis整合项目源码
struts2 spring hibernate 整合