码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC之接收参数总结

时间:2019-02-15 21:15:17      阅读:232      评论:0      收藏:0      [点我收藏+]

标签: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());
        }
    }

 

SpringMVC之接收参数总结

标签:result   ESS   setname   public   ping   ati   this   content   style   

原文地址:https://www.cnblogs.com/zengnansheng/p/10385893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!