AJAX

AJAX is an acronym for Asynchronous JavaScript and XML. Essentially, a JavaScript can make a HTTP request and update portions of a page directly, without going through a conventional POST or GET and refreshing the entire page. Better yet, a page can contain several JavaScripts making simultaneous (asynchronous) requests.

Ajax是异步JavaScript和XML的缩写,实际上, 一个JavaScript可以产生一个HTTP请求,并直接更新部分页面,不需要离开页面就可以通过传统的POST或GET刷新整个页面,更重要的是,一个页面可以包含几个JavaScript发送同步(异步)请求

The key point is that when a script makes an "Ajax request" (XHR), the server doesn't know it came from a script, and handles it like any other request. One reason Ajax is so successful is that it works just fine with existing server technologies, including Struts.

关键的一点是,一个脚本发送一个Ajax请求(XHR),这个服务器不知道它来自一个脚本,像处理任何其他请求一样处理它,Ajax一个如此成功的原因是它很好地使用现有的服务器技术工作,包括Struts。

It's not the Ajax request that is different, but the Ajax response. Instead of returning an entire page for the browser to display (or redisplay), an Ajax response will just return a portion of a page. The response can take the form of XML, or HTML, or plain text, another script, or whatever else the calling script may want.

它的Ajax请求不同,但Ajax响应,而不是返回整个页面的浏览器来显示(或重新显示),一个Ajax响应将只返回一个页面的一部分,相应可以采取XML格式或HTML或纯文本,另一个脚本,或任何其他可能需要调用的脚本。

Both Struts 1 and Struts 2 can return any type of response. We are not limited to forwarding to a server page. In Struts 1, you can just do something like:

Struts 1和Struts 2都可以返回任何类型的响应, 我们不局限于转发到服务器页面,
在Struts1,你可以这么做(如下)

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hello World!  This is an AJAX response from a Struts Action.");
out.flush();
return null;

In Struts 2, we can do the same thing with a Stream result.

在Struts2,我们可以做同样的事情包括一个结果流

Struts 2 Stream result Action(Struts2结果流的Action)s
package actions;

import java.io.InputStream;
import java.io.StringBufferInputStream;
import com.opensymphony.xwork2.ActionSupport;

public class TextResult extends ActionSupport  {
    private InputStream inputStream;
    public InputStream getInputStream() {
        return inputStream;
    }

    public String execute() throws Exception {
inputStream = new StringBufferInputStream("Hello World! This is a text string response from a Struts 2 Action.");
        return SUCCESS;
    }
}

Struts 2 Configuring the TextResult Action(Struts 2的配置TextResult Action)

<action name="text-result" class="actions.TextResult">
    <result type="stream">
        <param name="contentType">text/html</param>
        <param name="inputName">inputStream</param>
    </result>
</action>


Struts 2 can also return a JSON (JavaScript Object Notation) response, using a plugin.

Struts 2的也可以返回一个JSON(JavaScript对象符号)响应,使用一个插件。

On the client side, there are two basic strategies, which can be mixed and matched.

在客户端,有两个基本的策略, 它可以混合和匹配

First, you can use some type of JSP tag. Here, you don't have to know very much at all about Ajax or JavaScript. The taglib does all the work, and you just have to figure out how to use the taglib. The standard Struts 2 taglib includes several Ajax JSP tags, and many third-party libraries are available, including:

第一,你可以使用一些JSP的标签类型,在这里,你不必知道很多的关于Ajax和JavaScript,这个标签可以代替所有的工作,你只要弄清楚如何使用标签库, 标准的Struts 2的标签库包括几个Ajax的JSP标签,和许多可利用的第三方库,包含:

•Ajax Tags
•AjaxParts Taglib
•ColdTags Suite
•Prize Tags
•JSON-taglib
Alternatively, you can use a plain-old Ajax widget on a plain-old HTML page, using libraries like Dojo, JQuery, or YUI, and the StreamResult or the JSON Plugin. Here, the sky's the limit, but you actually have to learn something about JavaScript as a language.

