码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC接收复杂集合参数,集合对象

时间:2018-06-29 17:23:24      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:发送post请求   $.ajax   str   json   对象序列化   示例   var   前端   ping   

Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是application/json,因此发送POST请求时需要设置请求报文头信息,否则Spring MVC在解析集合请求参数时不会自动的转换成JSON数据再解析成相应的集合。以下列举接收List<Integer>、List<User>、List<Map<String,Object>>、User[]、User(bean里面包含List)几种较为复杂的集合参数示例:

  • 一、接收List<Integer>集合参数:

1、页面js代码:

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

 

Java代码

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

 

  •  接收List<User>、User[]集合参数:

1、User实体类:

 

Java代码

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

2、页面js代码:

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){
        …
    }
  });

 

3、Controller方法:

Java代码

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

如果想要接收User[]数组,只需要把add的参数类型改为@RequestBody User[] userArray就行了。

 

  • 接收List<Map<String,Object>>集合参数:

1、页面js代码(不需要User对象了):

Js代码

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

Java代码

  1. @Controller
    @RequestMapping(value = "/user")
    public class UserController(){
      @RequestMapping(value = "/index/add", method = RequestMethod.POST)
      @ResponseBody
      public ReturnMsg addOrEdit(@RequestBody List<Map<String,Object>> listMap) {
        …
      }
    }
  •  接收User(bean里面包含List)集合参数:

1、User实体类:

Java代码

  1. public class User {
      private int id;
      private String name;
      private String pwd;
      private List<User> userList;
      //省略getter/setter
    }

 

2、页面js代码:

 

Js代码

  1. 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){
            …
        }
      });

3、Controller方法:

Java代码

  1. @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();

SpringMVC接收复杂集合参数,集合对象

标签:发送post请求   $.ajax   str   json   对象序列化   示例   var   前端   ping   

原文地址:https://www.cnblogs.com/yangchuncool/p/9244190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!