這篇文章將為大家詳細(xì)講解有關(guān)怎么在SpringMVC中接收復(fù)雜的集合對象,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
為雅安等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及雅安網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、雅安網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
1、頁面js代碼:
Js代碼
var idList = new Array(); idList.push(“1”); idList.push(“2”); idList.push(“3”); var isBatch = false; $.ajax({ type: "POST", url: "<%=path%>/catalog.do?fn=deleteCatalogSchemes", dataType: 'json', data: {"idList":idList,"isBatch":isBatch}, success: function(data){ … }, error: function(res){ … } });
2、Controller方法:
Java代碼
@Controller @RequestMapping("/catalog.do") public class CatalogController { @RequestMapping(params = "fn=deleteCatalogSchemes") @ResponseBody public AjaxJson deleteCatalogSchemes(@RequestParam("idList[]") ListidList,Boolean isBatch) { … } }
接收List
1、User實(shí)體類:
Java代碼
public class User { private String name; private String pwd; //省略getter/setter }
2、頁面js代碼:
Js代碼
var userList = new Array(); userList.push({name: "李四",pwd: "123"}); userList.push({name: "張三",pwd: "332"}); $.ajax({ type: "POST", url: "<%=path%>/catalog.do?fn=saveUsers", data: JSON.stringify(userList),//將對象序列化成JSON字符串 dataType:"json", contentType : 'application/json;charset=utf-8', //設(shè)置請求頭信息 success: function(data){ … }, error: function(res){ … } });
3、Controller方法:
Java代碼
@Controller @RequestMapping("/catalog.do") public class CatalogController { @RequestMapping(params = "fn=saveUsers") @ResponseBody public AjaxJson saveUsers(@RequestBody ListuserList) { … } }
如果想要接收User[]數(shù)組,只需要把saveUsers的參數(shù)類型改為@RequestBodyUser[]userArray就行了。
接收List
1、頁面js代碼(不需要User對象了):
Js代碼
var userList = new Array(); userList.push({name: "李四",pwd: "123"}); userList.push({name: "張三",pwd: "332"}); $.ajax({ type: "POST", url: "<%=path%>/catalog.do?fn=saveUsers", data: JSON.stringify(userList),//將對象序列化成JSON字符串 dataType:"json", contentType : 'application/json;charset=utf-8', //設(shè)置請求頭信息 success: function(data){ … }, error: function(res){ … } });
2、Controller方法:
Java代碼
@Controller @RequestMapping("/catalog.do") public class CatalogController { @RequestMapping(params = "fn=saveUsers") @ResponseBody public AjaxJson saveUsers(@RequestBody List
接收User(bean里面包含List)集合參數(shù):
1、User實(shí)體類:
Java代碼
public class User { private String name; private String pwd; private Listcustomers;//屬于用戶的客戶群 //省略getter/setter }
2、頁面js代碼:
Js代碼
var customerArray = new Array(); customerArray.push({name: "李四",pwd: "123"}); customerArray.push({name: "張三",pwd: "332"}); var user = {}; user.name = "李剛"; user.pwd = "888"; user. customers = customerArray; $.ajax({ type: "POST", url: "<%=path%>/catalog.do?fn=saveUsers", data: JSON.stringify(user),//將對象序列化成JSON字符串 dataType:"json", contentType : 'application/json;charset=utf-8', //設(shè)置請求頭信息 success: function(data){ … }, error: function(res){ … } });
3、Controller方法:
Java代碼
@Controller @RequestMapping("/catalog.do") public class CatalogController { @RequestMapping(params = "fn=saveUsers") @ResponseBody public AjaxJson saveUsers(@RequestBody User user) { Listcustomers = user.getCustomers(); … } }
關(guān)于怎么在SpringMVC中接收復(fù)雜的集合對象就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。