Struts2、Spring与Hibernate是当前比较流行的开源框架,下面介绍一下他们的整合方法:

1、添加Spring、Hibernate、Struts2支持,

首先通过MyEclipse对项目添加Spring支持,添加时选择这些包:

Spring3.0 AOP Libraries

Spring3.0 Core Libraries

Spring3.0 Persistence Core Libraries

Spring3.0Persistence JDBC Libraries

Spring3.0 Web Libraries。

然后添加Hibernate支持,在项目中添加数据库驱动,并引入Hibernate jar包 (hibernate core 和hibernate anotations)

然后添加Struts2的jar包,引入下面这些jar包

struts2-core-2.1.8.1.jar

xwork-core-2.1.6.jar

ognl-2.7.3.jar

freemarker-2.3.15.jar

commons-fileupload-1.2.1.jar

commons-logging-1.0.4.jar

struts2-spring-plugin-2.1.8.1.jar

2、配置web.xml

首先web在.xml文件中配置监听器,用于在系统启动时载入Spring的配置文件

<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

若需要加入spring编码过滤器,则还需在web.xml中加入如下代码:

<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

然后在web.xml文件中配置Spring中OpenSessionInViewFilter过滤器解决hibernate延迟加载的问题

<!-- 解决hibernate延迟加载带来的异常,配置过滤器使Session在请求完成之后再关闭 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

