16 struts、spring、hibernate的整合.ppt

  1. 1、本文档共22页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第16章 Struts、Spring、Hibernate 的整合 本课教学内容 Struts整合Hibernate 整合Spring Struts 整合 Hibernate 整合结构如图 Struts 整合 Hibernate 目录结构如下: Struts 整合 Hibernate 从图中可以看出,前端使用 Struts 等框架完成,后端采用 Hibernate 访问数据库。而 Spring主要运行在 Struts 和 Hibernate 的中间,一般情况下,Spring 负责降低 Web 层和数据库层之间的耦合性,或者说,让 Struts 中的 Action 在调用 Hibernate 中的 DAO 时,尽量降低耦合性 编写数据库访问层 开发此项目,可以首先建立数据库层,即:编写 DAO。我们可以用 Hibernate 框架来简化 DAO 的编程 具体代码示例见课本 增加 Struts 框架支持 前端我们使用 Struts 完成,目录结构如下: 增加 Struts 框架支持 具体代码示例见课本 整合 Spring 将 Spring 框架支持导入到项目中去 整合 Spring 将 Spring 框架支持导入到项目中去 重构 CustomerDao 要降低耦合性,首先应该使得 Customer 的调用面向接口编程。为其编写接口: ICustomerDao.java package idao; import po.Customer; public interface ICustomerDao { public Customer getCustomerByAccount(String account); } 重构 CustomerDao 修改 CustomerDao.java,让其实现 ICustomerDao 接口。代码为: CustomerDao.java package dao; import idao.ICustomerDao; import org.hibernate.Session; import po.Customer; public class CustomerDao implements ICustomerDao{ ...} 修改 LoginAction 将 LoginAction 修改为面向接口编程,并将 ICustomerDao 定义为属性 具体代码示例见课本 Struts 整合 Spring Struts 整合 Spring 的原理是,让 LoginAction 和 CustomerDao 的装配由 Spring 框架来完 成,因此,Spring 框架负责生成这两个对象,并进行装配。但是 LoginAction 本来是由 Struts框架里的 ActionServlet 生成的,而依赖注入必须由 Spring 框架来完成 Struts 整合 Spring 现在需要让Spring 框架来生成 LoginAction,从而达到将 CustomerDao 装配LoginAction 的目的。也就是在 Struts 生成 LoginAction 之前截取之,命令其在 Spring 框架中生成 LoginAction Struts 整合 Spring 步骤为: 让 Struts 框架认识Spring 配置文件。方法是配置插件,在 Struts 的配置文件struts-config.xml中增加如下部分: plug-in className=org.springframework.web.struts.ContextLoaderPlugIn set-property property=contextConfigLocation value=/WEB-INF/applicationContext.xml / /plug-in Struts 整合 Spring 用org.springframework.web.struts.DelegatingActionProxy 类截获 Action 的生成。方法为:将 action 的 type 属性改动,保证提交的内容能被 Spring 截获 ,将 Struts 配置文件中的type 属性改为: action name=loginForm path=/login scope=request type=org.springframework.web.struts.DelegatingActionProxy / Struts 整合 Spring 修改 Spring 配置文件,使 bean 的 name 属性的值和 Struts 配置文件中 Action 的 path相

文档评论(0)

fpiaovxingl + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档