博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC接收复杂集合参数
阅读量:4317 次
发布时间:2019-06-06

本文共 3518 字,大约阅读时间需要 11 分钟。

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
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
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
> listMap) { … }}
  •  接收User(bean里面包含List)集合参数:

 1、User实体类:

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', //设置请求头信息    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(); … }}

转载于:https://www.cnblogs.com/koal/p/5164991.html

你可能感兴趣的文章
进程和线程概念及原理
查看>>
Lucene、ES好文章
查看>>
android 生命周期
查看>>
jquery--this
查看>>
MySQL 5.1参考手册
查看>>
TensorFlow安装流程(GPU加速)
查看>>
OpenStack的容器服务体验
查看>>
BZOJ 1066 蜥蜴(网络流)
查看>>
提高批量插入数据的方法
查看>>
Linux重启Mysql命令
查看>>
前端模块化:RequireJS(转)
查看>>
应用程序缓存的应用(摘抄)
查看>>
jQuery基础知识,很赞的!!!
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>
component-based scene model
查看>>
Echart输出图形
查看>>
hMailServer搭建简单邮件系统
查看>>
从零开始学习jQuery
查看>>
Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射
查看>>