3、配置applicationContext.xml
数据源采用c3p0,

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> <property name="maxPoolSize" value="${c3p0.pool.maxPoolSize}"/> <property name="minPoolSize" value="${c3p0.pool.minPoolSize}"/> <property name="initialPoolSize" value="${c3p0.pool.initialPoolSize}"/> <property name="acquireIncrement" value="${c3p0.pool.acquireIncrement}"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.xiaoyu.pvod.model</value> </list> </property> </bean> <!-- 配置事物管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置advice 事物传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/> <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/> <tx:method name="delete**" propagation="REQUIRED" isolation="DEFAULT"/> <tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/> <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/> <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/> <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/> <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" /> </tx:attributes> </tx:advice> <!-- 事务管理器应用范围 --> <aop:config> <aop:pointcut id="affectMethods" expression="execution(* com.xiaoyu.pvod.dao.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="affectMethods" /> </aop:config> <!-- HibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>

配置jdbc.properties

jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/pvod jdbc.user=root jdbc.password=rootroot c3p0.pool.maxPoolSize=10 c3p0.pool.minPoolSize=2 c3p0.pool.initialPoolSize=3 c3p0.pool.acquireIncrement=2 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true

配置log4j.properties(src/log4j.properties)

log4j.rootLogger=info,A1,B1 #,B1 log4j.appender.A1=org.apache.log4j.RollingFileAppender log4j.appender.A1.File=${catalina.home}/webapps/pvod/logs/pvod.log log4j.appender.A1.MaxFileSize=200KB log4j.appender.A1.MaxBackupIndex=10 log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%5p [%t] (%F:%L) -%d %m%n log4j.appender.B1=org.apache.log4j.ConsoleAppender log4j.appender.B1.layout=org.apache.log4j.PatternLayout log4j.appender.B1.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

4、配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n,encoding" value="UTF-8"/> <constant name="struts.ui.theme" value="simple"/> <package name="com.xiaoyu.pvod.struts" namespace="/" extends="struts-default"> </package> </struts>

配置工作基本就完成了,注意删除项目中得一些重复jar包,删除以asm带头的jar包,如asm-2.2.3.jar,否则可能导致项目启动失败

5、测试

在项目中创建好DAO、Service、Action层的类文件,并在struts.xml和applicationContext.xml中作相应的配置后,写好JSP测试文件后就可以测试了。具体过程比较简单,这里就不谈了。

SSH2(Struts2、Spring3与Hibernate3)的整合相关推荐

  1. Struts2+Spring3.1+Hibernate3.3的整个项目

    经过一天的折腾,终于在MyEclipse2013下搭建出一个Struts2+Spring3.1+Hibernate3.3整合的项目,具体过程如下,仅供新手学习,大神勿喷 首先新建Web项目: 直接fi ...

  2. SSH (Struts2+Spring3.0+Hibernate3)框架(二) 框架的配置

    一.准备工作: 1. JDK -> jdk1.6.0_17 安装(环境变量配置): JAVA_HOME = C:\ jdk1.6.0_17; PATH = %JAVA_HOME%\bin; %J ...

  3. 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境

    做了三年多的JavaEE开发了,在平时的JavaEE开发中,为了能够用最快的速度开发项目,一般都会选择使用Struts2,SpringMVC,Spring,Hibernate,MyBatis这些开源框 ...

  4. Struts2.3.5+Hibernate3+Spring3.1基于注解实现的多文件上传,下载

    Struts2.3.5+Hibernate3+Spring3.1基于注解实现的的多文件上传,下载,这里是上传文件到数据库中,上传控件可以增加和删除,有需要的朋友可以看看. 以下是源码下载地址:http ...

  5. Struts2、Hibernate、Spring整合所需要的jar包

    Struts2.Hibernate.Spring整合所需要的包: Struts2: struts2-core-2.0.14.jar  -- Struts2的核心包. commons-logging-1 ...

  6. struts2+spring3+ibatis2.3+jquery_ajax1.7

    struts2+spring3+ibatis2.3+jquery_ajax1.7合成 实现页面无刷新分页显示数据 演示效果图: [img]http://dl.iteye.com/upload/pict ...

  7. SSH整合教程(struts2+spring3+hibernate3)(含登陆示例)

    准备工作:下载整合过程中需要的包http://115.com/lb/5lbxlk60#lib.rar 115网盘礼包码:5lbxlk60 1.建立web项目 打开MyEclipse,我用的是8.6,项 ...

  8. 【Struts2+Spring3+Hibernate3】SSH框架整合实现CRUD_1.0

    作者: hzboy192@192.com Blog: http://my.csdn.net/peng_hao1988 版本总览:http://blog.csdn.net/peng_hao1988/ar ...

  9. struts2+spring3+hibernate3整合(二)转载

    3. 配置spring3.0.2 结合 hibernate3.3 3.1 导入hibernate3.3的包 在这里,由于我用的是myeclipse8.5,所以我是采用IDE自动导入的方法.我懒.途中的 ...

  10. Eclipse 搭建struts2 spring3 hibernate3环境实战 待完善

    1.struts2 目前是2.3版本,下载地址http://struts.apache.org/download.cgi struts2包 struts2-core-2.3.16.3.jar stru ...

最新文章

  1. 华为交换机ACL配置
  2. 巨头间未来战争-丰收节交易会·万祥军:农业AI发展概况
  3. handsome对应php文件,handsome主题魔改教程
  4. 我只是一只碌碌无为的工蚁 : (
  5. C++的new、delete需要注意的一点:使用危险函数导致的越界CRT detected that the application wrote to memory after end of heap
  6. 【CodeForces - 985D】Sand Fortress (二分,贪心,思维构造,技巧,有坑)
  7. 树莓派Java程序运行_树莓派上Java程序作为linux服务并开机自动启动
  8. 如何解决使用mac聚焦搜索无法搜索软件的情况
  9. 比特币价格疯涨!特斯拉或将支持比特币付款
  10. 【Elasticsearch】使用Elasticsearch中的copy_to来提高搜索效率
  11. mongoDB's Optimization example
  12. Static 单例模式
  13. 【Django 2021年最新版教程27】数据库model 查询2个日期范围内的所有日期
  14. move motorName by|to value units
  15. shark恒破解笔记5-VB之rtcMsgBox
  16. 使用vue模拟通讯录列表,对中文名拼音首字母提取并排序
  17. 安卓flash插件_谷歌Chrome 76稳定版正式发布:默认禁用Flash
  18. 小米2017校园招聘笔试题
  19. 深入浅出讲解FOC控制与SVPWM技术
  20. 前端学习随笔 css篇

热门文章

  1. PID控制器的输入量和输出量的物理关系解释
  2. 【Android】Instant Run原理解析
  3. android 应用引导用户去应用市场评论
  4. Andorid检测支付宝客户端是否安装
  5. 浅论汽车电子行业的汽车开放系统架构AUTOSAR
  6. 途家民宿VS木鸟民宿:民宿APP功能体验
  7. pythonurllib新浪微博_定向爬虫 - Python模拟新浪微博登录(示例代码)
  8. c++中char[]与char*的转换以及char*与数字互转
  9. 中文和英文论文哪个比较容易发表
  10. 东北旅行第一天流水账