Spring MVC处理对象数组

主要解决问题:通过jquery提供的ajax提交对象数组,后台用spring MVC进行处理。

1. 首先看一下通过ajax方式默认提交到后台的数据。

$.ajax({
			url:"<%=basePath%>productIn/saveList",
			type:"post",
			dataType:"json",
			data: data ,            
			success:function(r){
				if(r.success){
					$.messager.alert("提示","保存成功","info");
					$('#dlg').dialog('close');
					getData();
				}else{
					$.messager.alert("错误",r.msg,"error");
				}
			});

2. 用chrome查看提交到后台的数据,黑体字部分是提交到后台的对象数组的数据。


Request URL:http://localhost:8080/
Request Method:POST
Status Code:415 Unsupported Media Type
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:505
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:uid=1; JSESSIONID=73325AF6523922506FEF699CAA3C2CD5
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
id:0
code:CPRK-20140823154126
supplier:供货商
qa:质检人
inUser:入库人
productionId:单号
inTime:2014-08-13 15:42:13
productInListSize:5
testResult:测试结果
remark:备注
inNature:委外
param[0][sku]:JT14L-001_黑白_L
param[0][inCount]:11.00
param[1][sku]:JT14L-001_黑色加厚_L
param[1][inCount]:22.00
Response Headersview source
Content-Length:1048
Content-Type:text/html;charset=utf-8
Date:Sat, 23 Aug 2014 07:42:31 GMT
Server:Apache-Coyote/1.1

3. 提交到后台,后台使用spring mvc接收数据。param类带有一个list的成员变量List param。

public JSONObject saveList(@RequestBody ProductInList param){}
后台报错:

 "Property referenced in indexed property path 'param[0][inCount]' is neither an array nor a List nor a Map"

4. google后找到的解决方案

  • 在ajax中添加content-Type:"application/json",将data用JSON.stringify(data)进行转换。

$.ajax({
			url:"<%=basePath%>productIn/saveList",
			type:"post",
			dataType:"json",
			data:JSON.stringify( data ),
            		contentType:"application/json",
			success:function(r){
				if(r.success){
					$.messager.alert("提示","保存成功","info");
					$('#dlg').dialog('close');
					getData();
				}else{
					$.messager.alert("错误",r.msg,"error");
				}
			}
});

  • 浏览器提交到后台的数据

Remote Address:::1:8080
Request URL:http://localhost:8080/productIn/saveList
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:347
Content-Type:application/json
Cookie:uid=1; JSESSIONID=A120C53BBF5C681EE3B301A36609E3BA
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/views/warehouse/productIn.jsp
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview source
{id:0, code:CPRK-20140823160852, supplier:供货商, qa:质检人, inUser:入库人, productionId:单号,…}
code: "CPRK-20140823160852"
id: "0"
inNature: "采购"
inTime: "2014-08-27 16:09:15"
inUser: "入库人"
param: [{sku:JT14L-001_黑白_L, inCount:2.00}, {sku:JT14L-001_黑白_L, inCount:5.00}]
productInListSize: "5"
productionId: "单号"
qa: "质检人"
remark: "备注"
supplier: "供货商"
testResult: "测试结果"
Response Headersview source
Content-Type:application/json;charset=UTF-8
Date:Sat, 23 Aug 2014 08:09:40 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked


  • control层的方法。提交的数据可通过JSONObject接收,、数组对象,直接放到List中。

public JSONObject saveList(@RequestBody  JSONObject obj) {
		try {
            String id =(String)obj.get("id");
            String testResult=(String)obj.get("testResult");
            List list= (List)obj.get("param");}}






你可能感兴趣的:(java)