一、什么是SSH
SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架。
二、SSH框架整合原理
1.spring与struts2整合是将Action对象交给spring容器负责创建。
2.spring与hibernate整合是将sessionfactory交给spring来负责维护。
3. spring负责session维护以及aop事务。
三、SSH优势
1. 典型的三层架构体现MVC的思想。
2 .良好的可扩展性,ssh主流技术有强大的用户社区支持它,所以该框架扩展性非常强,针对特殊应用时具有良好的可插拔性,避免大部分因技术问题不能实现的功能。
3. 良好的可维护性,业务系统经常会有新需求,三层构架因为逻辑层和展现层的合理分离,可使需求修改的风险降低到最低。随着新技术的流行或系统的老化,系统可能需要重构,ssh构架重构成功率要比其他构架高很多。
四、准备工作
2.Eclipse下载地址(建议使用Eclipse IDE for Java EE Developers)
3.Tomcat(与JDK版本一致) 下载地址
5.SQLyog 下载地址
五、搭建步骤
新建web项目
打开sqlyog,创建ssh_crm数据库
1.导包
①各类包的下载地址 hibernate struts2 spring
springsource-tool-suite-3.9.4.RELEASE-e4.7.3a-updatesite(此包为spring框架依赖的jar包)
②导入hibernate: 将hibernate中lib文件中的required包全部导入eclipse lib目录下
导入java的持久化规范接口,在lib文件下的jpa文件中
③导入Mysql数据库驱动包 下载地址
④导入struts2
将apps文件下的struts2-blank.war包解压(可将此包放入到Tomact的webapps下,运行tomcat,自动解压)
解压成功后打开WEB-INF文件的lib文件夹,复制全部包导入eclipse lib目录下
javassist包会有重合,删除低版本
⑤导入struts2整合spring插件包,注意:这个包一旦导入,那么struts2在启动时就会寻找spring容器,找不到将会抛出异常
在strus2文件中的lib目录下,
⑥导入spring
a.基本包:4+2(core| beans| context| expression | logging|log4j), logging与log4j包在springsource-tool-suite包中
b.整合web:web包
c. 整合aop:4个(spring-aop| spring -aspect | spring aop联盟|aopweaving)
d. 整合jdbc事务:3个+整合hibernate包(Spring-jdbc| spring-tx | c3p0+spring-orm)
e.正Junit4测试:test包
f.标签库下载地址
g.导包完毕,共41个包
六、单独配置spring容器
1.在src目录下创建applicationCotext.xml文件
2.导入约束
<?xmlversion="1.0" encoding="utf-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
将红色文字复制到xml头部
3.web.xml中配置spring随项目启动
启动项目,没有报错
七、 单独配置struts2
1.创建struts.xml文件
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
复制到头部,可将版本号2.3改为2.1,即可自动提示代码
2.配置struts.xml
3.在web.xml中配置过滤器(可复制下面代码)
<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>
八、struts2与spring整合
1.导包:struts2-spring-plugin-2.3.24(已导入)
2.struts2.xml中配置常量
3.整合
a.整合方案1:class属性上仍然配置action的完整类名,struts2仍然创建action,由spring负责组装Action中的依赖属性(不推荐),理由:最好由spring完整管理action的生命周期,spring中的功能才能应用到action
b.整合方案2(推荐):class属性上填写spring中action对象的BeanName,完全由spring管理action生命周期,包括Action的创建,注意:需要手动组装依赖属性
启动项目,不报错
九、配置hibernate
2.配置主配置文件
在hibernate包中导入hibernate.cfg.xml,修改配置
十、spring整合hibernate
1.整合原理:将sessionFactory对象交给spring容器管理
2.加载方式