struts2 and spring 整合
预备工作(随机应变,也可以直接拷贝jar到lib下)
1,create a web project
2,导入spring所需jar包
工程右键-->myeclipse-->Add Spring Capabilities-->选择spring 3.0 core Libraries,spring 3.0 AOP Libraries,
spring 3.0 web Libraries-->Finish
3,导入struts2所需jar包
工程右键-->myeclipse-->Add Struts Capabilities-->struts specification 选择Struts2.0或2.1-->Finish
4,导入整合包struts2-spring-plugin-2.1.8.1.jar
配置工作
程序清单:/WebRoot/WEB-INF/web.xml
i,配置监听器(ContextLoaderListener)
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ii,配置contextConfigLocation,struts2对象的创建交给了spring,该参数定位配置bean的文件路径
<context-param>
<param-name>contextConfigLocation</param-name>
//classpath默认路径为src(客户访问服务端,服务端默认的路径为webroot.其他的默认空间均为src)
<param-value>classpath:applicationContext.xml</param-value>
//如果有多个applicationContext-xxx.xml文件,可以使用占位符配置
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
程序清单:src/applicationContext.xml
iii,配置bean,将代理struts创建对象
//beanId将在struts配置action时被引用
<bean id="beanId" class="类全路径">
//如果类中有对象属性需要被实例化,需要进行配置property
<property name="属性名" ref="beanId"></property>
</bean>
程序清单:src/struts.xml
//struts2.1需要指定namespace,namespace需要带"/",jsp中的form也需要带namespace属性
<package name="my" namespace="/ck" extends="strust-default">
<action name="actionName" class="bean的引用"></action>
</package>
整合完成
使用struts2标签需要将web.xml中的struts的filter-mapping配置中的url-pattern更改为/*.