首先回顾一下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下载~