eclipse整合Struts2+Spring 2.5+Hibernate3.2
来源:互联网 发布:软件开发 成都 编辑:程序博客网 时间:2024/05/16 09:49
首先,把SSH框架必需的的JAR包,添加到Web项目中去
Struts2需要的包:
struts2-core-2.x.x.jar : Struts 2框架的核心类库
xwork-2.x.x.jar :XWork类库,Struts 2在其上构建
freemarker-2.3.x.jar :Struts 2的UI标签的模板使用FreeMarker编写
ognl-2.6.x.jar :对象图导航语言(Object Graph Navigation Language),struts2框架通过其读写对象的属性
commons-fileupload-1.2.x.jar 文件上传组件,2.1.6版本后需要加入此文件
struts2-spring-plugin-2.x.x.jar :用于struts2集成Spring的插件
commons-io-1.3.x.jar:公共输入输出包 ,不添加eclipse可能会有异常
Commons-logging-1.0.x.jar:日志包,不添加eclipse可能会有异常
Spring 2.5需要的包:
Hibernate3.2需要的包:
这两个还没搞清楚,索性都加进去
然后,
一、Spring+Struts2整合:
1、spring配置在web.xml文件中的上下文监听器:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframwork.web.content.ContextLoaderListener</listener-class>
</listener>
2、struts2配置在web.xml文件中的过滤器:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-patter>/*</url-patter>
</filter-mapping>
3、添加struts.xml文件,然后在struts.xml 文件中配置每个action时,它的class就不是一个类了,而是在applicationContext.xml文件中定义过的类的ID
然后特别要注意的一个问题:action是一个请求就是一个action对象,而在spring中则不是的,它是自动分配类的实例的,是使用的单态模式来生产类的实例的,不符合action,因此在applicationContext.xml文件中定义每个bean时,都要在类后加上:
scope=“prototype” 属性
二、Spring+Hibernate整合:
Spring整合Hibernate,是做了一个很大的调整的,因为spring可以把管理Hibernate的工作都做了,以前的hibernate.cfg.xml文件都去掉了,而将这些内容都交给了spring来管理了。
1、 applicationContext.xml文件中应该配置如下内容:
Java代码
//配置数据连接类
<bean id=“dataSource” lass=“org.springframework.jdbc.datasource.DriverManagerDataSource”>
<property name=“driverClassName” value=“org.gjt.mm.mysql.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工厂类
<!-- annotation.AnnotationSessionFactoryBean 使用标注 进行映射 -->
<bean id=“sessionFactory” class=“org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
<property name=“dataSource”>
<ref bean=“dataSource” />
</property>
<property name=“hibernateProperties”>
<props>
<prop key=“hibernate.dialect”>
org.hibernate.dialect.MySQLDialect
</prop>
<prop key=“hibernate.show_sql”>true</prop>
</props>
</property>
<property name=“mappingResources”>
<value>com/hejianjiao/vo/Person.hbm.xml</value>
</property>
</bean>
2、可以使用spring中的HibernateDAOSupport与HibernateTemplate类来进行数据持久化操作:
A、HibernateDAOSupport类中定义了对session、sessionFactory的操作方法与getHibernateTemplate方法来获得一个HibernateTemplate实例;
B、HibernateTemplate类中定义了对数据持久化的各种封装的方法,我们可以用它来对数据进行操作。
因此在使用时,我们可以继承HibernateDAOSupport类,然后实例化HibernateTemplate类来进行数据持久化。
spring事务处理
1、事务的处理也交给了spring来管理,要在applicationContext.xml文件中上配置事务管理类:
Java代码
//实施事务管理的bean
<bean id=”transactionManager”
class=”org.springframwork.orm.hibernate3.HibernateTransactionManager”>
<property name=”sessionFactory”>
<ref bean=”sessionFactory” />
</property>
</bean>
它是通过sessionFactory来管理,因此在传进来一个sessionFactory来接管事务处理。
2、 声明式事务处理:
在spring中对事务进行管理时,可以显示地进行事务处理的定义:
//给事务添加的属性
Java代码
<tx:advice id=”txAdvice” transaction-manager=”transactionManager”>
<tx:attributes >
//propagation表示的是事务的传播特性,使用required时,是当检测到add开头的方法时,就看此时有没有开启的事务,如果有则将方法放进事务中去,如果没有,则新建一个事务。然后将方法放进去。
Java代码
<tx:method name=”add*” propagation=”REQUIRED”>
<tx:method name=”delete*” propagation=”REQUIRED”>
<tx:method name=”update*” propagation=”REQUIRED”>
//如果检测到其它的方法,则给其只读数据库的属性。即当本方法在读时,其它的方法不能再去写了。保证一个事务的完整性。
Java代码
<tx:method name=”*” read-only=”true”>
</tx:attributes>
</tx:advice>
对于事务的其它传播属性,则可以参考其它文档进行相关的了解。
上一个配置是针对于所有包中类的事务处理方法的设置。下面一段是<aop:config/> 的定义,它确保由 ‘txAdvice’ bean定义的事务通知在应用中合适的点被执行。首先我们定义了 一个切面,它匹配 HibernateDAO 接口定义的所有操作,我们把该切面叫做 ‘allManagerMethod’。然后我们用一个通知器(advisor)把这个切面与 ‘txAdvice’ 绑定在一起,表示当 ‘allManagerMethod’ 执行时,’txAdvice’ 定义的通知事务逻辑将被执行。这就是AOP切面工程:
Java代码
<aop:config>
<aop:pointcut id=”allManagerMethod”
expression=”execution(* com.hejianjiao.hibernate.HibernateDAO.*(..))”/>
//调用上面配置的事务属性,可以将它给本aop pointcut。
<aop:advisor advice-ref=”txAdvice” pointcut-ref=”allManagerMethod”/>
//如果还有其它的定义,则可以再加上pointcut、advisor来定义本切面点的事务逻辑。
</aop:config>
</aop:config>//expression中的内容是要执行本切面的一个接口,中的所有方法:如:一个接口中定义了操作数据的方 法:com.hejianjiao.hibernate.HibernateDAO,则下面execution括号中的内容就为:* com.hejianjiao.hibernate.HibernateDAO.*(..)。而如果在com.hejianjiao.hibernate 包中还有其它的类也有操作方法,我们要一起定义的话,就可以写为:* com.hejianjiao.*.*(..),其中(..)表示的是方法,前面的第一个*是操作的接口或者类。
上面的配置将为由 ‘HibernateDAO’ 定义的bean创建一个代理对象,这个代理对象被装配了事务通知,所以当它的相应方法被调用时,一个事务将被启动、挂起、被标记为只读,或者其它(根据该方法所配置的事务语义)。
在组合开发中,常见的一个问题就是session的管理,当我们使用HibernateTemplate操作数据库时,可以不对session进行显示的操作,spring可以自动处理session的打开与关闭。
我们可以在web.xml文件中显示的配置一个session管理的过滤器,它专门帮助我们关闭session:
Java代码
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>
org.springframwork.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
注:它一定要在struts2的过滤器之前。因为web.xml文件的过滤器执行是有顺序的。而session一定在前面进行。
以下是项目中详细的配置文件:
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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName"> <!-- default-autowire="byName" 使用类名进行自动注入 -->
<!-- 属性文件读入 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:config/jdbc/jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 dataSource -->
<bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- <property name="initialPoolSize" value="10"/>
<property name="minPoolSize" value="10"/>
<property name="maxPoolSize" value="150"/>
<property name="maxIdleTime" value="25000"/>
<property name="acquireIncrement" value="5" />
<property name="idleConnectionTestPeriod" value="1800"/>
<property name="checkoutTimeout" value="2000"/> -->
</bean>
<!-- 配置Session工厂 -->
<!-- annotation.AnnotationSessionFactoryBean 使用标注 进行映射 -->
<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">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan" value="com.cn.ciecc.search.model" />
</bean>
<!-- Hibernate TransactionManager 配置Hibernate的事务管理Bean -->
<bean id = "transactionManager" class = "org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref bean = "sessionFactory" /></property>
</bean>
<!-- 支持 @Transactional 标记 -->
<tx:annotation-driven/><!-- 支持 @AspectJ 标记-->
<aop:aspectj-autoproxy/>
<!-- 以AspectJ方式 定义 AOP -->
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.cn.ciecc.search.service..*Service.*(..))" advice-ref="txAdvice"/>
<aop:advisor pointcut="execution(* org.springside.core.dao.*Dao.*(..))" advice-ref="txAdvice"/>
</aop:config>
<!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置.默认的设置请参考Spring文档事务一章. -->
<tx:advice id = "txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 启用类扫描机制以启用注释驱动,使用annotation自动注册bean ,并检查@AutoWired @Required 的属性已注入 -->
<context:component-scan base-package="com.cn.ciecc.search" />
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>HibernateSearch</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!--Hibernate Open Session in View Filter-->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- filter形式过滤延迟加载 -->
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- Struts Configration -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring Configration -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
- eclipse整合Struts2+Spring 2.5+Hibernate3.2
- Struts2+Hibernate3.2+Spring 2.0整合应用
- 【Spring】Spring+Struts2+Hibernate3整合(十七)
- Struts2+Hibernate3.2+Spring 2.0整合应用配置
- SSH整合配置 (Spring 2.5 + Hibernate3.2)
- spring+hibernate3+struts2整合完整配置
- spring+hibernate3+struts2整合完整配置
- struts2+spring+hibernate3入门整合Demo
- Spring2.5.3+Hibernate3.2+Struts2.0.11整合
- 整合Spring2.5+Struts2.1+Hibernate3.2
- Struts2.1.6 + Spring2.5+Hibernate3.2整合
- Struts2+Hibernate3.2+Sprint2.5整合
- Struts2.2+Spring3.0+Hibernate3.6整合
- struts2+hibernate3.2+spring2.0整合篇
- struts2 spring3 hibernate3整合
- Struts2+Spring2+Hibernate3整合
- Struts2+Spring3+Hibernate3整合
- Struts2+Hibernate3+Spring3 整合
- 文字跑马灯HTML代码解析
- 第十二章 4 文件操作
- struts2.0中struts.xml配置文件详解
- 再一篇深拷贝、浅拷贝
- C#调用迅雷的时候 自动模拟点击”下载按钮” 关闭弹出窗口
- eclipse整合Struts2+Spring 2.5+Hibernate3.2
- 隐蔽的内存泄漏——pthread_create 属性设置不当导致
- 教出一个优秀的女儿
- c++中的overload(重载)、override(覆盖)和隐藏
- 读取/输出 CSV文件
- iPhone应用程序使用的设计模式
- 几种常用的显示程序进度的小方法
- C字符串和标准的C++string类
- Android2.2 sdk开发环境和模拟器 配置图解版 (详解)