struts2.0.14+hibernate3.2+spring2.5.6+tomcat6.0+mysql5.0整合全过程

这里为什么使用struts2.0.14这个版本呢??请看官网的文章如下:

24 November 2008 - Struts 2.0.14 General Availability Release

The latest release of Struts 2 is Struts 2.0.14, which was promoted to "General Availability" on 24 November 2008.

For changes included in Struts 2.0.14, see the release notes. Struts 2.0.14 is a bugfix release for version 2.0.12, which provided important security fixes. Struts 2.0.14 addresses two major bugs introduced with these security fixes, keeping the FilterDispatcher from serving built in static resources, and keeping conversion error messages from being displayed. All developers are strongly encouraged to update existing Struts 2 applications to Struts 2.0.14.

如果你的英语不差的话,自己去看下吧.这段英语的大概意思是,Struts 2.0.14修正了Struts 2.0.12 的两个主要的bug ;并且建议开发者更新到Struts 2.0.14
这个版本.Struts 2.0.14 General Availability Release 这是个通用的开发版本.
其实我在整合这个S2SH时,struts2 的最新版本是struts2.1.16 这个也是稳家版本,不过最新的版本网上资料很少.所以...........! 如果自己学的话可以选择这个两个版本.
下边是整合的全过程:
主要是jar包的引入,我个人比较不喜欢用IDE 工具自动引入jar包,所以下边是自己用的jar的引入,这样可以省去相关jar 的冲突;

1:首先struts2.0.14所需要的jar包如下:
commons-logging-1.0.4  ;
freemarker-2.3.8
 ognl-2.6.11
struts2-core-2.0.14
xwork-2.0.7
struts2-spring-plugin-2.0.14  这是整合spring 必需的jar 包,从它的名字就可以看出来了!   

2:hiberante3.2 jar 包如下 这是我的工程必需的,如果你的工程要用到其它的jar包可以去看一看hibernate 的必读文件那里有很详细的解释;
            这个是做一般的工程,引入最少的jar 了,一般的项目都可以了!

antlr-2.7.6         cglib-2.1.3     commons-collections-2.1.1    commons-logging-1.0.4      dom4j-1.6.1  
ehcache-1.2.3    hibernate3     jaas        jaxen-1.1-beta-7     jdbc2_0-stdext     jta    log4j-1.2.11  
xml-apis             asm             asm-attrs 

如果你自己不做整合的,就是单独做hibernate 学习,本人建议你把所有jar 都导入项目的lib目录下!这里不在做任何解释了!

3:  spring 2.5.6  相关的jar 也是做项目最少引入的jar包了,如果自己还需要特定的自己去引入;

    aspectjrt    aspectjweaver   ( 这两个是AOP 相关的jar包,因为spring  2.5的AOP实现,没有自己去做,也用到了第三方组件,分开了,这好像和2.0版本有出入!有关细节,请自己去查阅相关文档)
    spring

好的,这三个框架所有jar 包,都介绍完了,(不要奇怪我的所有jar 包没有后缀名,是我自己给隐藏了)

好的进入主题了(顺序最好 hibernate --->spring---->struts)

1:把相关的hibernate  jar 包导入项目lib 目录下!
2: 把spring  相关jar   包导入项目lib 目录下!
3: 把struts2相关jar   包导入项目lib 目录下!

三者整合:
web.xml文件如下里边都有解释自己看吧:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <!-- 配置spring监听器和上下文变量 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext-*.xml</param-value>
 </context-param>

 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 <!-- spring end -->

<!-- 配置字符过滤器-->
 <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>
   org.springframework.web.filter.CharacterEncodingFilter
  </filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>GBK</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <!--让spring来过滤管理Hiberante中的session-->
 <filter>
  <filter-name>lazyLoadingFilter</filter-name>
  <filter-class>
   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
  </filter-class>
 </filter>

 <!-- spring end -->

 <!-- 增加struts2所需要的过滤器,以便在服务器启动时,拦截URL请求 -->
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>

 <filter-mapping>
  <filter-name>lazyLoadingFilter</filter-name>
  <url-pattern>*.action</url-pattern>
 </filter-mapping>


 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
  </web-app>


applicationContext-common.xml 文件:其中我使用了,DBCP数据库连接波,如果你自己想用,
                           还的去下载相关的jar 包( commons-dbcp-1.2.2   commons-pool-1.3)记着引入哦!

我下边注释掉的就是没有用数据库连接池的,也就是不把src目录下的hibernate.cfg.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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
 <!-- 用DBCP数据库连接波来配置hibernate数据库连接 -->
 
  <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
  value="com.mysql.jdbc.Driver">
  </property>
  <property name="url" value="jdbc:mysql://localhost/oa"></property>
  <property name="username" value="root"></property>
  <property name="password" value="root"></property>
  </bean>
 
 <!-- 配置sessionFactory -->
  
  <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>
  <prop key="format_sql">true</prop>
  <prop key="hibernate.hbm2ddl.auto">update</prop>
  </props>
  </property>
  <property name="mappingResources">
  <list>
  <value>com/oa/model/Person.hbm.xml</value>
  <value>com/oa/model/Organization.hbm.xml</value>
  </list>
  </property>
  </bean>
  
<!-- 让spring管理sessionFactory的加一种方法 ,就是不删除hibernate.cfg.xml文件;
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation">
   <value>classpath:hibernate.cfg.xml</value>
  </property>
 </bean>
--> 
  
 <!-- 配置hibernate事物让spring来管理 -->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>


 <!-- 配置事物的传播特性 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="add*" propagation="REQUIRED" />
   <tx:method name="delete*" propagation="REQUIRED" />
   <tx:method name="update*" propagation="REQUIRED" />
   <tx:method name="*" read-only="true" />
  </tx:attributes>
 </tx:advice>
 <!-- 那些类的那些方法需要事物; -->
 <aop:config>
  <aop:pointcut id="allManagerMethod"
   expression="execution(* com.oa.manager.*.*(..))" />
  <aop:advisor pointcut-ref="allManagerMethod"
   advice-ref="txAdvice" />
 </aop:config>
</beans>

对了还需要注意的一个地方就是如果用了,我这种数据库连接池技术,得把你的数据库驱动类,复制tomcat lib 目录下.不然会报错的!

好了三个东西整合好,我也是费了九牛二虎之力才整合成功的.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值