1.客户端编程: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>
</head>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<body>
<form action="">
<table>
<tr>
<td>名字</td>
<td><input type="text" id="name" name="name"/></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" id="age" name="age"/></td>
</tr>
<tr>
<td><input type="button" value="提交" οnclick="commit();"/> </td>
</tr>
</table>
<table id="ulist" border="2">
</table>
</form>
</body>
<script type="text/javascript">
function commit(){
$.ajax(
{type : "post",
data:{name:$('#name').val(),age:$('#age').val()},
url : "testJson_testJson.action",
dataType : "JSON",
success : callback
}
);
}
function callback(data){
var json =
JSON.parse(data);
alert("fdf");
var ulist =
$("#ulist");
$.each(json, function(i,item){
ulist.append(
"<tr><td>"+item.name+"</td><td>"+item.age+"</td></tr>"
);
})
}
</script>
</html>
2.服务端编程:用到sturst2
public class Person {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
public class TestJsonAction {
private static final long serialVersionUID = -3571998877536556903L;
public String testJson() throws Exception {
Person p1 = new Person();
p1.setName("nn");
p1.setAge("11");
Person p2 = new Person();
p2.setName("gg");
p2.setAge("12");
Person p3 = new Person();
p3.setName("rr");
p3.setAge("24");
List<Person> ulist = new ArrayList<Person>();
ulist.add(p1);
ulist.add(p2);
ulist.add(p3);
String name =ServletActionContext.getRequest().getParameter("name");
String age = ServletActionContext.getRequest().getParameter("age");
Person p4 = new Person();
p4.setName(name);
p4.setAge(age);
ulist.add(p4);
JSONArray json = JSONArray.fromObject(ulist);
ServletActionContext.getResponse().getWriter().print(json);
return null;
}
}
<%@ page language="java" contentType="text/html; charset=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>
</head>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<body>
<form action="">
<table>
<tr>
<td>名字</td>
<td><input type="text" id="name" name="name"/></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" id="age" name="age"/></td>
</tr>
<tr>
<td><input type="button" value="提交" οnclick="commit();"/> </td>
</tr>
</table>
<table id="ulist" border="2">
</table>
</form>
</body>
<script type="text/javascript">
function commit(){
}
function callback(data){
}
</script>
</html>
2.服务端编程:用到sturst2
public class Person {
}
public class TestJsonAction {
}