springmvc 集合参数

阅读更多
第一种情况:数据是基本类型或者String
1, 直接用表单提交,参数名称相同即可;
Controller参数定义为数组类型即可.不要定义为List






 @RequestMapping(value = "/xxx")
    public void xxx(String[] xxx) {
        System.out.println(xxx);
    }

Controller中不要用List接收,不然会报下面这个错org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface

2, ajax请求:
$.ajax({
        url: "${pageContext.request.contextPath}/dashboard/xxx",
        type: "POST",
        contentType : 'application/json;charset=utf-8', //请求头一定要有
        dataType:"json",
        data: JSON.stringify(["aaa","bbb"]), //将Json对象转成Json字符串
        success: function(data){
            alert(data);
        },
        error: function(res){
            alert(res.responseText);
        }
    });


    @RequestMapping(value = "/xxx")
    public void xxx(@RequestBody String[] xxx) {
        System.out.println(xxx);
    }

ajax请求时请求头contentType 申明为application/json;charset=utf-8,且type 为post, 这样请求体在body中且请求体为一个json字符串, controller中获取参数一定要加@RequestBody,表示取请求体中的所有的内容,然后转为String[], 至于形参名xxx可以任意命名,因为application/json;charset=utf-8的请求体中无参数名称


第二种情况:数据是自定义对象
public class User {

    private Integer id;
    private String name;
    private String pwd;

    //getter、setter省略
 }


1, 表单提交
ID:
Username:
Password:

ID:
Username:
Password:



除了刚才公用的User类,还要封装一个User的容器类UserModel:
public class UserModel {
    private List users;

    public List getUsers() {
        return users;
    }

    public void setUsers(List users) {
        this.users = users;
    }

    public UserModel(List users) {
        super();
        this.users = users;
    }

    public UserModel() {
        super();
    }

}
    @RequestMapping(value = "/xxx")
    public void xxx(UserModel users) {
        List userList = users.getUsers();
    }


2, ajax 请求
var arr = new Array();
arr.push({id: "1", name: "李四", pwd: "123"});
arr.push({id: "2", name: "张三", pwd: "332"});

$.ajax({
        url: "${pageContext.request.contextPath}/dashboard/xxx",
        type: "POST",
        contentType : 'application/json;charset=utf-8', //请求头一定要加
        dataType:"json",
        data: JSON.stringify(arr),    //将Json对象序列化成Json字符串
        success: function(data){
            alert(data);
        },
        error: function(res){
            alert(res.responseText);
        }
    });

    @RequestMapping(value = "/xxx")
    public void xxx(@RequestBody List xxx) {
        System.out.println(xxx);
    }

这里可以定义成List, 不会报异常.ajax中的contentType 和controller中的@RequestBody一定要加


第三种,适宜任何类型数据
用js将请求参数转为json字符串, 然后contentType 设置为application/x-www-form-urlencoded, 这种请求的请求体格式是name1=value1&name2=value2&...

var arr = new Array();
arr.push({id: "1", name: "李四", pwd: "123"});
arr.push({id: "2", name: "张三", pwd: "332"});

$.ajax({
        url: "${pageContext.request.contextPath}/dashboard/xxx",
        type: "POST",
        contentType : 'application/x-www-form-urlencoded', //设置请求头信息
        dataType:"json",
        data: "xxx="+JSON.stringify(customerArray), //将Json对象序列化成Json字符串
        success: function(data){
            alert(data);
        },
        error: function(res){
            alert(res.responseText);
        }
    });

@RequestMapping(value = "/xxx")
    public void xxx(String xxx) {
        System.out.println(xxx);
       //用Gson或其他json包转成对象或数组
    }

如果 contentType 设置为 application/json;charset=utf-8, controller参数要加@RequestBody,否则取不到值.

你可能感兴趣的:(springmvc,集合参数)