需要导入包
struts2-spring-plugin-2.3.24.jar
Spring配置文件
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:aop="http://www.springframework.org/schema/aop" 3 xmlns:tx="http://www.springframework.org/schema/tx" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 8 http://www.springframework.org/schema/tx 9 http://www.springframework.org/schema/tx/spring-tx.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context.xsd"> 14 15 <context:annotation-config/> 16 <context:component-scan base-package="com.zsl"/> 17 <tx:annotation-driven transaction-manager="transactionManager"/> 18 19 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 20 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 21 <property name="url" value="jdbc:mysql://localhost:3306/SSH"></property> 22 <property name="username" value="root"></property> 23 <property name="password" value="root"></property> 24 </bean> 25 26 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 27 <property name="dataSource" ref="dataSource"></property> 28 <property name="packagesToScan" value="com.zsl.model"> 29 </property> 30 <property name="hibernateProperties"> 31 <props> 32 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> 33 <prop key="hibernate.hbm2ddl.auto">update</prop> 34 <prop key="hibernate.show_sql">true</prop> 35 <prop key="hibernate.format_sql">true</prop> 36 <prop key="current_session_context_class">thread</prop> 37 <prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop> 38 </props> 39 </property> 40 </bean> 41 42 <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 43 <property name="sessionFactory" ref="sessionFactory"></property> 44 </bean>
web配置文件
1 <listener> 2 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 3 </listener> 4 5 <context-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:bean1.xml</param-value> 8 </context-param> 9 10 11 <filter> 12 <filter-name>struts2</filter-name> 13 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 14 </filter> 15 16 <filter-mapping> 17 <filter-name>struts2</filter-name> 18 <url-pattern>/*</url-pattern> 19 </filter-mapping>
当Struts2的action交由Spring管理的时候,不需要注入,action会自己讲有set方法的属性按名字查找从Spring的配置文件中找到相应的ID自己注入。
或者将Struts2的xml文档里的action的class参数改为Spring配置中依赖注入的ID,则action类中的属性需自己注入。