springmvc 集合参数

第一种情况:数据是基本类型或者String
1, 直接用表单提交,参数名称相同即可;
Controller参数定义为数组类型即可.不要定义为List<String>
<form action="${pageContext.request.contextPath}/dashboard/xxx" method="post">
	<input type="text" name="xxx"/><br>
	<input type="text" name="xxx"/><br>
	<input type="text" name="xxx"/><br>
	<input type="text" name="xxx"/><br>
	<input type="submit" value="tijiao"/>
</form>


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

Controller中不要用List<String>接收,不然会报下面这个错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, 表单提交
<form action="/user/submitUserList_2" method="post">
        ID:<input type="text" name="users[0].id"><br/>
        Username:<input type="text" name="users[0].name"><br/>
        Password:<input type="text" name="users[0].pwd"><br/><br/>

        ID:<input type="text" name="users[2].id"><br/>
        Username:<input type="text" name="users[2].name"><br/>
        Password:<input type="text" name="users[2].pwd"><br/><br/>
        <input type="submit" value="Submit">
    </form>


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

    public List<User> getUsers() {
        return users;
    }

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

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

    public UserModel() {
        super();
    }

}
    @RequestMapping(value = "/xxx")
    public void xxx(UserModel users) {
        List<User> 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<User> xxx) {
        System.out.println(xxx);
    }

这里可以定义成List<User>, 不会报异常.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,否则取不到值.

猜你喜欢

转载自18810098265.iteye.com/blog/2380269