spring2结合struts2的配置

1. 前置条件
引入struts2-spring-plugin-2.0.9.jar
2. web文件中引入spring配置文件装载的监听器
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果spring不是默认文件或者不在默认位置(web-inf/classes/),需要设置装载器的参数(这里可以使用通配符*)
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-context/applicationContext.xml</param-value>
</context-param>
3. 在spring中配置struts配置中用到的aciton(像普通bean那样)
<bean id="bookAction" class="com.sterning.books.web.actions.BooksAction">
    <property name="booksService">
<ref bean="booksService"/>
    </property>
</bean>
4. 在struts中使用spring配置的bean id作为action(class使用bean id而不是具体类)
<package name="products" extends="struts-default">
    <action name="list" class="bookAction" method="list">
      <result name="success">/list.jsp</result>
      <result name="error">/error.jsp</result>
    </action>
    <action name="delete" class="bookAction" method="delete">
      <result type="redirect" name="success">list.action</result>
      <result type="redirect" name="error">/error.jsp</result>
    </action>
</package>
5. 对于spring配置的简化
可以设置beans的default-autowire="byName",让spring帮你建立bean之间的依赖关系,那就不需要在定义bean的时候写<property>标签来显式定义依赖关系,只需要在bean类中增加依赖的bean的set方法即可。当然,如果这样设置的话,会使得配置文件难于理解,复杂的应用还是在配置文件中声明bean的依赖关系比较好。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值