后台
@RequestMapping("/aaa")
@ResponseBody
public Object aaa(RequestBody List<SysUser> syss){
SysDepart model= sysDepartService.findById(guid);
return model;
}
前台
var users = new Array();
var user = {};
user.userName="aa";
user.userGuid="1";
users.push(user);
var user1 = {};
user1.userName="aa";
user1.userGuid="1";
users.push(user1);
var result=JSON.stringify(users);
$.ajax({
type : 'POST',
url : '${base}/aaa.do?guid='+ guid,
dataType : 'json',
contentType:"application/json",
data:result,
success : function(data) {
}
});
参考:http://blog.csdn.net/z69183787/article/details/41653843
http://jxd-zxf.iteye.com/blog/2072300
第二种方式
<div class="main_con">
<form id="frm_test">
<input type="hidden" name="departGuid" value="110" />
<input name="departName" type="hidden" value="112"/>
<input type="text" name="lsUsers0].userGuid" value="SONY"><br>
<input type="text" name="lsUsers[1].userGuid" value="MOTO"><br>
<input type="text" name="lsUsers[2].userGuid" value="LG"><br>
</form>
var form = $("#frm_test");
var baseObj = form.serializeObject();
$.ajax({
type:'POST',
url : '${base}/aaa.do?guid='+ guid,
dataType : 'json',
data:baseObj,
success : function(data) {
}
});
@RequestMapping("/queryDepartByGuid")
@ResponseBody
public Object aaa(String guid,SysDepart depart){
SysDepart model= sysDepartService.findById(guid);
return model;
}
SysDepart类中有 List<SysUser> lsUses; 字段
第三种方式
在第二种方式的基础上修改
html那一段
这一段 换成这样也可以,不用表单
var ooo= {"departGuid": "110", "departName": "112",
"lsUsers[0].userGuid": "SONY",
"lsUsers[1].userGuid": "MOTO", "lsUsers[2].userGuid": "LG"};
传 ooo对象到后台。
注意 用数组渲染的方式是不对的,调试可以发现,表单输出力的 lsUsers不是数组 而是 整个 lsUsers[0].userGuid 作为 object的属性名
如下是不行的:
var users = new Array();
var user2 = {};
user2.userGuid="1";
var user1 = {};
user1.userGuid="12";
var depart={};
depart.departGuid="sss";
depart.lsUsers=[];
depart.lsUsers.push(user1);
depart.lsUsers.push(user2);
user1.userGuid="123";
lsUsers.push(user1);
user2.userGuid="12222";
lsUsers.push(user2);
var result = parseParam(lsUsers, "lsUsers", 0);//明细
var formObj = form.serialize();
type:'POST',
url : '${base}/aaa.do?guid='+ guid,
dataType : 'json',
data:formObj+"&"+result,
success : function(data) {
}
});
<form id="frm_test">
<input type="hidden" name="departGuid" value="110" />
<input name="departName" type="hidden" value="112"/>
</form>
data:{guid:'3333'},