真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

spirngmvcjs傳遞復(fù)雜json參數(shù)到controller的實(shí)例

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

創(chuàng)新互聯(lián)公司主營輝南網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件開發(fā),輝南h5重慶小程序開發(fā)搭建,輝南網(wǎng)站營銷推廣歡迎輝南等地區(qū)企業(yè)咨詢

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

接收List集合參數(shù):

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 idList,Boolean isBatch) { 
   … 
 } 
} 

接收List、User[]集合參數(shù):

1、User實(shí)體類:

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', //設(shè)置請求頭信息 
 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 userList) { 
  … 
 } 
} 

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

接收List>集合參數(shù):

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', //設(shè)置請求頭信息 
 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> listMap) { 
  … 
 } 
} 

接收User(bean里面包含List)集合參數(shù):

1、User實(shí)體類:

public class User { 
 private String name; 
 private String pwd; 
 private List 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', //設(shè)置請求頭信息 
 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 customers = user.getCustomers(); 
  … 
 } 
} 

以上這篇spirngmvc js傳遞復(fù)雜json參數(shù)到controller的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持創(chuàng)新互聯(lián)。


當(dāng)前文章:spirngmvcjs傳遞復(fù)雜json參數(shù)到controller的實(shí)例
鏈接地址:http://weahome.cn/article/peepes.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部