Spring live中文版
Matt Raible
白汉奇[译]
版权 © 2004 SourceBeat, LLC
Many designations used by organizations to distinguish their products are claimed as trademarks. These trademarked names may appear in this book. We
use the names in an editorial fashion only with no intention of infringing on the trademark; therefore you will not see the use of a trademark symbol with
every occurrence of the trademarked name.
As every precaution has been taken in writing this book, the author and publisher will in no way be held liable for any loss or damages resulting from the
use of information contained in this book.
XML to PDF by RenderX XEP XSL-FO Formatter, visit us at http://www.renderx.com/
目录
关于作者 ......................................................................................................................................... xii
致谢 ............................................................................................................................................... xiii
简介 ............................................................................................................................................... xiv
翻译札记 ......................................................................................................................................... xvi
缘起 ....................................................................................................................................... xvi
在NetBeans中配置Equinox ......................................................................................................... xvi
1. Spring 简介 .................................................................................................................................... 26
Spring 诞生记 ............................................................................................................................ 26
关于Spring ................................................................................................................................ 26
为什么每个人都钟情于它 .................................................................................................. 26
对Spring的一些常见的批评 ................................................................................................ 27
Spring 的工作原理 ............................................................................................................. 27
Spring如何简化J2EE开发 ........................................................................................................... 28
接口编程 ......................................................................................................................... 29
简单的测试 ...................................................................................................................... 29
降低耦合:Factory Pattern vs Spring ..................................................................................... 29
本章小结 ................................................................................................................................. 31
2. Spring快速入门教程 ....................................................................................................................... 33
概述 ........................................................................................................................................ 33
下载Struts和Spring ..................................................................................................................... 33
下载Struts和Spring ..................................................................................................................... 34
创建项目目录和Ant Build文件 .................................................................................................... 35
Tomcat和Ant ..................................................................................................................... 36
为持久层编写单元测试 ............................................................................................................. 39
配置Hibernate和Spring ............................................................................................................... 41
Equinox中Spring是如何配置的 ............................................................................................ 42
用Hibernate实现UserDAO ........................................................................................................... 44
进行单元测试,用DAO验证CRUD操作 ....................................................................................... 45
创建Manager,声明事务处理 ..................................................................................................... 46
对Struts Action进行单元测试 ...................................................................................................... 50
为web层创建Action和Model(DynaActionForm) ............................................................................... 51
运行单元测试,验证Action的CRUD操作 ..................................................................................... 56
填充JSP文件,这样可以通过浏览器来进行CRUD操作 .................................................................. 57
通过浏览器验证JSP的功能 ......................................................................................................... 58
用Commons Validator添加验证 .................................................................................................... 60
在struts-config.xml中添加ValidatorPlugin ................................................................................ 60
创建validation.xml,指定lastName为必填字段 ....................................................................... 60
把 DynaActionForm 改为 DynaValidatorForm ......................................................................... 61
为save()方法设置验证(validation) .......................................................................................... 61
本章小结 ................................................................................................................................. 62
3. BeanFactory及其工作原理 ............................................................................................................... 63
关于BeanFactory ........................................................................................................................ 63
BeanFactory中一个bean的生命周期 .............................................................................................. 63
反转控制 ......................................................................................................................... 64
暴露bean定义 ................................................................................................................... 65
配置域属性和依赖关系 ..................................................................................................... 68
预先初始化bean ................................................................................................................ 69
就绪状态 ......................................................................................................................... 73
销毁bean .......................................................................................................................... 74
ApplicationContext:与bean交互 .................................................................................................. 74
抓取context ...................................................................................................................... 74
iv
XML to PDF by RenderX XEP XSL-FO Formatter, visit us at http://www.renderx.com/
单元测试和导入context的一些技巧 ..................................................................................... 75
国际化和MessageSource ...................................................................................................... 76
事件的发布和订阅 ............................................................................................................ 77
深入探讨MyUsers的ApplicationContext.xml ............................................................................ 77
本章小结 ................................................................................................................................. 78
4. Spring MVC 框架 ............................................................................................................................ 79
概述 ........................................................................................................................................ 79
Spring Controller单元测试 ........................................................................................................... 81
配置DispatcherServlet和ContextLoaderListener ................................................................................ 81
修改web.xml,使用Spring的DispatchServlet ........................................................................... 82
为UserController编写单元测试 ............................................................................................ 84
创建UserController,配置action-servlet.xml ............................................................................ 85
创建userList.jsp页面显示用户列表 ....................................................................................... 87
为UserFormController创建单元测试 ..................................................................................... 90
创建UserFormController,并在action-servlet.xml中配置它 ....................................................... 92
创建userForm.jsp页面,以便编辑用户资料 .......................................................................... 97
为Spring配置Commons Validator .......................................................................................... 99
SimpleFormController:方法生命周期回顾 .................................................................................. 102
Spring的JSP标签 ...................................................................................................................... 104
本章小结 ................................................................................................................................ 105
5. 高级的MVC框架----使用模板,验证,异常处理,文件上传 ............................................................. 106
SiteMesh模板技术 .................................................................................................................... 106
安装和配置 .................................................................................................................... 107
Tiles模板技术 .......................................................................................................................... 111
安装和配置 .................................................................................................................... 111
Spring验证方法 ....................................................................................................................... 117
使用Commons Validator ............................................................................................................ 119
XDoclet .......................................................................................................................... 120
一系列的验证器 .............................................................................................................. 121
在业务委派中进行验证 .................................................................................................... 121
Spring未来的声明式验证框架 ........................................................................................... 122
控制器中的异常处理 ............................................................................................................... 123
上传文件 ................................................................................................................................ 126
拦截请求 ................................................................................................................................ 131
发送邮件 ................................................................................................................................ 133
本章小结 ................................................................................................................................ 135
6. View的多种选择 ........................................................................................................................... 136
View(视图)和ViewResolver(视图解析器) ...................................................................................... 136
利用jWebUnit测试View ............................................................................................................. 138
JSP ......................................................................................................................................... 140
View Resolver配置 ............................................................................................................ 140
JSTL .............................................................................................................................. 142
Tiles ............................................................................................................................... 143
Velocity ................................................................................................................................... 144
在MyUsers中使用Velocity .................................................................................................. 144
SiteMesh和Velocity ............................................................................................................ 146
创建Velocity模板 ............................................................................................................. 148
部署和测试 .................................................................................................................... 150
Velocity小结 .................................................................................................................... 151
FreeMarker .............................................................................................................................. 151
View Resolver配置 ............................................................................................................ 151
SiteMesh和FreeMarker ....................................................................................................... 152
创建FreeMarker模板 ......................................................................................................... 155
部署和测试 .................................................................................................................... 158
v
Spring live中文版
XML to PDF by RenderX XEP XSL-FO Formatter, visit us at http://www.renderx.com/