博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpingMvc复杂参数传收总结
阅读量:7005 次
发布时间:2019-06-27

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

上一篇文章]总结了简单传收参数,这一篇讲如何传收复杂参数,比如Long[] 、User(bean里面包含List)、User[]、List<User><user style="margin: 0px; padding: 0px; max-width: 100%; overflow-wrap: break-word !important; box-sizing: border-box;">、List<Map<String,Object>等几种复杂参数</user>。

一.简单数组集合类

比如Long[],String[],List<User><long style="margin: 0px; padding: 0px; max-width: 100%; overflow-wrap: break-word !important; box-sizing: border-box;">等</long>

前端:

1.重复单个参数
//(1)普通http://localhost:8080/ajaxGet?id=1&id=2&id=3
//(2)Ajaxget方式 发送请求时等于(1)方式    $.ajax({        type: "GET",        url: "http://localhost:8080/ajaxGet?id=1&id=2&id=3"    });
//(3)Form表单GET方式 发送请求时等于(1)方式
//(4)Form表单POST方式 //发送请求参数会被拼接成 id=1&id=2&id=3 存储在请求体中
后端SpringMvc:
//数组public void ajaxGet(Long[] id){}
//List集合public void ajaxGet(@RequestParam("id") List
id){}

2.数组参数

前端:
//(1)普通urlhttp://localhost:8080/ajaxGet?id[]=1&id[]=2&id[]=3
//2.Form GET方式(Ajax异步表单提交) 发送请求时等于(1)方式$.ajax({        type: "GET",        url: "http://localhost:8080/ajaxGet",        data: {"id":[1,2,3]},        contentType:'application/x-www-form-urlencoded'    });
//(3)Form POST方式(Ajax异步表单提交)//发送请求参数会被拼接成 id[]=1&id[]=2&id[]=3 存储在请求体中$.ajax({        type: "POST",        url: "http://localhost:8080/ajaxPost",        data: {"id":[1,2,3]},        contentType:'application/x-www-form-urlencoded'    });
后端SpringMvc:
//数组public void ajaxGet(@RequestParam("id[]") Long[] id){}
//List集合public void ajaxGet(@RequestParam("id[]") List
id){}

其实以上两种都是一个道理,主要是看发送请求时 参数是id还是id[](可使用浏览器的F12开发者工具查看network请求),来决定后端使不使用@RequestParam("id[]")进行数据绑定

二.复杂实体类与集合

比如User(bean里面包含List)、User[]、List<User><user style="margin: 0px; padding: 0px; max-width: 100%; overflow-wrap: break-word !important; box-sizing: border-box;">、List<Map<String,Object>等,此种类型均使用Json提交</user>

1.复杂实体类User

User实体类

//User实体类public class User {      private String name;       private String pwd;      private List
customers;//属于用户的客户群 //省略getter/setter }
前端:
//用户var user = {};user.name = "李刚";user.pwd = "888";//客户var customerArray = new Array();customerArray.push({name: "李四",pwd: "123"});customerArray.push({name: "张三",pwd: "332"});user. customers = customerArray;    $.ajax({        type: "POST",        url: "http://localhost:8080/ajaxPost",        data: JSON.stringify(user),        contentType:'application/json;charset=utf-8'    });
后端SpringMvc:
public void ajaxPost(@ResponBody User user){ }
前端:
//用户var userList = new Array();  userList.push({name: "李四",pwd: "123"});   userList.push({name: "张三",pwd: "332"});   $.ajax({        type: "POST",        url: "http://localhost:8080/ajaxPost",        data: JSON.stringify(userList),        contentType:'application/json;charset=utf-8'    });
后端SpringMvc:
public void ajaxPost(@ResponBody User[] user){ }
public void ajaxPost(@ResponBody List
user){ }
public void ajaxPost(@ResponBody List
> userMap){ }

image

image

 THANDKS

  • End -

一个立志成大腿而每天努力奋斗的年轻人

伴学习伴成长,成长之路你并不孤单!

扫描二维码,关注公众号

转载地址:http://usytl.baihongyu.com/

你可能感兴趣的文章
yii2权限控制rbac之rule详细讲解
查看>>
Android AbsListView Abs前缀
查看>>
Redis应用场景一
查看>>
webservice 协议
查看>>
SAR-303 xml validator验证框架
查看>>
牛腩学用MUI做手机APP
查看>>
WCF--安全小见解...
查看>>
C# Type.GetConstructor() 根据构造函数参数获取实例对象(一)
查看>>
针对各地项目icomet停止服务的临时处理办法
查看>>
Spring源代码解析
查看>>
搞明白这八个问题,Linux系统就好学多了
查看>>
Android Weekly Notes Issue #222
查看>>
CAD字体显示问号的解决办法
查看>>
微信支付开发(1) JS API支付V3版(转)
查看>>
利用tween,使用原生js实现模块回弹动画效果
查看>>
InfluxDB源码目录结构解析
查看>>
Mysql连接错误:Lost connection to Mysql server at 'waiting for initial communication packet'
查看>>
使用hosts.allow和hosts.deny实现简单的防火墙
查看>>
Javascript将字符串日期格式化为yyyy-mm-dd的方法 js number 类型 没有length 属性 string类型才有...
查看>>
磁波刀和海扶刀的区别
查看>>