首頁  >  文章  >  web前端  >  js實作傳遞複雜json參數到controller

js實作傳遞複雜json參數到controller

小云云
小云云原創
2018-03-30 09:39:481600瀏覽

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

以下列舉接收Listf7e83be87db5cd2d9a8a0b8117b38cd4、List4c8e0c17c3bd7e0081bb17cc795e1984、Lista06c5c2dfccfdea21bf0b155d9343663>、User[]、User(bean裡麵包含List)幾種較為複雜的集合參數範例:

接收Listf7e83be87db5cd2d9a8a0b8117b38cd4集合參數:

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: &#39;json&#39;, 
 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[]") Listf7e83be87db5cd2d9a8a0b8117b38cd4 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 : &#39;application/json;charset=utf-8&#39;, //设置请求头信息 
 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就行了。

接收Lista06c5c2dfccfdea21bf0b155d9343663>集合參數:

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 : &#39;application/json;charset=utf-8&#39;, //设置请求头信息 
 success: function(data){ 
  … 
 }, 
 error: function(res){ 
  … 
 } 
});

2、Controller方法:

@Controller 
@RequestMapping("/catalog.do") 
public class CatalogController { 
 
 @RequestMapping(params = "fn=saveUsers") 
 @ResponseBody 
 public AjaxJson saveUsers(@RequestBody Lista06c5c2dfccfdea21bf0b155d9343663> 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 : &#39;application/json;charset=utf-8&#39;, //设置请求头信息 
 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(); 
  … 
 } 
}

相關推薦:

MVC中取得controller、URL及action的ASP.NET方法詳解

#

以上是js實作傳遞複雜json參數到controller的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn