上一篇文章[javaWeb傳收參數方式總結]總結了簡單傳收參數,這一篇講如何傳收復雜參數,好比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>java
//(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)方式 <form id="fromGet" action="fromGet" method="GET"> <input type="text"name="id" value="1"> <input type="text"name="id" value="2"> <input type="text"name="id" value="3"> </form>
//(4)Form表單POST方式 //發送請求參數會被拼接成 id=1&id=2&id=3 存儲在請求體中 <form id="fromGet" action="fromGet" method="POST"> <input type="text"name="id" value="1"> <input type="text"name="id" value="2"> <input type="text"name="id" value="3"> </form>
//數組 public void ajaxGet(Long[] id){ }
//List集合 public void ajaxGet(@RequestParam("id") List<Long> id){ }
//(1)普通url http://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' });
//數組 public void ajaxGet(@RequestParam("id[]") Long[] id){ }
//List集合 public void ajaxGet(@RequestParam("id[]") List<Long> id){ }
其實以上兩種都是一個道理,主要是看發送請求時 參數是id仍是id[](可以使用瀏覽器的F12開發者工具查看network請求),來決定後端使不使用@RequestParam("id[]")進行數據綁定ajax
好比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>json
User實體類後端
//User實體類 public class User { private String name; private String pwd; private List<User> 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' });
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' });
public void ajaxPost(@ResponBody User[] user){ }
public void ajaxPost(@ResponBody List<User> user){ }
public void ajaxPost(@ResponBody List<Map<String,Object>> userMap){ }
THANDKS數組
一個立志成大腿而天天努力奮鬥的年輕人瀏覽器
伴學習伴成長,成長之路你並不孤單!app