Spring4 整合Hibernate4,Struts2

Posted kika

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring4 整合Hibernate4,Struts2相关的知识,希望对你有一定的参考价值。

 

第一节:S2SH 整合所需Jar 包

 

Struts2.3.16,Spring4.0.6,Hibernate4.3.5 整合所需jar 包;

 

Struts2.3.16 jar 包

 

 

Spring4.0.6 jar 包

 

 

Hibernate4.3.5 jar 包

 

 

第二节:Spring4 整合Hibernate4

  

Spring4 接管Hibernate4 所有Bean 实例,以及SessionFactory,事务管理器;

泛型注入;

   

第三节:Spring4 整合Struts2

 

Spring4 接管Struts2 所有Bean 实例;

 

 

第四节:S2SH 实例测试

 

1.框架搭建

 

index.jsp:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 
11 S2SH!!!!
12 
13 </body>
14 </html>

 

web.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>S2SH</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.jsp</welcome-file>
 6   </welcome-file-list>
 7   
 8   <!-- 添加对spring的支持 -->  
 9   <context-param>  
10     <param-name>contextConfigLocation</param-name>  
11     <param-value>classpath:applicationContext.xml</param-value>  
12   </context-param>  
13     
14     <!-- 定义Spring监听器,加载Spring  -->
15     <listener>  
16         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
17     </listener>  
18       
19   <!-- 添加对struts2的支持 -->  
20   <filter>  
21     <filter-name>struts2</filter-name>  
22     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
23   </filter>   
24   
25   <filter-mapping>  
26     <filter-name>struts2</filter-name>  
27     <url-pattern>/*</url-pattern>  
28   </filter-mapping>  
29   
30   <!-- Session延迟加载到页面  --> 
31    <filter>  
32     <filter-name>openSessionInViewFilter</filter-name>  
33     <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
34     <init-param>  
35       <param-name>singleSession</param-name>  
36       <param-value>true</param-value>  
37     </init-param>  
38   </filter>  
39     
40    <filter-mapping>  
41     <filter-name>openSessionInViewFilter</filter-name>
42     <url-pattern>*.action</url-pattern>
43   </filter-mapping>  
44 </web-app>

 

struts.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <!DOCTYPE struts PUBLIC  
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">  
 5       
 6 <struts>  
 7     <constant name="struts.action.extension" value="action" />  
 8       
 9     <package name="s2sh" namespace="/user" extends="struts-default">
10         <action name="user_*" method="{1}" class="com.wishwzp.action.UserAction">
11             <result name="success">/success.jsp</result>
12             <result name="error">/index.jsp</result>
13         </action>
14     </package>
15       
16 </struts>   

hibernate.cfg.xml:

 1 <?xml version=\'1.0\' encoding=\'UTF-8\'?>  
 2 <!DOCTYPE hibernate-configuration PUBLIC  
 3          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
 4     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
 5   
 6 <hibernate-configuration>  
 7     <session-factory>  
 8         
 9         <!--方言-->
10         <property name="dialect">org.hibernate.dialect.mysql5Dialect</property>  
11 
12         <!-- 显示sql语句 -->  
13         <property name="show_sql">true</property>  
14         
15         <!-- 自动更新 -->
16         <property name="hbm2ddl.auto">update</property>
17   
18     </session-factory>  
19 </hibernate-configuration>

 

applicationContext.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>    
 2 <beans xmlns="http://www.springframework.org/schema/beans"    
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
 4     xmlns:p="http://www.springframework.org/schema/p"  
 5     xmlns:aop="http://www.springframework.org/schema/aop"   
 6     xmlns:context="http://www.springframework.org/schema/context"  
 7     xmlns:jee="http://www.springframework.org/schema/jee"  
 8     xmlns:tx="http://www.springframework.org/schema/tx"  
 9     xsi:schemaLocation="    
10         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
11         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
12         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
13         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
14         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    
15   
16     <!-- 定义数据源 -->
17     <bean id="dataSource"
18         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
19         <property name="driverClassName"
20             value="com.mysql.jdbc.Driver">
21         </property>
22         <property name="url"
23             value="jdbc:mysql://localhost:3306/test">
24         </property>
25         <property name="username" value="root"></property>
26         <property name="password" value="root"></property>
27     </bean>
28       
29     <!-- session工厂 -->  
30     <bean id="sessionFactory"  
31         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
32         <property name="dataSource">  
33             <ref bean="dataSource" />  
34         </property>  
35         <property name="configLocation" value="classpath:hibernate.cfg.xml"/>  
36         <!-- 自动扫描注解方式配置的hibernate类文件 -->  
37         <property name="packagesToScan">  
38             <list>  
39                 <value>com.wishwzp.entity</value>  
40             </list>  
41         </property>  
42     </bean>  
43   
44     <!-- 配置事务管理器 -->  
45     <bean id="transactionManager"  
46         class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
47         <property name="sessionFactory" ref="sessionFactory" />  
48     </bean>  
49   
50     <!-- 配置事务通知属性 -->  
51     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
52         <!-- 定义事务传播属性 -->  
53         <tx:attributes>  
54             <tx:method name="insert*" propagation="REQUIRED" />  
55             <tx:method name="update*" propagation="REQUIRED" />  
56             <tx:method name="edit*" propagation="REQUIRED" />  
57             <tx:method name="save*" propagation="REQUIRED" />  
58             <tx:method name="add*" propagation="REQUIRED" />  
59             <tx:method name="new*" propagation="REQUIRED" />  
60             <tx:method name="set*" propagation="REQUIRED" />  
61             <tx:method name="remove*" propagation="REQUIRED" />  
62             <tx:method name="delete*" propagation="REQUIRED" />  
63             <tx:method name="change*" propagation="REQUIRED" />  
64             <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
65             <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
66             <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
67             <tx:method name="*" propagation="REQUIRED" read-only="true" />  
68         </tx:attributes>  
69     </tx:advice>  
70       
71    
72     <!-- 配置事务切面 -->  
73     <aop:config>  
74         <aop:pointcut id="serviceOperation"  
75             expression="execution(* com.wishwzp.service..*.*(..))" />  
76         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
77     </aop:config>  
78   
79     <!-- 自动加载构建bean -->  
80     <context:component-scan base-package="com.wishwzp" />
81   
82 </beans>  

 

运行结果显示:

S2HH!!!!

 

-----------------------------------------------------------------------------

 

2.简单的登录功能测试:

连接:http://pan.baidu.com/s/1dF1kAyP

密码:80st

 

 

---------------------------

 

以上是关于Spring4 整合Hibernate4,Struts2的主要内容,如果未能解决你的问题,请参考以下文章

SpringMVC4+Spring4+Hibernate4框架整合

spring4和hibernate4.0.0的整合

J2EE开发框架搭建 - springmvc4 + spring4 + hibernate4 整合

Struts2+Spring4.2+Hibernate4.3整合

原创整合Spring4+Hibernate4+Struts2时NullPointerException问题解决

搭建springmvc4 spring4 hibernate4整合框架tomcat用啥版本