struts2 spring3.2 hibernate4.1 框架搭建 整合

ssh是企业开发中常遇到的框架组合,现将框架的搭建过程记录下来,以便以后查看。
我的搭建过程是,首先struts,然后spring,最后hibernate。
struts2的最新版本为2.3.8,我下载的是完整包,包含示例和所有jar包,下载地址为:http://struts.apache.org/
spring的最新版本为3.2.1,下载地址为:http://www.springsource.org/download/community
hibernate的最新版本为4.1.9,下载地址为:http://www.hibernate.org/downloads

struts2

1.jar包准备

打开下载的zip包,apps目录下,解压struts2-blank.war,struts2-blank.war是一个空的项目,我们可以把这个项目的所有jar包导入到当前的项目中。
在lib目录下,找到struts2-spring-plugin-2.3.8.jar包,放入项目中,集成spring的时候用到。

2.配置文件准备

同上,把struts2-blank.war中的struts.xml放入当前项目中,适当的去掉用不到的配置,如include标签。这样,struts2的准备工作就做完了。

web.xml配置

打开web.xml,配置struts2的过滤器,所有的请求都交给struts2去处理,配置如下:

1
2
3
4
5
6
7
8
9
<!-- 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>

spring3.2

1.jar包准备

jar包不止以下列出的这些,在大家过程中,根据提示,再加载缺少的jar包
spring-aop-3.2.1.RELEASE.jar、spring-beans-3.2.1.RELEASE.jar、spring-context-3.2.1.RELEASE.jar、spring-core-3.2.1.RELEASE.jar
spring-expression-3.2.1.RELEASE.jar、spring-orm-3.2.1.RELEASE.jar、spring-tx-3.2.1.RELEASE.jar、spring-web-3.2.1.RELEASE.jar

2.配置文件准备

spring需要的配置文件默认为:WEB-INF下的applicationContext.xml文件,可是,我想在官网找3.2.1版本对应的applicationContext.xml这个文件,没找到,后来无奈只能从以前的项目中拷贝,并修改,修改的时候请注意,applicationContext.xml文件中beans节点的属性改成对应spring的版本号,并检查xsd文件是否存在。

3.web.xml配置

配置spring的监听,配置方式如下:

1
2
3
4
5
6
7
<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>

applicationContext.xml文件默认在WEB-INF下,通过指定context-param可以改变applicationContext.xml的存放位置。

hibernate

1.jar包准备

打开下载的hibernate文件包,拷贝lib equired 目录下的所有jar至WEB-INF/lib下,在以后运行中,如缺少包,再根据提示找相应的jar包。

2.配置文件准备

hibernate的核心配置文件为hibernate.cfg.xml,用来配置数据库连接信息,方言,对应实体的配置文件等。
hibernate.cfg.xml文件的全部信息也可以配置到applicationContext.xml文件中。

3.配置使用spring来管理sessionFactory。

这个配置需要在applicationContext.xml中来配置

1
2
3
4
5
6
7
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <!-- 指定数据源 -->
    <property name="dataSource" ref="dataSource"/>
    <!--指定Hibernate核心配置文件-->
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
原文地址:https://www.cnblogs.com/herd/p/5264098.html