上一篇文章]总结了简单传收参数,这一篇讲如何传收复杂参数,比如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") Listid){}
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[]") Listid){}
其实以上两种都是一个道理,主要是看发送请求时 参数是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 Listcustomers;//属于用户的客户群 //省略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 Listuser){ }
public void ajaxPost(@ResponBody List
THANDKS
- End -
一个立志成大腿而每天努力奋斗的年轻人
伴学习伴成长,成长之路你并不孤单!