springmvc 集合参数

[b]第一种情况:数据是基本类型或者String[/b]
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的请求体中无参数名称


[b]第二种情况:数据是自定义对象[/b]

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一定要加


[b]第三种,适宜任何类型数据[/b]
用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,否则取不到值.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值