直接在方法上使用List<Bean>报不能实例化错误!
必须将List<Bean>放在某个对象里作为属性方可接收。具体见如下例子
前端代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>page</title>
</head>
<body>
<h1>数组传参数测试</h1>
<script src="https://www.linuxidc.com/webpagetest/js/jquery-2.0.3.min.js"></script>
<script type="text/javascript">
function test() {
$.ajax({
url:'/webpagetest/pageData',
data:{
'id':1,
'order[0].column':'name',
'order[0].dir':'asc',
'order[1].column':'phone',
'order[1].dir':'desc'
},
success:function (rt) {
alert(rt);
},
error:function () {
alert('error');
}
});
}
function test2() {
$.ajax({
url:'/webpagetest/pageData2',
data:{
'id':1,
'order[0][column]':'name',
'order[0][dir]':'asc',
'order[1][column]':'phone',
'order[1][dir]':'desc'
},
success:function (rt) {
alert(rt);
},
error:function () {
alert('error');
}
});
}
</script>
<button>测试</button>
<button>测试2</button>
</body>
</html>
后台代码
@RequestMapping("/pageData")
@ResponseBody
public String index(@RequestParam(value="order[0].column",required = false) String order0column,
HttpServletRequest request,
MyVO vo){
System.out.println("获取单个参数:");
System.out.println(order0column);
System.out.println("获取所有参数:");
Map<String,String[]> params = request.getParameterMap();
for(Map.Entry<String,String[]> entry:params.entrySet()){
System.out.println("key:"+entry.getKey()+" value:"+ Arrays.asList(entry.getValue()));
}
System.out.println("获取VO");
System.out.println("id:"+vo.getId());
System.out.println("orders:");
for(Order o:vo.getOrder()) {
System.out.println("column:" + o.getColumn()+" dir:"+o.getDir());
}
return "ok";
}
@RequestMapping("/pageData2")
@ResponseBody
public String index(@RequestParam(value="order[0][column]",required = false) String order0column,
HttpServletRequest request /*,
MyVO2 vo2*/){
System.out.println("获取单个参数:");
System.out.println(order0column);
System.out.println("获取所有参数:");
Map<String,String[]> params = request.getParameterMap();
for(Map.Entry<String,String[]> entry:params.entrySet()){
System.out.println("key:"+entry.getKey()+" value:"+ Arrays.asList(entry.getValue()));
}