AJAX is an acronym for Asynchronous JavaScript and XML. Essentially, a piece of JavaScript can create an HTTP request and directly update a part of the page without a post or get request like normal, and refresh the entire page. Even better, a page can contain multiple JavaScript that create synchronous (asynchronous) requests.
The key point is that when a script creates an "Ajax request" (XHR), the server does not know it comes from the script and processes it as it treats other requests. One reason Ajax is so successful is that it works well with Xianyou's server-side technology, including struts.
The difference is not AJAX requests, but Ajax responses (Ajax response). Instead of returning a complete page for the browser to display (or display), an AJAX response will only return part of the page. This response can be xml,html, plain text, another script, or anything else that is desired by any script that executes the call.
Both Struts1 and Struts2 can return any type of response. We are not limited to the forwarding Server page. In struts 1, you can do something like this:
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out.println ("Hello world! This is the
AJAX response from a Struts Action. Out.flush ();
return null;
In Struts 2, we can do the same thing with stream results.
There's a simpler way.
Using struts 2 plug-ins (such as JSON plug-ins, JQuery plug-ins, and so on) is universal and recommended to write responses that come directly from the action. Please read the section below to find out the details. Struts 2 result Action
Package actions;
<b>
import Java.io.InputStream;
Import Java.io.StringBufferInputStream;
Import Com.opensymphony.xwork2.ActionSupport;
<b> public
class Textresult extends actionsupport{
private InputStream inputstream;
Public InputStream getInputStream () {return
inputstream;
}
<b> public
String execute () throws Exception {
InputStream = Newbytearrayinputstream ("Hello world! This is a text string response from a Struts Action.
Return SUCCESS
}
}
Struts 2 Configure Textresult Actions
<action name= "Text-result" class= "actions. Textresult ">
<result type=" Stream ">
<param name=" ContentType ">text/html</param>
<param name= "InputName" >inputStream</param>
</result>
</action>
Struts2 can also use Plug-ins to return a JSON response.
On the client side, there are two basic strategies, namely, blending and matching.
Search first, you can use a JSP tag type. Here, you don't need to know very much about Ajax or JavaScript. Taglib has done all the work, you just need to point out how to use Tablib. Standard Struts 2 taglib includes several Ajax JSP tags, as well as a large number of available Third-party libraries, including: Ajax Tags ajaxparts taglib coldtags Suite Prize Tags json-taglib
You can use a typical old-style Ajax form on an ordinary old-style HTML page by using a dojo, JQUERY, or Yui, as well as a streamresult or JSON plugin. Here, the sky is the limit, but you actually need to have JavaScript as a language to do some learning. Ajax plug-ins
Although struts can plug and play with Ajax, there are several Ajax-centric Plug-ins available for added benefit. JQuery Ajax Parts Dojo YUI other Ajax plugins Ajax File Upload GWT JSON Ajax Results with JSP
Although server-side pages are most often used to generate HTML, we can use server-side pages to create other types of data flows. Here is 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 ("It is:" +book.tojsonstring ());
}
%>
In the example code above, we use SYSTEM.OUT.PRINTLN to return a JSON data stream as a response. For more information on this technology, you can view the document to build AJAX applications using Dojo and JSON (English tutorial)