另外,你可以在纯HTML页面上使用Ajax的小部件,使用像Dojo库,JQuery,或YUI,和结果流或JSON插件,在这里,前途无量, 但实际上你要作为JavaScript的一种语言来学习。

Ajax Plugins(Ajax插件)
While Struts works fine with Ajax out-of-the-box, for added value, several Ajax-centric plugins are available.

而Struts工作和Ajax拆盒即可使用的,以增加价值,几个以Ajax为中心的插件可有效的利用

Ajax Tag Plugins
• Ajax Parts - The AjaxParts Taglib (APT) is a component of the Java Web Parts (JWP) project (http://javawebparts.sourceforge.net) that allows for 100% declarative (read: no Javascript coding required!) AJAX functionality within a Java-based webapp.
• Dojo - The Ajax Tags Dojo Plugin was represented as a theme for Struts 2.0. For Struts 2.1, the Dojo tags are bundled as a plugin.
• YUI - The Yahoo User Interface (YUI) Plugin has only a few tags are available so far, but the YUI tags tend to be easier to use than the Dojo versions.
• jQuery - The jQuery Plugin provide ajax functionality and UI Widgets an JavaScript Grid based on the jQuery javascript framework.

Ajax标签插件
• Ajax零部件 - 该Ajax零部件标签库(亚太)是一个Java Web部件组件(联合工作方案)项目(http://javawebparts.sourceforge.net)宣布,允许100%的声明(阅读:暂无需要使用Javascript编码)在Java的AJAX功能,基于Web应用。
• DOJO - 插件的Ajax标签DOJO的代表作为一个为Struts 2.0的主题。为Struts 2.1的Dojo标签捆绑为一个插件
• YUI -迄今为止雅虎的用户界面(锐)插件只有一些标签,但锐标签往往是易于使用的Dojo版本。
• jQuery – jQuery插件提供了Ajax功能和一个基于JavaScript网格的jQuery JavaScript框架

Other Ajax Plugins
• Ajax File Upload - With the Ajax File Upload Plugin we can upload a file to the server and asynchronously monitor its progress.
• GWT - The Google Web Toolkit Plugin exposes Struts 2 actions to the GWT RPC mechanism.
• JSON - The JSON Plugin serializes Actions properties into JSON, making it easy to respond to JavaScript requests.
See the Struts Plugin Repository for a complete list of Struts 2 plugins.

其他的Ajax插件
• Ajax文件上传 – 具有Ajax文件上传插件我们可以上传一个文件到服务器,异步监测其进展情况
• GWT -在谷歌网页工具包插件中公开Struts 2的行动为延迟绑定介绍远程过程的机制

Ajax Results with JSP
While server pages are most often used to generate HTML, we can use server pages to create other types of data streams. Here's an example:

Ajax 结果和jsp
虽然服务器网页最经常被用来生成HTML,我们可以使用服务器页面来创建其他类型的数据流, 下面是一个例子

book.jsp
<%@ page import="java.util.Iterator,
java.util.List,
com.esolaria.dojoex.Book,
com.esolaria.dojoex.BookManager" %>
<%
String bookIdStr = request.getParameter("bookId");
int bookId = (bookIdStr == null || "".equals(bookIdStr.trim()))
? 0 : Integer.parseInt(bookIdStr);
Book book = BookManager.getBook(bookId);
if (book != null) {
out.println(book.toJSONString());
System.out.println("itis: " + book.toJSONString());
}
%>
In the code example, we use System.out.println to return a JSON data stream as the response. For more about this technique, see the article Using Dojo and JSON to Build Ajax Applications.

在代码示例中,我们使用System.out.println作为响应打印一个JSON数据流作为响应, 想了解更多的技术, 参见这篇文章:使用Dojo和JSON构建Ajax应用程序

你可能感兴趣的:(JavaScript,jquery,Ajax,struts,dojo)