Spring+Struts2 整合

首先回顾一下Struts2的工作流程大概是这样的:

当Struts2和Spring整合的时候, 其实就是把Struts2的Action交由Spring来控制管理;

首先在struts.xml配置文件中加入:

  <constant name="struts.i18n.encoding" value="UTF-8"/>
  <constant name="struts.objectFactory" value="spring"/>

在struts.xml中指定objectFactory对象工厂为spring
即action的创建交由spring进行

然后在Spring的配置文件applicationContext.xml中配置好要用的Action:

<bean name="productActionBean" class="Action.ProductAction" />

要注意这个文件要放在WEB-INF文件下

然后是要在web.xml中配置Context监听器:

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

这个监听器的作用是:

Spring提供ServletContentListener的一个实现类ContextLoaderListener监听器,该类可以作为Listener使用,在启动Tomcat容器的时候,该类的作用就是自动装载ApplicationContext的配置信息,如果没有设置contextConfigLocation的初始参数则会使用默认参数WEB-INF路径下的application.xml文件。如果需要自定义读取多个配置文件或者修改默认路径,则可以在web.xml
中设置:

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
</context-param>

这里需要导入一个包struts2-spring-plugin-2.2.3.1.jar
使用idea的话可以直接选择在maven下载~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值