SSH2(Struts2、Spring3与Hibernate3)的整合

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测试文件后就可以测试了。具体过程比较简单,这里就不谈了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值