`
chunchong
  • 浏览: 37665 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

spring/struts2 整合

阅读更多

在Struts2中整合Spring的IoC支持是一件十分简单的事情。让我们一步一步来实现:
1)复制struts2-spring-plugin-x-x-x.jar和相应的spring.jar到/WEB-INF/lib目录下。
2)在struts.properties中设置struts.objectFactory属性值
struts.properties
struts.objectFactory = spring
或者是在XML文件中进行常量配置
struts.xml
<struts>
       <constant name="struts.objectFactory" value="spring" />
</struts>
3)配置Spring监听器
web.xml
<listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

通过Spring配置来注册对象
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC 
       "-//SPRING//DTD BEAN//EN"
       "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
       <bean id="personManager" class="com.acme.PersonManager"/>
</beans>
当然你也可以拥有更多的Spring配置文件。在web.xml中进行下列设置,从而使Spring的ApplicationContext通过匹配所给定模式的文件来初始化对象
<!-- 用来定位Spring XML文件的上下文配置 -->
<context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>

4)修改你的Struts配置文件
struts.xml
<!DOCTYPE struts PUBLIC
       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
       "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
       <include file="struts-default.xml"/>
       <package name="default" extends="struts-default">
           <action name="foo" class="com.acme.Foo">
               <result>foo.ftl</result>
           </action>
       </package>
       <package name="secure" namespace="/secure" extends="default">
           <action name="bar" class="bar">
               <result>bar.ftl</result>
           </action>
       </package>
</struts>
默认情况下,Spring从下面的文件中寻找为bar所做的配置
applicationConext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC 
       "-//SPRING//DTD BEAN//EN"
       "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
       <bean id="bar" class="com.my.BarClass" singleton="false"/>
       ...
</beans>
5)好了,现在你的Struts2和Spring就能正常的一起工作了。有几个配置技术点需要详细说明下:装配模式。你可以通过设置修改struts.properties中下列属性的值来改变装配模式。
struts.objectFactory.spring.autoWire = type
该参数可供选择的设置值有
name type auto constructor
按照你的action的属性的名字和Spring里的bean的名字匹配,如果匹配就自动装配。这是缺省的
按照你的action的属性的类型,在Spring注册的bean中查找,如果相同就自动装配。这需要你在Spring中仅注册了一个此类型的bean
Spring会试图自动监测来找到最好的方法自动装配你的action
Spring会自动装配bean的构造函数的参数
是否使用类缓存。你可以通过设置修改struts.properties中下列属性的值来改变是否使用Spring自身的类缓存机制。可以设定的值为true或false,默认为true。
struts.objectFactory.spring.useClassCache = false

分享到:
评论
9 楼 maliang.scut 2010-03-13  
如果在Spring的配置中,将提供给struts2的action实例设置为单例,会有什么样的后果???
8 楼 wuqi123321 2008-12-12  
    
7 楼 aishangtao 2008-12-09  
很好用的
6 楼 blurm 2008-11-13  
第二步不需要,在plugin包里的struts-plugin.xml就已经配置好了
5 楼 wanghua305 2008-10-28  
要是报错Action class [bar] not found - action什么原因??
4 楼 boansen520 2008-09-17  
报这样的错误
[ERROR]main-org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/ishop]-Exception starting filter struts2
Cannot locate the chosen ObjectFactory implementation: spring - [unknown location]
3 楼 小小鱼 2008-09-16  
不错!很好!
2 楼 wangyazhen 2008-05-24  
5)好了,现在你的Struts2和Spring就能正常的一起工作了。有几个配置技术点需要详细说明下:装配模式。你可以通过设置修改struts.properties中下列属性的值来改变装配模式。

-----------------------------------
这个举些例子好吗?没看懂
1 楼 wangyazhen 2008-05-24  
很好 挺详细的  

相关推荐

Global site tag (gtag.js) - Google Analytics