《ajax 框架:dwr 与 ext》实战 胡东峰
《ajax 框架:dwr 与 ext》实战
动手!动手!再动手!!!
先有效果,再讲道理!!!
V0.1 2008-2-17
作者: 胡东峰
javaFound@gmail.com
蓝杰信息 www.NetJava.cn
1
胡东
峰
数字签名人 胡东峰
DN:cn=胡东峰,
c=CN-中国,o=蓝杰
信息,ou=软件技术,
email=foxhdf@gma
il.com
日期:2008.02.17
17:09:28 +08'00'
《ajax 框架:dwr 与 ext》实战 胡东峰
目 录
7.DWR 快速上手 .......................................................................................................................................................................3
1.DWR
简介 ................................................................................................................................................................................ 3
2.DWR
马上上手 : 在
JS
调用
JAVA
对象 .................................................................................................................................... 6
3.DWR
进阶 : LOADING
界面创建 ............................................................................................................................................ 11
4.DWR
进阶 : 线交流功能 ...................................................................................................................................................... 14
5.DWR
应用小结 ................................................................................................................................................................... 17
8.DWR 综合实践:用户管理模块实现...................................................................................................................................18
1. 系统功能界面: ................................................................................................................................................................. 18
2. 编写后台业务实现: ......................................................................................................................................................... 20
3. 测试
DWR : .......................................................................................................................................................................... 22
4. 编写前台
DWR
调用代码: .............................................................................................................................................. 24
5. 集合类型的自动转换: ................................................................................................................................................... 27
6.JAVA
对象与
JS
对象的对接: .............................................................................................................................................. 27
7.DWR
中快捷的批赋值方法: .............................................................................................................................................. 28
9.DWR 详解...............................................................................................................................................................................29
1.DWR
组件分析: ............................................................................................................................................................... 29
2.DWR
中的数据类型转换 ................................................................................................................................................... 30
3.DWR SERVLET
配置: ............................................................................................................................................................. 30
4.DWR
中的
SESSION
管理和页面转发 ...................................................................................................................................... 31
5.DWR.XML
配置 ....................................................................................................................................................................... 32
6. 使用
ANNOTATION
代替
DWR.XML
配置 .................................................................................................................................... 33
7.ENGINE.JS
说明: ................................................................................................................................................................... 34
8.UTIL.JS
说明: ....................................................................................................................................................................... 34
10.DWR+SP+HB 整合:重构《员工日志管理》系统.............................................................................................................35
1.DWR+SP+HB
结构说明: ................................................................................................................................................ 35
2.DWR
与
SPRING
整合说明: ................................................................................................................................................... 36
3.DWR
与
HIBERNATE
整合说明: .............................................................................................................................................. 36
4.DWR
整合总结 : ..................................................................................................................................................................... 36
5. 任务与总结: ..................................................................................................................................................................... 37
11.关与 EXT...............................................................................................................................................................................37
1. 为什么需要
EXT? ................................................................................................................................................................. 37
蓝杰信息 www.NetJava.cn
2
《ajax 框架:dwr 与 ext》实战 胡东峰
7.DWR 快速上手
1.dwr 简介
通过前面几节,我们己体验到了 ajax 应用的强大功能,但对与习惯了一种 OOP 语言编程
的程序员来说: DOM、CSS、javascript、html,如何把这么多异构的东东协调的组合在一起,
是一个十分头疼的问题;即使写好的 AJAX 应用,由与大量的使用了各种异构语言,系统的或
维护性和清晰性也会变得困难。而对与每个 AJAX 应用而言,核心的功能是充当浏览器和服务
器间的中间件----我们再从一个较高的层面抽像这个问题,后台以 java 语言为例:ajax 实现的
功能是将 java 对象从服务器上取下来,显示到浏览器组件中。
创建 XMLHTTPRequest、发送请求、在指定的回调函数中解析服务器返回的数据(这个数据
其实就是后台的某个 javaBean),更新页面;这三步是每个 AJAX 应用必不可少的。我们说过,
一但工作有规则,就有可能用代码去 hack!这就是程序员的职则,与催生 MVC、ORM 等框架
的原因同样,AJAX 也有很多现成的框架,让我们使用,让我们的将精力放在与业务相关的处理
中而不是纠缠在 dom 或 js 的泥潭里。
DWR(Direct Web Remoting)就是这样的一个东东,请登陆
http://getahead.org/dwr/ DWR allows Javascript in a browser to interact with
Java on a server and helps you manipulate web pages with the results.如同 DWR
图标所示的那样:
我猜想蓝、红箭头分别是代表了 javaScript 和
javaBean,DWR 做了中介:在页面的 js 中直接调用服务器上 javaBean,这可是个有创意的
东东。
DWR 主页上关与其的详细介绍如下:
DWR is a RPC library which makes it easy to call Java functions
from JavaScript and to call JavaScript functions from Java (a.k.a
Reverse Ajax).
蓝杰信息 www.NetJava.cn
3
《ajax 框架:dwr 与 ext》实战 胡东峰
It has a large user-base, active mailing list and has been used in
many projects including the Walmart shopping site and American
Airlines flight booking site.
DWR has a number of features like call batching, marshalling of
virtually any data-structure between Java and Javascript (including
binary file uploading and downloading), exception handling, advanced
CSRF protection and deep integration with several Java server-side
technologies like Spring and Guice.
The first diagram shows how DWR can alter the contents of a selection
list as a result of some Javascript event.
Reverse Ajax (available since DWR version 2.0) allows Java code running on the server
to find out what clients are viewing certain pages, and to send to them JavaScript,
generated either manually or using a Java API. These JavaScript generating APIs
generally match a client-side APIs.
蓝杰信息 www.NetJava.cn
4
《ajax 框架:dwr 与 ext》实战 胡东峰
DWR consists of two main parts:
• A Java Servlet running on the server that processes requests and sends
responses back to the browser.
• JavaScript running in the browser that sends requests and can dynamically
update the webpage.
DWR works by dynamically generating Javascript based on Java classes. The code
does some Ajax magic to make it feel like the execution is happening on the browser,
but in reality the server is executing the code and DWR is marshalling the data back
and forwards.
This method of remoting functions from Java to JavaScript gives DWR users a feel
much like conventional RPC mechanisms like RMI or SOAP, with the benefit that it runs
over the web without requiring web-browser plug-ins.
The DWR project is developing a method of automatically creating Java versions of
JavaScript APIs which developers can use to control browsers from the server. A
server-side version of the TIBCO GI library is currently in alpha release, and the DWR
project aims to expand this to cover other client side APIs including the Dojo Toolkit,
JQuery, YUI, Ext and others.
For details on how to get started with DWR see the getting started guide and
download instructions.
没错,这段文字是直接从 DWR 网站上复制下来的,如果你看不懂这些英文,你就找个字典一
个词一个词的看!要做程序员,你口语不标准可以接授,但如你看不懂简单的英文文档---那就
蓝杰信息 www.NetJava.cn
5