标签:result ESS setname public ping ati this content style
新建3个实体类
public class A { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class B { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class C { private int cid; private A a; private ArrayList<B> listb; public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public A getA() { return a; } public void setA(A a) { this.a = a; } public ArrayList<B> getListb() { return listb; } public void setListb(ArrayList<B> listb) { this.listb = listb; } }
一 表单方式
var data = { id : 10, name : "zns" } $.ajax({ url : "http://localhost/test", type : "post", //get方式也行 data : data, //直接用JSON对象 success : function(result) { } }); @RequestMapping("/test") public void test(Integer id,String name){ System.out.println(id); System.out.println(name); }
var data = { id : 10, name : "zns" } $.ajax({ url : "http://localhost/test", type : "post", //get方式也行 data : data, //直接用JSON对象 success : function(result) { } }); @RequestMapping("/test") public void test(A a){ System.out.println(a.getId()); System.out.println(a.getName()); }
二 json方式
var data = { id : 10, name : "zns" } $.ajax({ url : "http://localhost/test", type : "post",//用get不行 data : JSON.stringify(data), //转JSON字符串 dataType : ‘json‘, contentType : ‘application/json;charset=UTF-8‘, //contentType很重要 success : function(result) { } }); @RequestMapping("/test") public void test(@RequestBody A a){ System.out.println(a.getId()); System.out.println(a.getName()); }
var data = new Array(); data.push({id: 1, name: "张三"}); data.push({id: 2, name: "李四"}); $.ajax({ url : "http://localhost/test", type : "post",//用get不行 data : JSON.stringify(data), //转JSON字符串 dataType : ‘json‘, contentType : ‘application/json;charset=UTF-8‘, //contentType很重要 success : function(result) { } }); @RequestMapping("/test") public void test(@RequestBody List<A> list){ for (A a : list) { System.out.println(a.getId()); System.out.println(a.getName()); } }
var a = { id : 1, name : "zns" } var list_b = new Array(); list_b.push({ id : 1, name : "张三" }); list_b.push({ id : 2, name : "李四" }); var data = { cid : 10, a : a, listb : list_b } $.ajax({ url : "http://localhost/test", type : "post",//get不行 data : JSON.stringify(data), //转JSON字符串 dataType : ‘json‘, contentType : ‘application/json;charset=UTF-8‘, //contentType很重要 success : function(result) { } }); @RequestMapping("/test") public void Test(@RequestBody C c) { System.out.println(c.getCid()); System.out.println(c.getA().getId() + "-" + c.getA().getName()); for (B b : c.getListb()) { System.out.println(b.getId() + "-" + b.getName()); } }
标签:result ESS setname public ping ati this content style
原文地址:https://www.cnblogs.com/zengnansheng/p/10385893.html