Spring Struts2集成步骤


Struts 2框架整合Spring很简单,下面是整合的步骤。
1.复制文件 。复制struts2-spring-plugin-x-x-x.jar和spring.jar到WEB-INF/lib目录下。其中的x对应了Spring的版本号。还需要复制commons-logging.jar文件到WEB-INF/lib目录下。
2.配置struts.objectFactory属性值 。在struts.properties中设置struts.objectFactory属性值:
struts.objectFactory = spring
或者在XML文件中进行常量配置:
<struts>
       <constant name="struts.objectFactory" value="spring" />
       ...
</struts>
3.配置Spring监听器 。在web.xml文件中增加如下内容:
<listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4.Spring配置文件 。默认情况下,Spring配置文件为applicationContext.xml,该文件需要保存在Web应用的WEB-INF目录下。内容示例如下所示:
<?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>
5.修改Struts配置文件 。Struts 2框架整合Spring框架,需要在Struts配置文件中有所改变,下面是一个示例:
<!DOCTYPE struts PUBLIC
       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
       " www.2cto.com">
<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>
该配置文件中定义了两个Action配置:foo是一个标准的Struts 2框架Action配置,指定了Action实现类为com.acme.Foo;bar对应的class并不存在,那么框架将在Spring配置文件中查找id属性为“bar”的定义 ,该配置文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
       "-//SPRING//DTD BEAN//EN"
       " www.2cto.com">
<beans default-autowire="byName">
       <bean id="bar" class="com.my.BarClass" singleton="false"/>
<!--1.关联到struts中,若没有可以用struts-plugin.xml( truts2-spring-plugin-x-x-x.jar中定义) 来给struts处理-->
       ...
</beans>
作者:huang798807481
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值