(六)Spring4 整合Hibernate4,Struts2
2024-08-24 19:35:51
第一节: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:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> S2SH!!!! </body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>S2SH</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- 添加对spring的支持 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 定义Spring监听器,加载Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 添加对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> <!-- Session延迟加载到页面 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<constant name="struts.action.extension" value="action" /> <package name="s2sh" namespace="/user" extends="struts-default">
<action name="user_*" method="{1}" class="com.wishwzp.action.UserAction">
<result name="success">/success.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package> </struts>
hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory> <!--方言-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 显示sql语句 -->
<property name="show_sql">true</property> <!-- 自动更新 -->
<property name="hbm2ddl.auto">update</property> </session-factory>
</hibernate-configuration>
applicationContext.xml:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 定义数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/test">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean> <!-- session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<!-- 自动扫描注解方式配置的hibernate类文件 -->
<property name="packagesToScan">
<list>
<value>com.wishwzp.entity</value>
</list>
</property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 配置事务通知属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义事务传播属性 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="new*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice> <!-- 配置事务切面 -->
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* com.wishwzp.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config> <!-- 自动加载构建bean -->
<context:component-scan base-package="com.wishwzp" /> </beans>
运行结果显示:
S2HH!!!!
-----------------------------------------------------------------------------
2.简单的登录功能测试:
连接:http://pan.baidu.com/s/1dF1kAyP
密码:80st
---------------------------
最新文章
- CSS3径向渐变----大鱼吃小鱼之孤单的大鱼
- flex的用途
- 安装PhantomJS
- Java在ACM中的应用
- WordPress折腾日记
- HTML5的自定义属性data-* 的用法解析
- ZOJ 1151 Word Reversal
- OpenCV例程实现人脸检测
- 浏览器的重绘repaints与重排reflows深入分析
- DapperLambda发布
- CSU 1812 三角形和矩形
- iOS项目架构 小谈
- 关于hashmap的理解
- HDU 2063 过山车(模板—— 二分图最大匹配问题)
- EventBus InMemory 的实践基于eShopOnContainers (二)
- 2018年东北农业大学春季校赛 I-wyh的物品(二分查找)
- 阿里OSS存储,php版demo
- selenium配置文件定位元素
- springboot-24-restTemplate的使用
- zookeeper+kafka集群的安装部署