花了一下午,终于将struts2.18+hibernate3.5.6+spring2.5.5进行整合,完成功能:在Oracle数据库的TCourse表中插入一条数据。

  Ⅰ,整合流程:web启动————》spring与struts2启动————》hibernate启动

  一,web.xml文件中配置spring与struts2;

<?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">
<display-name></display-name> <!--spring容器加载 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!--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> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

说明:
  1,spring容器加载:

  spring容器是以监听器的形式整合到web服务器中,伴随服务器的启动,容器也启动并创建相应的bean,其中:单例的bean会被创建(如:CourseDaoImpl,CourseServiceImpl等),多例的不会创建(如:struts2的action——CourseAction).

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

  这段代码表示加载spring的配置文件。

  2,struts2加载:

  struts2的加载是通过过滤器实现的,过滤器原理,没啥好说的。

  

  二,struts2与spring的集合:

  1,引入一个struts2提供的包:struts2-spring-plugin-2.1.8.1.jar。

  2,原理: 如果包解压开会看见一个 “struts-plugin.xml”的文件,如下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" /> <!-- Make the Spring object factory the automatic default -->
<constant name="struts.objectFactory" value="spring" /> <constant name="struts.class.reloading.watchList" value="" />
<constant name="struts.class.reloading.acceptClasses" value="" />
<constant name="struts.class.reloading.reloadConfig" value="false" /> <package name="spring-default">
<interceptors>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
<interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
</interceptors>
</package>
</struts>

说明:只需解释两行关键代码:

<!--    创建一个对象工厂,名字为spring(其class类型可能是spring的工厂的实现,或者将spring注入其中)-->

<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
 <!--  当需要对象的时候,从上面名为spring的工厂中去找-->
 <constant name="struts.objectFactory" value="spring" />

   三,spring与hibernate的整合:通过spring配置文件---->得到hibernate的SessionFactory---->得到HibernateTemplate

  1,spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!--dataSource -->
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="***}" />
<property name="url" value="***" />
<property name="username" value="***" />
<property name="password" value="***" />
</bean> <!--sessionFactory(猜想:spring在初始化时候将hibernate的session工厂注入,因为spring的类并非继承hibernate的) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
    <!--关键代码:加载hibernate配置文件-->
<property name="configLocations"><value>classpath:hibernate.cfg.xml</value></property>
</bean> <!--hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- ======hibernate配置完毕,得到sessionFactory====分割线======= --> <bean id="courseDao" class="org.ssh.dao.impl.CourseDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean> <bean id="courseService" class="org.ssh.service.impl.CourseServiceImpl">
<property name="courseDao" ref="courseDao"></property>
</bean> <bean id="courseAction" class="org.ssh.action.CourseAction" scope="prototype">
<property name="courseService" ref="courseService"></property>
</bean>
</beans>

  说明:

  1,1  dataSoure对象----》取得数据库连接;

dataSoure对象的类型BasicDataSource是Java.sql.DataSource接口的实现,实现此接口就可以得到一个数据库连接。

  1,2 sessionFactory对象----》取得hibernate的sessionFactory;

类型为:org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean  表示支持annotation,如果没有annotation可以用:

org.springframework.orm.hibernate3.LocalSessionFactoryBean

  即:<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  1,3 <property name="configLocations"><value>classpath:hibernate.cfg.xml</value></property>---->加载hibernate的配置文件

四,项目文件结构

Ⅱ,常见问题

  1,java.lang.NoClassDefFoundError: javax/persistence/EntityListeners

  在使用Hibernate3的时候,发现程序编译好了,在运行时总是抛出java.lang.NoClassDefFoundError: javax/persistence/EntityListeners异常,经查找是因为缺少ejb3-persistence.jar包。
解决方法:类库中加入ejb3-persistence.jar。

出处:http://javapub.iteye.com/blog/869380

  2,Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/Cacheable

  少了包

解决方案:加入hibernate-jpa-2.0-api-1.0.1.Final.jar

出处http://javapub.iteye.com/blog/869380

  3,class org.hibernate.cfg.ExtendedMappings has interface org.hibernate.cfg.Mappings as super class

  hibernate3.5中的hibernate3.0包,已经包涵了hibernate-annotations,hibernate-commons-annotations.jar,commons-collections 这三个包,

解决方法:要去掉commons-collections 一个包或者hibernate-annotations,hibernate-commons-annotations.jar两个包。

附件:所需包

http://download.csdn.net/detail/shall_we_talk/6245523

