第4章 Spring+Struts+Hibernate(理论).ppt_第1页
第4章 Spring+Struts+Hibernate(理论).ppt_第2页
第4章 Spring+Struts+Hibernate(理论).ppt_第3页
第4章 Spring+Struts+Hibernate(理论).ppt_第4页
第4章 Spring+Struts+Hibernate(理论).ppt_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、SCCE 2010课程体系介绍,第4章 Spring+Struts+Hibernate,回顾,Spring和Hibernate集成 Spring和Hibernate集成的优点 Spring DAO用法 BaseDAO的写法,本章内容,SSH集成的优点 创建ApplicationContext的方式 Spring整合Struts SSH应用,本章目标,了解SSH集成的优点 掌握创建ApplicationContext的两种方式 掌握Spring对Struts的管理 理解SSH注入过程 掌握SSH应用,1. SSH介绍,1.1 SSH的层次结构 企业级的J2EE应用中,最典型的是三层结构 表现层

2、中间层(业务逻辑层) 数据服务层,SSH的层次结构,中间层采用流行的Spring+Hibernate 为了将控制层与业务逻辑层分离,可以进行细分 Web层 Service层 DAO层 PO Spring的作用贯穿了整个中间层,将Web层、Service层、DAO层及PO无缝整合,1.2 SSH的优点,一个良好的框架可以让开发人员减轻重新建立解决复杂问题方案的负担和精力;它可以被扩展以进行内部的定制化;并且有来自用户社区的强大支持 实现视图、控制器与模型的分离 实现业务逻辑层与持久层的分离,2. Spring管理各层的关系,2.1 Web服务的启动过程 Spring ApplicationCon

3、text作为IOC容器,应该优先加载 ,加载方式如下 ContextLoaderListener ContextLoaderServlet,Web服务的启动过程,ContextLoaderListener方式 Spring提供了ServletContextListener的一个实现类ContextLoaderListener 如果只有一个配置文件applicationContext.xml ,web.xml写法如下, org.springframework.web.context.ContextLoaderListener ,Web服务的启动过程,如果存在多个配置文件, contextConf

4、igLocation /WEB-INF/classes/applicationContext.xml, /WEB-INF/classes/applicationContext_biz_user.xml, /WEB-INF/classes/applicationContext_biz_emp.xml, /WEB-INF/classes/applicationContext_biz_card.xml org.springframework.web.context.ContextLoaderListener ,配置文件用“,”分隔,Web服务的启动过程,多个配置文件通过classpath加载, co

5、ntextConfigLocation classpath*:applicationContext*.xml org.springframework.web.context.ContextLoaderListener ,contextConfigLocation名称不能变,所有以applicationContext开头的配置文件,Web服务的启动过程,ContextLoaderServlet方式 Spring提供了一个特殊的Servlet类ContextLoaderServlet, context org.springframework.web.context.ContextLoaderSer

6、vlet 1 ,值为1,保证配置文件优先加载,如果有多个配置文件,操作方式与ContextLoaderListener相同,Web服务的启动过程,web.xml中加载struts的配置方式, action org.apache.struts.action.ActionServlet config /WEB-INF/config/struts-config.xml, 0 action *.do ,一个Struts配置文件, action org.apache.struts.action.ActionServlet config /WEB-INF/config/struts-config.xml,

7、 /WEB-INF/config/struts-config-user.xml, /WEB-INF/config/struts-config-employee.xml 0 action *.do ,多个Struts配置文,用“,”分隔,2.2 Spring整合Hibernate,连接Oracle数据库的方式,lang jdbc:oracle:thin:localhost:1521:myOracle org.hibernate.dialect.Oracle9Dialect myOracle_DB 000000 oracle.jdbc.driver.OracleDriver ,连接Oracle的U

8、RL,Oracle的数据库方言,Oracle的数据库驱动,2.3 Spring整合Struts,Spring整合Struts的两种策略 使Spring IOC容器管理Struts的Action 采用Spring的ActionSupport类的子类 推荐使用Spring IOC容器管理Struts Action的方式。采用这种方式能充分利用Spring依赖注入的优势,而不必显式地加载ApplicationContext实例,Spring整合Struts,Spring IOC容器管理Action的方式有两种 使用org.springframework.web.struts.DelegatingRe

