在 struts中的ajax
来源:互联网 发布:巴洛特利 fifa15数据 编辑:程序博客网 时间:2024/05/21 10:03
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.
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.
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.
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 Action fragment
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.
Struts 2 Stream result Action
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
<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.
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:
•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.
Ajax Plugins
While Struts works fine with Ajax out-of-the-box, for added value, several Ajax-centric plugins are available.
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.
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 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:
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.
Next: Dependency Injection
Comments (Hide)
encoding
Struts 2 Configuring
<result type="stream">
<param name="contentType">text/html; charset=utf-8</param>
<param name="inputName">inputStream</param>
</result>
Struts 2 Stream result Action
public String execute() throws Exception
Unknown macro: { inputStream = new StringBufferInputStream( new String("中文".getBytes("UTF-8"), "ISO-8859-1") ); return SUCCESS; }
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.
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.
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 Action fragment
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.
Struts 2 Stream result Action
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
<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.
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:
•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.
Ajax Plugins
While Struts works fine with Ajax out-of-the-box, for added value, several Ajax-centric plugins are available.
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.
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 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:
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.
Next: Dependency Injection
Comments (Hide)
encoding
Struts 2 Configuring
<result type="stream">
<param name="contentType">text/html; charset=utf-8</param>
<param name="inputName">inputStream</param>
</result>
Struts 2 Stream result Action
public String execute() throws Exception
Unknown macro: { inputStream = new StringBufferInputStream( new String("中文".getBytes("UTF-8"), "ISO-8859-1") ); return SUCCESS; }
- 在 struts中的ajax
- Struts中的Ajax请求
- Ajax 在Struts 2中的三种实现方式
- 在Struts中应用AJAX
- 在struts中,通过Ajax,利用jQuery,将action中的数据转到前台页面
- Struts+Ajax+JSP中的中文乱码问题
- 如何在Struts中使用Ajax
- 在Struts应用中使用AJAX
- 在Struts应用中使用Ajax
- 在struts中处理AJAX请求
- 在Struts应用中施展Ajax魔法
- 在Struts应用中施展Ajax魔法
- 如何在Struts中使用Ajax
- 如何在Struts中使用Ajax
- 如何在Struts中使用Ajax
- 如何在Struts中使用Ajax
- 如何在Struts中使用Ajax
- 如何在Struts中使用Ajax
- uclinux内核的对象表示:kobjects
- vs.php for visual studio 2005错误解决
- 杨澜说女孩应该这样:
- SQL注入内容更新.
- js报表效果
- 在 struts中的ajax
- 2009-04-24单词学习
- Linux系统网络配置详细说明
- 企业网站为什么要选择搜索引擎优化
- 通过更改字库文件组件及相关设置实现对NK进行裁剪(尤其适用中文简体系统)
- 利用 Microsoft 的 HTML 分析器来获得 Web 站点的数据
- route命令的用法简介(windows)
- 最近被骂的厉害。。
- 线程---几个网址的资料比较经典