SSH三大框架搭建整合struts2+spring+hibernate
SSH框架整合理论:
在SSH框架的JAVAweb项目的开发过程中,WEB层通常使用的是Struts2+jsp,service层使用的是javaBean,DAO层使用的是hibernate,而spring的使用覆盖三层。
使用了spring框架之后,我们可以把对象交给spring来管理。在WEB层中,所有action对象的创建和管理都可以交给spring来完成,这样Struts2就不用自己来new一个action,这一切都可以交给spring,直接向spring来要action对象。
在DAO层中hibernate框架,在这一层中hibernate–SessionFactory对象,包括session的获得,AOP事务,都可以交给spring,这样使用hibernate只需要做一些业务操作就可以了,
把其他的关于事务以及对象的管理都交给spring来完成。
简单的说,三大框架整合,就是把Struts2整合到spring当中,把hibernate整合到spring当中。
‘
‘
下面我们来进行三大框架整合:
整合的第一步,我们首先需要做的就是导包,导入三个框架需要用到的jar包,这是三大框架整个中的第一步,也是最重要的一步,导包完成后,我们的三大框架整合就已经完成了百分之六十。
如果三大框架所用到的jar包文件压缩包没有,可以去各个框架的相关网站上下载。
具体需要用的jar包如下
hibernate包:
- hibernate/lib/required
- hibernate/lib/jpahibernate/lib/jpa
- 数据库驱动包数据库驱动包
在hibernate中的lib下的required包中jar文件,都需要拿过来在hibernate中的lib下的required包中jar文件,都需要拿过来。
/lib/jpa java持久化规范
Struts2包:
如果有重复,删除版本低的那个。
Struts整合spring插件包
如果导入这个包,Struts在启动的时候就会寻找spring容器,如果还没有配置spring容器,单单是启动Struts,项目就会抛出异常。如果只是用Struts框架时,这个包可以不需要导入。
spring包:
- 基本包:4+2
- 整合web需要的web包整合web需要的web包
- 整合aop:4个包
- 整合aop:4个包整合aop:4个包
- 整合JDBC事务:4个整合JDBC事务:4个
- 整合Junit4测试:1个
基本包:4+2 core |beans |context| expression| logging| log4j
整合web需要的web包:spring-web
整合aop:4个包:spring-aop|spring-aspect|aop联盟|aopweaving
整合JDBC事务:spring-jdbc|spring-tx|c3p0|spring-orm
整合Junit4测试:spring-test
基本包:
整合web需要的web包
整合aop:4个包
整合JDBC事务
整合Junit4测试
截止到这里,用到的包就导入完了,但是如果需要使用的是eclipse,标签库的包还是需要自己导入一下。
导包完成
导包完成之后,就需要进行三大框架的整合步骤,首先是单独为项目配置每一个框架
单独配置spring容器
创建配置文件,并导入约束(4个)beans|context|aop|tx
首先,创建spring配置文件applicationContext.xml
然后切换到设计视图
然后再继续导入剩余的4个约束
beans约束
context约束
然后aop,tx 这样四个命名空间就导入完成了
代码如下:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<bean name="userAction " class="com.test.web.action.UserAction "></bean>
</beans>
截止到这里,spring的约束以及命名空间也导入完成。
这样spring容器就配置好了,但是我们想让spring随着项目的启动而启动,就需要在web.xml文件中配置一个监听器。
web.xml
<!-- 让spring随web启动而创建的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring配置文件位置参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
这个时候启动项目,项目不报错,说明spring容器随着项目的启动而启动配置没有错误。
单独配置Struts2
- 配置Struts2主配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- # struts.objectFactory = spring 将action的创建交给spring容器
struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
-->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="crm" namespace="/" extends="struts-default" >
<action name="UserAction_*" class="userAction" method="{1}" >
<result name="toHome" type="redirect" >/index.htm</result>
<result name="error" >/login.jsp</result>
</action>
</package>
</struts>
主配置文件写好之后,需要配置Struts2的核心过滤器
在web.xml文件中配置Struts2的核心过滤器
代码如下:
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<!-- struts2核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
需要注意的是:配置到这里,还是建议重新的启动项目,看看是否配置正确,我们在这里需要注意的是,每配置一步,我们就启动项目测试一下,看看该步是否正确
防止,等最后我们配置完的时候,出现错误的时候,不方便进行检查。
配置完spring到web项目,以及配置完Struts2到web项目,下面需要做的就是整合Struts2到spring中
整合Struts2与spring
-
需要导包,导入spring与Struts2整合包,这个包我们在第一步已经导入过了struts2-spring-plugin-2.3.24.jar
-
配置常量:
(1)struts.objectFactory = spring 将action的创建交给spring容器 (2)struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
我们只需要配置第一个常量即可,因为第二个默认是打开的,不用配置。第一个常量是将action的创建交给spring容器 ,我们需要在Struts.xml文件中配置
<!-- # struts.objectFactory = spring 将action的创建交给spring容器
struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
-->
<constant name="struts.objectFactory" value="spring"></constant>
下面进行Struts2与spring整合
整合有两种方案
第一种方案:class属性上仍然配置action的完整类名,struts2仍然创建action,由spring负责组装Action中的依赖属性
使用这种方案,就是在配置文件中action class依旧使用完整的类名,这样可以看成是Struts与spring配合进行对象创建。struts2仍然创建action,由spring负责组装Action中的依赖属性
不过这种方法不推荐使用,不推荐理由:最好由spring完整管理action的生命周期.spring中功能才应用到Action上.
第二种方案:
class属性上填写spring中action对象的BeanName,BeanName是在applicationContext.xml文件中定义的,完全由spring管理action生命周期,包括Action的创建注意:需要手动组装依赖属性
注意:需要手动组装依赖属性,因为这样配置的话,spring就不能自动组装,需要手动组装依赖属性
手动组装依赖属性的意思就是,在applicationContext.xml文件中,把各个对象的依赖手动配置进去。
在Struts.xml文件中,class属性只需要写beanName就可以了
注意:scope=“prototype” Action对象作用范围一定是多例的.这样才符合struts2架构
<!-- action -->
<!-- 注意:Action对象作用范围一定是多例的.这样才符合struts2架构 -->
<bean name="userAction" class="com.test.web.action.UserAction" scope="prototype" >
<property name="userService" ref="userService" ></property>
</bean>
<!-- service -->
<bean name="userService" class="com.test.service.impl.UserServiceImpl" >
</bean>
到这里,Struts整合spring完成了
下面需要做的是。进行单独配置hibernate,然后把hibernate与spring进行整合:
单独配置hibernate
创建hibernate配置文件hibernate.cfg.xml,配置相关实体以及数据库的连接
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
<property name="dataSource" ref="dataSource" ></property>
<!-- 配置hibernate基本信息 -->
<property name="hibernateProperties">
<props>
<!-- 必选配置 -->
<prop key="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url" >jdbc:mysql:///crm</prop>
<prop key="hibernate.connection.username" >root</prop>
<prop key="hibernate.connection.password" >123456</prop>
<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
<!-- 可选配置 -->
<prop key="hibernate.show_sql" >true</prop>
<prop key="hibernate.format_sql" >true</prop>
<prop key="hibernate.hbm2ddl.auto" >update</prop>
</props>
</property>
<!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
<property name="mappingDirectoryLocations" value="classpath:com/test/domain" ></property>
</bean>
这一种方案是推荐方案,我们把sessionFactory交给spring容器来管理
在三大框架整合中,这些是最基本的整合操作,下面将对整合中剩余的的细节进行补充:
使用C3P0连接池:
1.配置db.properties
jdbc.jdbcUrl=jdbc:mysql:///crm
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
2.引入连接池到spring中
<!-- 读取db.properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
<property name="driverClass" value="${jdbc.driverClass}" ></property>
<property name="user" value="${jdbc.user}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>
有了连接池后,直接从连接池中读取配置信息,所以可以在spring容器applicationContext.xml文件中,。就不用配置数据库连接信息了
3.将连接池注入给SessionFactory
<!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
<property name="dataSource" ref="dataSource" ></property>
到这里,配置信息配置的都已经差不多了,下面需要做的是使用DAO方法操作数据库了。