Spring 接收表单List集合数据
4种方法,详情见:http://blog.csdn.net/lutinghuan/article/details/46820023
第一种:
1.新增类,其中有属性为该对象的集合
public class SettingModel { private List<Setting> sets; public final List<Setting> getSets() { return sets; } public final void setSets(List<Setting> sets) { this.sets = sets; } @Override public String toString() { return "SettingModel [sets=" + sets + "]"; } }
2.表单字段name属性值为数组形式:
<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>
3.后台接收:
@RequestMapping("/update") public void update(SettingModel users){
System.out.println(sets);
}
第二种:
1.ajax请求,POST请求、'Content-Type' : 'application/json;charset=utf-8'、data:json(例如:[{"id":"1","content":"14"},{"id":"2","content":"测试修改2"}])
$.ajax({ url:'/update', type:'POST', headers : { 'Content-Type' : 'application/json;charset=utf-8' }, dataType:"json", data:datastr,//datastr=JSON.stringify(datastr);转换为JSON格式 success: function(data){ alert('111'); } });
2.后台接收:
@RequestMapping(value="/update") public void update(@RequestBody List<Setting> sets){ System.out.println(sets); }