首先第一步:
导入jar包:
我的做法:
导入你的基本使用的spring的jar包
和基本使用的struts2的jar包
然后struts2中有一个和spring整合的jar包一定要导入,不然会抛异常。包名是这个:struts2-spring-plugin-2.3.30.jar
在web.xml中装载spring容器:
<!-- 装载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在web.xml中装载struts2:
<!-- 装载struts2框架 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然后在struts2的配置文件中加入常量声明action交给spring管理:
<!-- 将action交给spring管理 -->
<constant name="struts.objectFactory" value="spring"/>
当action交给spring管理之后,我们在action中的class属性可以写成spring容器中的bean的标识:
<package name="person" namespace="/person" extends="struts-default"> <!-- 这个地方的class属性变成了spring中的bean的标识 --> <action name="person_*" class="personAction" method="{1}"> <result name="message" type="redirectAction"> <param name="actionName">person_findAll</param> <param name="namespace">/person</param> </result> <result name="list">/person/personList.jsp</result> </action> </package>
其他地方正常开发。
此例子为ssh2整合的。
补充代码:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:component-scan base-package="cn.itcast"/> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/> <property name="minPoolSize" value="${jdbc.minPoolSize}"/> <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/> <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/> <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/> </bean> <!-- 定义sessionFactory对象 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>cn/itcast/domain/Person.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true </value> </property> </bean> <!-- 注册事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 注解管理事务 --> <tx:annotation-driven transaction-manager="txManager"/> </beans>