action先列出来
<package name="Struts2_AJAX_DEMO"
extends="json-default">
<action
name="JsonPlugin"
class="test.JsonPluginAction">
<result
type="json" />
</action>
<action
name="Autocompleter"
class="test.AutocompleterAction">
<result
type="json">
<param
name="root">names</param>
</result>
</action>
</package>
第一异步获取:
类:JsonPluginAction
package test;
import java.util.ArrayList;
import java.util.List;
import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;
public class JsonPluginAction extends ActionSupport {
private
static final long serialVersionUID = -6784977600668791997L;
private int
bookId;
private
String title;
private
double price;
private
List<String>
comments;
private
transient String secret1;
private
String secret2;
@JSON(name="ISBN")
public int
getBookId() {
return bookId;
}
public
void setBookId(int bookId) {
this.bookId = bookId;
}
public
List<String> getComments() {
return comments;
}
public
void setComments(List<String>
comments) {
this.comments = comments;
}
public
double getPrice() {
return price;
}
public
void setPrice(double price) {
this.price = price;
}
public
String getTitle() {
return title;
}
public void
setTitle(String title) {
this.title = title;
}
@Override
public
String execute() {
bookId = 15645912;
title = "Max On Java";
price = 0.9999d;
comments = new
ArrayList<String>(3);
comments.add("It's no bad!");
comments.add("WOW!");
comments.add("No comment!");
secret1 = "You can't see me!";
secret2 = "I am invisible!";
return SUCCESS;
}
}
2.页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./1999/xhtml">
<head>
<title>JSON
Plugin</title>
<script
type="text/javascript">
var
bXmlHttpSupport = (typeof XMLHttpRequest != "undefined" ||
window.ActiveXObject);
if (typeof
XMLHttpRequest == "undefined" &&
window.ActiveXObject) {
function XMLHttpRequest() {
var arrSignatures = ["MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
"Microsoft.XMLHTTP"];
for (var i=0; i < arrSignatures.length; i++) {
try {
var oRequest = new
ActiveXObject(arrSignatures[i]);
return
oRequest;
} catch (oError) { }
}
throw new Error("MSXML is not installed on your
system.");