前文已经搭建好了struts2环境,而在struts2的基础上集成spring,则需要加入spring的核心jar包已经相应的struts-spring插件以及commons-logging.jar
1.首先导入所需jar包
2.在web-inf下添加spring配置文件applicationContext.xml
3.在web.xml中加入spring组件:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
4.将建好的action交给spring去管理,即在applicationContext.xml添加action的bean配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean name="helloWorldAction" class="ssmdemo.HelloWorld" /> </beans> 5.最后修改struts.xml文件
<!-- 告知Struts 2运行时使用Spring来创建对象 --> <constant name="struts.objectFactory" value="spring" /> <package name="struts2" extends="struts-default"> <action name="HelloWorld" class="helloWorldAction"> <result>/HelloWorld.jsp</result> </action> </package>