码迷,mamicode.com
首页 > 编程语言 > 详细

关于Spring和Struts2的整合

时间:2018-01-02 21:15:02      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:XML   config   dia   for   pass   conf   tran   cto   work   

需要导入包

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类中的属性需自己注入。

关于Spring和Struts2的整合

标签:XML   config   dia   for   pass   conf   tran   cto   work   

原文地址:https://www.cnblogs.com/zhengsonglin/p/8178773.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!