jsp代碼:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery.js" ></script>
<script type="text/javascript" >
$(document).ready(function(){
$("#submit").click(function(){
var t = $("#check").val();
var time = new Date();
$.ajax({
cache:false,
url :'JsonPlugin.action', //后台处理程序
type:'post', //数据发送方式
cache:false,
dataType:'json', //接受数据格式
data:{check:t,t:time}, //要传递的数据
success:update_page //回传函数(这里是函数名)
});
});
//ajax提交
$("#loading").ajaxStart(function(){
$(this).html("<img src=\"loader.gif\"> 正在加载");
alert(new Date());
});
//ajax结束状态
$("#loading").ajaxStop(function(){
$(this).html("");
});
});
function update_page (json ) { //回传函数实体,参数为XMLhttpRequest.responseText
var str= "ISBN="+json.ISBN + ",check:" + json.check + ",title="+json.title
+ ",list=" + json.comments[2];
$("#bookHolder").html(str);
}
</script>
</head>
<body>
<div id="loading"></div>
<input id="check" type="text" >
<input id="submit" type="button" value="Retrieve Book" />
<div id="bookHolder"></div>
</body>
</html>
action代碼:
package tutorial;
import java.net.URLEncoder;
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;
private String check ;
@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() throws Exception{
if(check!=null){
System.out.println("check:"+URLEncoder.encode(check,"utf-8"));
}
bookId = 15645912;
title = "Max On 驕傲va";
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;
}
public void setCheck(String check) {
this.check = check;
}
public String getCheck() {
return check;
}
}
struts.xml代碼:
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="Struts2_AJAX_DEMO" extends="json-default"> <action name="JsonPlugin" class="tutorial.JsonPluginAction"> <result type="json" /> </action> </package> </struts>