国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

spirngmvc js傳遞復雜json參數到controller的實例

2024-05-06 16:43:23
字體:
來源:轉載
供稿:網友

Spring MVC在接收集合請求參數時,需要在Controller方法的集合參數里前添加@RequestBody,而@RequestBody默認接收的enctype (MIME編碼)是application/json,因此發送POST請求時需要設置請求報文頭信息,否則Spring MVC在解析集合請求參數時不會自動的轉換成JSON數據再解析成相應的集合。

以下列舉接收List<String>、List<User>、List<Map<String,Object>>、User[]、User(bean里面包含List)幾種較為復雜的集合參數示例:

接收List<String>集合參數:

1、頁面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方法:

@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實體類:

public class User {   private String name;  private String pwd;  //省略getter/setter } 

2、頁面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方法:

@Controller @RequestMapping("/catalog.do") public class CatalogController {   @RequestMapping(params = "fn=saveUsers")  @ResponseBody  public AjaxJson saveUsers(@RequestBody List<User> userList) {   …  } } 

如果想要接收User[]數組,只需要把saveUsers的參數類型改為@RequestBody User[] userArray就行了。

接收List<Map<String,Object>>集合參數:

1、頁面js代碼(不需要User對象了):

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方法:

@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實體類:

public class User {  private String name;  private String pwd;  private List<User> customers;//屬于用戶的客戶群  //省略getter/setter } 

2、頁面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方法:

@Controller @RequestMapping("/catalog.do") public class CatalogController {   @RequestMapping(params = "fn=saveUsers")  @ResponseBody  public AjaxJson saveUsers(@RequestBody User user) {   List<User> customers = user.getCustomers();   …  } } 

以上這篇spirngmvc js傳遞復雜json參數到controller的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泸定县| 清远市| 田林县| 碌曲县| 垣曲县| 大港区| 油尖旺区| 惠水县| 玉田县| 都匀市| 湄潭县| 江津市| 柏乡县| 平山县| 望都县| 石渠县| 西平县| 东海县| 涞源县| 蚌埠市| 蓝田县| 南汇区| 楚雄市| 永泰县| 昂仁县| 韶山市| 饶平县| 小金县| 阆中市| 资中县| 沈丘县| 江西省| 三都| 宽城| 无棣县| 富源县| 布拖县| 自治县| 嵊州市| 铁力市| 海林市|