1. 程式人生 > >springmvc接收集合引數,集合物件

springmvc接收集合引數,集合物件

Spring MVC在接收集合請求引數時,需要在Controller方法的集合引數裡前新增@RequestBody或者@RequestParam,而@RequestBody預設接收的enctype (MIME編碼)是application/json,因此傳送POST請求時需要設定請求報文頭資訊(ajax提交資料時設定提交型別為:contentType : 'application/json;charset=utf-8', //設定請求頭資訊),否則Spring MVC在解析集合請求引數時不會自動的轉換成JSON資料再解析成相應的集合。

本文通過List<Integer>、List<User>、List<Map<String,Object>>、User[]、User(bean裡面包含List)幾種較為複雜的集合引數示例

eg1:接收List<Integer>集合引數

1、Js程式碼:

var arr = [1,2,3];
$.jBox.confirm("確定要刪除資料嗎?", "warning",function () {
    $.ajax({
       type: 'get',
       url: '${base.contextPath}/giving/index/del',
       dataType: 'json',
       data: {ids: arr},
       success: function (result) {
          …
       },
       error: function (result) {
          …
       }
   })
})

2、Controller方法:

@Controller
@RequestMapping("/wxgiving")
public class WxGivingController{
  @RequestMapping(value = "/index/del", method = RequestMethod.GET)
  @ResponseBody
  public ReturnMsg del (@RequestParam(value = "ids[]")List <Integer> ids){
     …
  }
}

eg2: 接收List<User>、User[]集合引數:

1、js程式碼

var userList = new Array();
userList.push({name: "張三",pwd: "123"});
userList.push({name: "李四",pwd: "223"});
$.ajax({
    type: "POST",
    url: "${base.contextPath}/user/index/add",
    data: JSON.stringify(userList),//將物件序列化成JSON字串
    dataType:"json",
    contentType : 'application/json;charset=utf-8', //設定請求頭資訊
    success: function(result){
        …
    },
    error: function(result){
        …
    }
  });

2、Controller方法:

@Controller
@RequestMapping(value = "/user")
public class UserController(){
  @RequestMapping(value = "/index/add", method = RequestMethod.POST)
  @ResponseBody
  public ReturnMsg addOrEdit(@RequestBody List<User> userList) {
     …
  }
}

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

1、js程式碼

var userList = new Array();
userList.push({name: "張三",pwd: "123"});
userList.push({name: "李四",pwd: "223"});
$.ajax({
    type: "POST",
    url: "${base.contextPath}/user/index/add",
    data: JSON.stringify(userList),//將物件序列化成JSON字串
    dataType:"json",
    contentType : 'application/json;charset=utf-8', //設定請求頭資訊
    success: function(result){
        …
    },
    error: function(result){
        …
    }
  });

2、Controller方法:

@Controller
@RequestMapping(value = "/user")
public class UserController(){
  @RequestMapping(value = "/index/add", method = RequestMethod.POST)
  @ResponseBody
  public ReturnMsg addOrEdit(@RequestBody List<Map<String,Object>> listMap) {
    …
  }
}

eg4:User實體類

1、js程式碼

var userArray= new Array();
userArray.push({name: "張三",pwd: "123"});
userArray.push({name: "李四",pwd: "223"});
var user = {};
user.name = "王五";
user.pwd = "888";
user.userList= userArray;
$.ajax({
    type: "POST",
    url: "${base.contextPath}/user/index/add",
    data: JSON.stringify(user),//將物件序列化成JSON字串
    dataType:"json",
    contentType : 'application/json;charset=utf-8', //設定請求頭資訊
    success: function(result){
        …
    },
    error: function(result){
        …
    }
  });

2、Controller方法:

@Controller
@RequestMapping(value = "/user")
public class UserController(){
  @RequestMapping(value = "/index/add", method = RequestMethod.POST)
  @ResponseBody
  public ReturnMsg addOrEdit(@RequestBody User user) {
    List<User> userList= user.getUserList();