前言 转载请标明出处:http://www.cnblogs.com/smfx1314/p/7795837.html
本项目是我写的一个练习,目的是回顾ssh框架的整合以及使用。项目介绍:此项目主要有前台管理员通过登录进入员工管理系统页面,之后可以对员工列表进行常规的增删改查。以及部门列表的增删改查。IDE使用的是eclipse,个人感觉比较好用,不过最近我正在研究idea,数据库是mysql,前台主要以bootstrap为主。
这点是直接摘抄的
struts 控制用的
hibernate 操作数据库的
spring 用解耦的
Struts 、 spring 、 Hibernate 在各层的作用
1 ) struts 负责 web 层 .
ActionFormBean 接收网页中表单提交的数据,然后通过 Action 进行处理,再 Forward 到对应的网页。
在 struts-config.xml 中定义 , ActionServlet 会加载。
2 ) spring 负责业务层管理,即 Service (或 Manager).
1 . service 为 action 提供统计的调用接口,封装持久层的 DAO.
2 .可以写一些自己的业务方法。
3 .统一的 javabean 管理方法
4 .声明式事务管理
5. 集成 Hiberante
3 ) Hiberante ,负责持久化层,完成数据库的 crud 操作
hibernate 为持久层,提供 OR/Mapping 。
它有一组 .hbm.xml 文件和 POJO, 是跟数据库中的表相对应的。然后定义 DAO ,这些是跟数据库打交道的类,它们会使用 PO 。
在 struts+spring+hibernate 的系统中,
对象的调用流程是: jsp-> Action - > Service ->DAO ->Hibernate 。
数据的流向是 ActionFormBean 接受用户的数据, Action 将数据从 ActionFromBean 中取出,封装成 VO 或 PO,
再调用业务层的 Bean 类,完成各种业务处理后再 forward 。而业务层 Bean 收到这个 PO 对象之后,会调用 DAO 接口方法,进行持久化操作。
spring:Aop管理事务控制,IoC管理各个组件的耦合,DaoTemplate作为常规持久层的快速开发模板!
struts:控制层Action,页面标签和Model数据,调用业务层
Hibernate:负责数据库和对象的映射,负责DAO层(Data Access Object:数据访问)
spring整合hibernate和struts,只要在配好了applicationContext.xml,在struts的action中直接调用就可以了。hibernate访问数据库的操作都在spring中实现了,spring的调用又在stuts的action中实现了。这个ssh框架就连到了一起……
备注
这里关于mysql的表我就不贴出来了,这个大家可以根据实体类进行创建,我创建的是emp和dept表,他们之间是一对多关系。
另外,关于jsp页面中的*.js,你可以根据jsp中的路径自己创建
由于内容过多,今天我先说下ssh框架的整合
接下来让我们看下目录结构
第一步,导入jar包
第二步:我们看下index.jsp页面
pageEncoding="UTF-8"%>
登录用户名
密码
确认
上面主要使用了bootstrap框架。当然,登录页面我做的比较简单,你也可以根据自己的感觉去写效果
第三步是配置文件,首先我们配置web.xml
ssh-day02
index.jsp
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
OpenSessionInviewFilter
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
OpenSessionInviewFilter
/*
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
都是些基本的配置,no session配置是后边两张表进行关联查询时session提前关闭的问题
第四步:把对应的包类创建出来,方便我们在applicationContext.xml中配置bean实例
接下来我们开始配置applicationContext.xml
主要是创建数据库连接池,创建sessionFactory ,配置hibernate属性以及声明式事务,aop(我这块没有用到,就没有配置),以及bean的实例化配置
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
org.hibernate.dialect.MySQLDialect
true
update
com/ssh/entity/User.hbm.xml
com/ssh/entity/Emp.hbm.xml
com/ssh/entity/Dept.hbm.xml
然后在引入Struts2的配置文件
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
注意,上面把hibernate的配置文件和spring进行了整合,所有没有单独创建hibernate的配置文件。
到这里,ssh的配置基本完成。运行成功就是一个登陆页面