J2EE系列之Spring4学习笔记(十三)--Spring4整合Struts2和Hibernate4
来源:互联网 发布:sql数据库文件怎么打开 编辑:程序博客网 时间:2024/05/17 15:59
现在使用Spring4来整合Struts2和Hibernate4.
Struts2.3.16,Spring4.0.6,Hibernate4.3.5 整合所需jar 包:
Struts2.3.6所需jar包:
Spring4.0.6所需jar包:
Hibernate4.3.5所需jar包:
本地安装的是mysql数据库,还要添加mysql驱动包:mysql-connector-java-3.1.12-bin.jar
1.新建dynamic web project工程:S2SH
2.把所需的jar包粘贴到lib目录下:
3.配置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>
这里特别说一下有一个session延迟加载到页面配置,如果不加这个配置的话,当在后台取关联数据的时候session被关闭掉,页面是取不到级联的数据的。当加了openSessionInViewFilter,session就能延迟加载到页面。下面的<url-pattern>*.action</url-pattern>表示对所有的*.action请求进行延迟加载,也就是对所有的struts2请求延迟加载。
4.配置struts2,src文件夹下新建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" /> </struts>
这里表示struts的请求后缀使用.action
5.src下新建hibernate.cfg.xml文件配置hibernate4:
<?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>
这里的这些配置在讲hibernate的时候讲过。
6.可以看到上面的struts2,hibernate4的配置文件比单独讲这两个技术的时候要省了很多东西,这是因为省掉的东西要做spring配置文件中配置,spring接管了struts2和hibernate4的一些东西。在src目录下新建文件applicationContext.xml来配置spring:
<?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="123456"></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.test.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.test.service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> </aop:config> <!-- 自动加载构建bean --> <context:component-scan base-package="com.test" /> </beans>
这里要强调的有一下几点:
1)里面使用自动扫描注解方式配置的hibernate类文件,这里使用packagesToScan属性的话会把com.test.entity包下的所有类,这样的话就节省了我们一个一个的配置。
2)自动加载构建bean:后面全部使用注解的方式加载类,所以配置一下自动加载构建bean,把包com.test下的类都进行扫描。
6.建包层次,这里比传统的多了事务层:建立的包层次如下:
7.新建一个index.jps页面测试配置效果:
<%@ 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>
8.把工程加入到tomcat服务器,运行程序,发现程序没有报错。打开浏览器:
程序运行成功,这说明Spring整合struts2和hibernate4成功。
- J2EE系列之Spring4学习笔记(十三)--Spring4整合Struts2和Hibernate4
- J2EE系列之Spring4学习笔记(十四)--Spring4整合Struts2和Hibernate4
- Spring4学习笔记-Spring4整合Hibernate4
- Struts2+Hibernate4+Spring4整合
- spring4+hibernate4+struts2整合
- Spring4 整合Hibernate4,Struts2
- Struts2+Spring4+Hibernate4整合系列--(一) 概述
- Struts2+Spring4+Hibernate4整合系列--(二) 原理讲解
- Struts2+Spring4+Hibernate4整合系列--(三) 环境准备
- Struts2+Spring4+Hibernate4整合系列--(四)StepByStep1
- Struts2+Spring4+Hibernate4整合系列--(四)StepByStep2
- Struts2+Spring4+Hibernate4整合系列--(四)StepByStep3
- Struts2+Spring4+Hibernate4整合系列--(四)StepByStep4
- Struts2+Spring4+Hibernate4整合系列--(四)StepByStep5
- Struts2+Spring4+Hibernate4整合系列--(五)后续思考
- 整合Struts2、Hibernate4.3和Spring4.2
- J2EE系列之Spring4学习笔记(一)--Spring介绍
- struts2、spring4、hibernate4(ssh)整合案例
- [LeetCode] Largest Divisible Subset
- Golang实现的时间轮
- uva839 Not so Mobile 递归
- 从数据库取出的Date类型的数据放入Map<String,Object>之前需要做的处理
- Spring提供RestTemplate用于访问Rest服务的客户端实践
- J2EE系列之Spring4学习笔记(十三)--Spring4整合Struts2和Hibernate4
- iOS 11 发布!巨大进步、里程碑式飞跃!
- 探索ReactNative应用
- 矩阵伪逆介绍及C++/OpenCV/Eigen的三种实现
- 微信小程序----实例源码大全下载
- MYSQL如何进行sql like (sql查询结果)的查询
- 亚洲诚信首推MySSL.com,获WHD2017年最佳云安全奖
- java + 线程系列之一些定义(小结)
- 搜索网址整理