一个简单json数据提交实例
生活随笔
收集整理的這篇文章主要介紹了
一个简单json数据提交实例
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.客戶(hù)端編程:jsp頁(yè)面
<%@ 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.服務(wù)端編程:用到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"
? ? ? ?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.服務(wù)端編程:用到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;
? ? ? ?}
}
總結(jié)
以上是生活随笔為你收集整理的一个简单json数据提交实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: HyperlinkButton——WP8
- 下一篇: Fragment实现的底部导航