S2SH三大框架整合(配置及思想)的更多相关文章

  1. SSH三大框架整合配置详解

    首先,三大框架整合,肯定是要导入相当多的jar包,这是不容置疑的!     这里就不一一列举了,直接截图吧:             (1) 基于配置文件的整合:        第一步:我们需要在we ...

  2. s2sh三大框架整合过程(仅供参考)

    三大框架顾名思义就是非常有名的Struts2 ,Hibernate,Spring, 框架整合的方法很多,现在我写一个非常简单的整合过程,相信大家一看就会! 这里使用的struts-2.2.1.1.hi ...

  3. SSH三大框架整合配置详细步骤(3)

    5 配置Spring2.5 5.1 基础配置 1)        导入spring包.下载spring-framework-2.5.6并解压后,在spring-framework-2.5.6" ...

  4. SSM三大框架整合配置详解

    首先,导入框架所需要的全部jar包(此处省略...........) 第一步:先从mybatis框架开始 我们只需要在mybatis的核心配置文件sqlConfigXml里写上这么一段话,代表的是给p ...

  5. SSM三大框架整合配置(Spring+SpringMVC+MyBatis)

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  6. SSH三大框架整合配置详细步骤(2)

    4 配置Hibernate Hibernate MySql连接配置 在Hibernate中,可以配置很多种数据库,例如MySql.Sql Server和Oracle,Hibernate MySql连接 ...

  7. SSH三大框架整合配置详细步骤(1)

    配置Struts2.0 3.1 基础配置 1)引入Struts必需的五个jar包.下载struts-2.1.6-all.zip解压后,struts-2.1.6\lib目录下是struts所有的相关ja ...

  8. 手动配置三大框架整合:Spring+Struts2+mybatis

    如今主流的项目框架中,数据库持久层有可能不是hibernate,而是mybatis或者ibatis,事实上它们都是一样的,以下我来把环境搭建一下: [导入相关jar包]新建web项目projectms ...

  9. ssm三大框架整合基本配置

    ssm三大框架整合基本配置 maven目录结构 数据库脚本mysql create database maven; use maven ; -- --------------------------- ...

随机推荐

  1. SPI 驱动分析

    断更博客两个月后我又回来了,眯着躺倒就能睡熟的小眼睛,在这儿敲键盘.这篇文章给你快乐,你有没有爱上我! SPI驱动由三部分组成,分别为drivers.core.device.通过bus总线连接.困了不 ...

  2. ROS的单线程Spinning和多线程Spinning

    单线程Spinning ros::spin()是最简单的单线程自旋, 它会一直调用直到结束 用法:  ros::spin(); 另一个单线程spinning是ros::spinOnce(),它定期调用 ...

  3. 用 highlight.js 为文章中的代码添加语法高亮

    来源:http://www.ghostchina.com/adding-syntax-highlighting-to-ghost-using-highlight-js/ --------------- ...

  4. hibernate5ID生成策略

    1.uuid2:使用JDK自带的UUID生成36位的ID 2.guid: 3.uuid:生成32位的uuid,不符合ETF RFC 4122标准,已被uuid2取代. 4.uuid.hex:等同uui ...

  5. lsof 常用命令

    lsof 常用命令   原文地址: Lsof 是遵从Unix 哲学的典范,它只做一件事情,并且做的相当完美——它可以列出某个进程打开的所有文件信息.打开的文件可能是普通的文件,目录,NFS文件,块文件 ...

  6. Linux主机共享目录给Windows主机的方法

    Linux主机共享目录可以通过samba来实现 首先,来看下百科上关于samba的介绍: Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Ser ...

  7. Electron-使用Electron开发第一个应用

    使用Electron开发第一个应用 Electron 应用的目录结构如下: app/ ├── package.json ├── main.js └── index.html 新建一个app文件夹 将这 ...

  8. iOS UIView上添加mp4视频

    ViewController.h文件中添加如下代码: #import <MediaPlayer/MediaPlayer.h> @property(nonatomic,retain) MPM ...

  9. 实验四 简单的PV操作

    实验四 简单的PV操作 专业 网络工程   姓名 方俊晖 学号 201406114309 一.        实验目的 1.掌握临界区的概念及临界区的设计原则: 2.掌握信号量的概念.PV操作的含义以 ...

  10. 二、Python 数据类型

    计算机是用来辅助人类工作的,能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定义不同的数据类型,在程序设计中映射了现实世界的分类,以便于抽象的分析 序列:不 ...