SpringMVC传递一维数组:传递数组类型时,需要在@requestParam()中添加value,
否则会出现HTTP Status 400 - Required long[] parameter 'data' is not present错误。
例如: @RequestParam(value = "data[]")long[] data
前端jsp代码:
var dataArr = new Array();
for(var i = 0; i < 10; i++){
dataArr.push(i);
}
$.ajax({
url : "test/arrayParam.shtml",
data : {
"datas" : dataArr
},
dataType : "json",
success : function(data) {
alert(data);
},
async : false
});
}
后端JAVA代码:
package com.pyc.search.view.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("/arrayParam")
public @ResponseBody
int test1(@RequestParam(value = "datas[]") long[] datas) {
return datas.length;
}
}