您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在SpringMVC中接收復雜的集合對象,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
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[]") List<String> idList,Boolean isBatch) { … } }
接收List<User>、User[]集合參數:
1、User實體類:
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', //設置請求頭信息 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 List<User> userList) { … } }
如果想要接收User[]數組,只需要把saveUsers的參數類型改為@RequestBodyUser[]userArray就行了。
接收List<Map<String,Object>>集合參數:
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', //設置請求頭信息 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<Map<String,Object>> listMap) { … } }
接收User(bean里面包含List)集合參數:
1、User實體類:
Java代碼
public class User { private String name; private String pwd; private List<User> customers;//屬于用戶的客戶群 //省略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', //設置請求頭信息 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) { List<User> customers = user.getCustomers(); … } }
關于怎么在SpringMVC中接收復雜的集合對象就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。