9、questProcessor, ,使用DelegatingRequestProcessor,Action的type元素可以去掉,type元素可以去掉,Spring整合Struts,使用org.springframework.web.struts.DelegatingActionProxy, ,使用DelegatingActionProxy,无须controller,2.4 Spring管理依赖关系,Spring管理各组件间的依赖关系,3. SSH应用,3.1 分别添加SSH特性 添加Spring和Hibernate的顺序要先添加Spring,然后再添加Hibernate SSH推荐的添加方式

10、Spring Hibernate Struts,3.2 DAO层,DAO的基类BaseDAO及其配置,public class BaseDAO extends HibernateDaoSupport /实体对象全路径名称,包括包名 private String poName = ; public List getObjects() List poList = null; try String hql = from + poName; poList = this.getHibernateTemplate().find(hql); catch (Exception e) e.printStackT

11、race(); return poList; public void setPoName(String poName) this.poName = poName; ,对poName设置注入,查询所有记录,其他方法省略,DAO层,DAO的配置, com.hr.g3.persist.Account ,将SessionFactory注入给BaseDAO的HibernateDaoSupport,将具体的实体对象注入给具体DAO,3.3 业务逻辑层,业务逻辑层接口,public interface AccountHandler public AccountDAO getAccountDAO(); ,pu

12、blic class AccountHandlerImpl implements AccountHandler private AccountDAO accountDAO; public AccountDAO getAccountDAO() return accountDAO; public void setAccountDAO(AccountDAO accountDAO) this.accountDAO = accountDAO; ,业务逻辑层实现类,业务逻辑层,业务逻辑配置文件, ,将DAO注入给业务逻辑类,3.4 web层,ActionForm类,public class UserFor

13、m extends ActionForm private Account account = new Account(); public Account getAccount() return account; public void setAccount(Account account) this.account = account; ,用实体对象作为ActionForm属性,web层,Action类,public class UserAction extends DispatchAction private static final Logger logger = Logger.getLo

14、gger(UserAction.class); /定义用户管理的业务逻辑类 private AccountHandler accountHandler; public void setAccountHandler(AccountHandler accountHandler) this.accountHandler = accountHandler; /省略Action方法 ,定义日志,定义业务逻辑类,为方法注入,web层,Action类方法,public ActionForward login(ActionMapping mapping, ActionForm form, HttpServle

15、tRequest request, HttpServletResponse response) ActionForward forward = new ActionForward(); UserForm userForm = (UserForm) form; Account user = accountHandler.getAccountDAO().login(userForm.getAccount(); if(user != null) request.getSession().setAttribute(“user, user); forward = mapping.findForward(

16、login); else forward = mapping.findForward(error); return forward; ,根据用户对象查询登录用户,将用户对象放入Session中,web层,Struts配置文件, ,使用DelegatingRequestProcessor ,省略action的type元素,web层,Spring配置文件, ,这里必须是name元素,而不是ID元素,name元素值匹配Struts配置文件action的path元素值,将业务逻辑类注入给action,web层,登录页面, userName: password: ,web层,登录成功, main.jsp

17、 欢迎$employee.empName 您登录成功 ,web层, error.jsp ,登录失败,S2SH - web.xml,web.xml文件,添加struts2配置,spring配置部分与SSH相同 struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /* ,S2SH - struts.xml,在struts.xml文件头部加上配置 在spring配置文件中注入Action类 在struts配置文件中引用配置 ,当发生请求时 委托spring代理,引用spring中注入名称 则名称相同,spring事务,修改ap

18、plicationContext.xml 文件元素 ,spring2事务,添加事务配置管理器 ,spring2事务,添加事务通知 每个method配置是目标对象中连接点的描述,例如以upd, sav开头的方法会自动添加事务,spring2事务,添加事务代理 expression=execution(* com.alex.service.*.*(.) 表达式是对那些目标对象拥有事务的描述 (* com.alex.service.*.*(.),有空格,表示包,表示所有类,表示所有方法,注意:被注入的目标对象假如向拥有事务,必须有接口(AOP事务必须面向接口),spring2事务,修改struts.xml文件 true:表示出现异常后由struts

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论