SUN的《的《AJAX与与J2EE》全文译了》全文译了
SUN的《AJAX与J2EE》全文译了
开玩笑、原文出处:http://java.sun.com/developer/technicalArticles/J2EE/AJAX/
中英文对照如下、全是我自己译的、水平有限(6级考78分)、欢迎指正:
Asynchronous JavaScript Technology and XML (AJAX)
With Java 2 Platform, Enterprise Edition
——《异步JavaScript+XML技术与J2EE》
By Greg Murray, June 9, 2005
Anyone who has used Flickr, GMail, Google Suggest, or Google Maps will realize that a new breed of dynamic web applications is emerging. These applications look and act very similar to traditional desktop applications without relying on plug-
ins or browser-
specific features. Web applications have traditionally been a set of HTML pages that must be reloaded to change any portion of the content. Technologies such as JavaScript programming language and cascading style sheets (CSS) have matured to the point where they can be used effectively to create very dynamic web applications that will work on all of the major browsers. This article will detail several techniques that you can use today to enable your web applications to be more rich and interactive like desktop applications. ——
使用过Flickr、GMail,、Google、Suggest或Google地图的人都知道一种全新的动态WEB应用正在成型、这些应用看上去和传统桌面应用非常相似、他们不依赖于插件或者浏览器特效。传统意义上的
WEB应用就是一组网页、当页面上有任何一点内容有所变化的时候必须更新整个网页。一些技术比如JavaScript和CSS已经非常成熟、你可以使用他们高效的创建动态网页、并可以跑在大多数主流浏览
器上。本文将细述数种可以使你的WEB应用更丰富更具交互性(就象桌面应用一样)的技术。
Introducing Asynchronous JavaScript Technology and XML (AJAX)
——AJAX介绍
Using JavaScript technology, an HTML page can asynchronously make calls to the server from which it was loaded and fetch XML documents. The XML documents may then be used by the JavaScript technology to update or modify the Document Object Model (DOM) of the HTML page. The term Asynchronous JavaScript Technology and XML (AJAX) has emerged recently to describe this interaction model. ——
使用JavaScript技术、HTML网页可以异步调用生成它的服务器上的服务并可以获取返回的XML文档形式的结果。JavaScript再使用这个XML文档更新或改动本网页的文档对象模型(DOM)、名词AJAX
就是近年来出现的用与描述这种交互模式的新名词。
AJAX is not new. These techniques have been available to developers targeting Internet Explorer on the Windows platform for many years. Until recently, the technology was known as web remoting or remote scripting. Web developers have also used a combination of plug-
ins, Java applets, and hidden frames to emulate this interaction model for some time. What has changed recently is that the inclusion of support for the XMLHttpRequest object has became ubiquitous in the mainstream browsers across all platforms. The real magic is the result of the JavaScript technologys XMLHttpRequest object. Although this object is not specified in the formal JavaScript technology specification, all of todays mainstream browsers support it. The subtle differences with the JavaScript technology and CSS support among current generation browsers such as Firefox, Internet Explorer, and Safari are manageable. If you are required to support older browsers, AJAX may not be the answer for you. ——
AJAX并不是新事物、数年前使用微软IE浏览器的开发者们就在使用它、直到最近、这项技术被冠以远程脚本而更出名了。以前WEB开发者使用插件、applets或者隐藏框架来仿效这种交互模式。但是现
在由于XMLHttpRequest对象技术已经在各大平台的主流浏览器上普及、所以这种模仿的情况越来越少了。真正神奇的事情在于JavaScript和XMLHttpRequest对象技术的结合、尽管XMLHttpRequest对象
技术并不属于JavaScript标准规范、但是现代的主流浏览器都支持、在Firefox、IE、 Safari这些浏览器对JavaScript和CSS的执行有微小不同、但这些不同之处很容易理解和处理。如果你一定要兼容老
一代浏览器那还是不要用AJAX了。
//////////////////////////////////////////////////////////////////////////////////////////
1楼
BS 娄猪
2楼
What makes AJAX-based clients unique is that the client contains page-
specific control logic embedded as JavaScript technology. The page interacts with the JavaScript technology based on events such as the document being loaded, a mouse click, focus changes, or even a timer. AJAX interactions allow for a clear separation of presentation logic from the data. An HTML page can pull in bite-
size pieces of data as needed rather than reloading the whole page every time a change needs to be displayed. AJAX will require a different server-
side architecture to support this interaction model. Traditionally, server-
side web applications have focused on generating HTML documents for every client event resulting in a call to the server. The clients would then refresh and re-
render the complete HTML page for each response. Rich web applications focus on a client fetching an HTML document that acts as a template or container into which to inject content, based on client events using XML data retrieved from a server-
side component.——是什么使得基于AJAX的客户端如此独特?那就是客户端所嵌入的页面特定的控制逻辑、这些控制逻辑以JavaScript代码的形式存在。页面与JavaScript的交互建立在事件的基础上
(比如文档加载事件、鼠标点击事件、焦点变化事件、甚至一个时钟事件)。AJAX清楚的将表示逻辑和数据分离开来、一个HTML网页能够在需要的时候获取以比特为单位的数据片段!这不同于以前的
有一点变化就必须刷新整个页面的做法。同时AJAX也需要一种完全不同的服务端架构来支持这种交互模式。以前的传统服务端WEB应用专注于为每一个客户端的每一次请求生成HTML页面、每一次客户
端接到响应都要刷新和重新渲染整个页面。而我们所谈的WEB应用专注于客户端把HTML文档当作模版或容器、客户端向这个容器中插入内容、作到这些的原理就是每当客户端发生事件、客户端都可以
向服务端发出请求并使用服务端返回的XML数据。
Some uses for AJAX interactions are the following:
Real-Time Form Data Validation: Form data such as user IDs, serial numbers, postal codes, or even special coupon codes that require server-
side validation can be validated in a form before the user submits a form.
Autocompletion: A specific portion of form data such as an email address, name, or city name may be autocompleted as the user types.
Master Details Operations: Based on a client event, an HTML page can fetch more detailed information on data such as a product listing that enables the client to view the individual product information without refreshing the page.
Sophisticated User Interface Controls: Controls such as tree controls, menus, and progress bars may be provided that do not require page refreshes.
Refreshing Data on the Page: HTML pages may poll data from a server for up-to-date data such as scores, stock quotes, weather, or application-specific data.
——一些AJAX的使用例子如下:
实时的表单数据验证:象ID号、序列号、邮政编码、优惠卷号码这样的表单数据可以在用户提交整个表单之前就得到验证!
自动补全:一些特定表单数据比如email、姓名、城市名可以根据用户的类型自动填写。
细节操作:基于客户端事件、客户可以不刷新页面就可以查看某一种产品的细节信息。
智能客户端接口控制:诸如树、菜单、工具条都可以在不刷新页面的情况下随时提供服务。
刷新实时数据:页面可以从服务端筛选动态数据并显示、诸如:比分、股票报价、天气预报等。
This list is not all-
inclusive, but it shows that AJAX interactions allow web applications to do much more than they have done in the past. Although many of these benefits are noteworthy, this approach has some drawbacks as well:
Complexity: Server-side developers will need to understand that presentation logic will be required in the HTML client pages as well as in the server-
side logic to generate the XML content needed by the client HTML pages. HTML page developers must have JavaScript technology skills. Creating AJAX-
enabled applications will become easier as new frameworks are created and existing frameworks evolve to support the interaction model.
Standardization of the XMLHttpRequest Object: The XMLHttpRequest object is not yet part of the JavaScript technology specification, which means that the behavior may vary depending on the client.
JavaScript Technology Implementations: AJAX interactions depend heavily on JavaScript technology, which has subtle differences depending on the client. See QuirksMode.org for more details on browser-
specific differences.
Debugging: AJAX applications are also difficult to debug because the processing logic is embedded both in the client and on the server.
Viewable Source: The client-side JavaScript technology may be viewed simply by selecting View Source from an AJAX-enabled HTML page. A poorly designed AJAX-
based application could open itself up to hackers or plagiarism.
Frameworks and patterns for AJAX technology are likely to emerge as developers gain more experience writing applications that use the AJAX interaction model. It is still early to focus on a one-
size-fits-
all framework for AJAX interactions. This article and the associated solutions focus on how AJAX interactions can be supported today by existing Java 2 Platform, Enterprise Edition (J2EE) technologies such as servlets, JavaServer Pages (JSP) software, JavaServer Faces applications, and the Java Standard Tag Libraries (JSTL).
——这些用处并不全、但是它们说明了AJAX交互使得WEB应用可以作到大量以前无法作到的事情。这些事情可能并不多么引人注目、而且还有一些缺陷:
复杂性:服务端开发者必须掌握XML、页面开发者必须掌握JavaScript技术。
XMLHttpRequest对象的标准化:XMLHttpRequest对象仍不是JavaScript标准规范。
JavaScript技术实现:AJAX严重依赖与JavaScript技术。可参考QuirksMode.org。
Debugging:AJAX难于调试。
查看原文件:使用这种方法可以容易的看到JavaScript源码、给黑客、剽窃者以可乘之机。
AJAX技术框架和模式还在探索和经验形成阶段、固化形成一个真正的框架还为时过早。本文只是探讨如何使用J2EE技术来支持AJAX交互。
The Anatomy of an AJAX Interaction
Now that we have discussed what AJAX is and what some higher-level issues are, lets put all the pieces together and show an AJAX- enabled J2EE application.
Lets consider an example. A web application contains a static HTML page, or an HTML page generated in JSP technology contains an HTML form that requires server-
side logic to validate form data without refreshing the page. A server-
side web component (servlet) named ValidateServlet will provide the validation logic. Figure 1 describes the details of the AJAX interaction that will provide the validation logic
——AJAX交互剖析:来看一个AJAX使能的J2EE应用:名为ValidateServlet的Servlet验证表单数据、但是要求不能对整个页面刷新。步骤如下:
1、A client event occurs. ——客户端事件发生。
2、An XMLHttpRequest object is created and configured. ——一个XMLHttpRequest对象被创建并配置。
3、The XMLHttpRequest object makes a call. ——XMLHttpRequest对象发出请求。
4、The request is processed by the ValidateServlet. ——请求被ValidateServlet处理。
5、The ValidateServlet returns an XML document containing the result. ——ValidateServlet返回一个包含结果的XML文档。
6、The XMLHttpRequest object calls the callback() function and processes the result. ——XMLHttpRequest对象调用callback()函数并处理结果。
7、The HTML DOM is updated.——HTML文档对象被更新。
3楼