javascript,controller,json
web前端-js教程Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是application/json,因此发送POST请求时需要设置请求报文头信息,否则Spring MVC在解析集合请求参数时不会自动的转换成JSON数据再解析成相应的集合。
倩女幽魂易语言辅助源码,vscode配置masm,ubuntu 查看服务器状态,tomcat关联sdk,sqlite3可以,wordpress网站地图生成插件,响应式页面 前端框架,爬虫 代理ip原理,php中判断,百度seo软件seo顾问,商务网站主页源码,网页源码内搜索关键字,仿最新版奇热模板,小程序水滴福袋怎么回事lzw
以下列举接收List、List、List<Map>、User[]、User(bean里面包含List)几种较为复杂的集合参数示例:chrome 源码搜索,vscode版本稳定,如何启动u盘里面的ubuntu,tomcat传输协议,sqlite可以联网吗,消息悬浮窗插件,web前端响应式布局框架,学python爬虫难吗,php 站长,SEO广告收费,磁力链接网站源码,手机网页 自动切换 图片,手机淘宝登陆网页模板下载软件lzw
接收List集合参数:微信强制分享群聊邀请诱导转发源码,vscode断网无法打开,ubuntu修复硬盘,怎样查找tomcat路径,scrapy 爬虫项目,php 过滤 utf8,大淘客网站seo设置,discuz 开发手机网站模板下载,图书商城模板lzw
1、页面js代码:var idList = new Array(); idList.push(“1”); idList.push(“2”); idList.push(“3”); var isBatch = false; $.ajax({ type: "POST", url: "/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[]集合参数:
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: "/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>集合参数:
1、页面js代码(不需要User对象了):
var userList = new Array(); userList.push({name: "李四",pwd: "123"}); userList.push({name: "张三",pwd: "332"}); $.ajax({ type: "POST", url: "/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<Map> 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: "/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(); … } }