SpingMvc復(fù)雜參數(shù)傳收總結(jié)
引言
上一篇文章[javaWeb傳收參數(shù)方式總結(jié)]總結(jié)了簡單傳收參數(shù),這一篇講如何傳收復(fù)雜參數(shù)
比如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>等幾種復(fù)雜參數(shù)</user>。
簡單數(shù)組集合類
比如Long[],String[],List<User><long style="margin: 0px; padding: 0px; max-width: 100%; overflow-wrap: break-word !important; box-sizing: border-box;">等</long>
1.重復(fù)單個參數(shù)
前端
//(1)普通
http://localhost:8080/ajaxGet?id=1&id=2&id=3
//(2)Ajaxget方式 發(fā)送請求時(shí)等于(1)方式 $.ajax({ type: "GET", url: "http://localhost:8080/ajaxGet?id=1&id=2&id=3" });
//(3)Form表單GET方式 發(fā)送請求時(shí)等于(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方式 //發(fā)送請求參數(shù)會被拼接成 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>
后端SpringMvc:
//數(shù)組 public void ajaxGet(Long[] id){ }
//List集合 public void ajaxGet(@RequestParam("id") List<Long> id){ }
2.數(shù)組參數(shù)
前端:
//(1)普通url
http://localhost:8080/ajaxGet?id[]=1&id[]=2&id[]=3
//2.Form GET方式(Ajax異步表單提交) 發(fā)送請求時(shí)等于(1)方式 $.ajax({ type: "GET", url: "http://localhost:8080/ajaxGet", data: {"id":[1,2,3]}, contentType:'application/x-www-form-urlencoded' });
//(3)Form POST方式(Ajax異步表單提交) //發(fā)送請求參數(shù)會被拼接成 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:
//數(shù)組 public void ajaxGet(@RequestParam("id[]") Long[] id){ }
//List集合 public void ajaxGet(@RequestParam("id[]") List<Long> id){ }
其實(shí)以上兩種都是一個道理,主要是看發(fā)送請求時(shí) 參數(shù)是id還是id[](可使用瀏覽器的F12開發(fā)者工具查看network請求),來決定后端使不使用@RequestParam("id[]")進(jìn)行數(shù)據(jù)綁定
復(fù)雜實(shí)體類與集合
比如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.復(fù)雜實(shí)體類User
User實(shí)體類
//User實(shí)體類 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' });
后端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> user){ }
public void ajaxPost(@ResponBody List<Map<String,Object>> userMap){ }
以上就是SpingMvc復(fù)雜參數(shù)傳收總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpingMvc復(fù)雜參數(shù)傳收的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring AOP自定義注解方式實(shí)現(xiàn)日志管理的實(shí)例講解
下面小編就為大家分享一篇spring AOP自定義注解方式實(shí)現(xiàn)日志管理的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Java中json格式化BigDecimal保留2位小數(shù)
這篇文章主要給大家介紹了關(guān)于Java中json格式化BigDecimal保留2位小數(shù)的相關(guān)資料,BigDecimal是Java中的一個數(shù)學(xué)庫,可以實(shí)現(xiàn)高精度計(jì)算,文中給出了詳細(xì)的代碼實(shí)例,需要的朋友可以參考下2023-09-09spring boot 項(xiàng)目利用Jenkins實(shí)現(xiàn)自動化部署的教程詳解
這篇文章主要介紹了spring boot 項(xiàng)目利用Jenkins實(shí)現(xiàn)自動化部署的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07springboot啟動后卡住無日志的幾種情況小結(jié)
這篇文章主要介紹了springboot啟動后卡住無日志的幾種情況小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12實(shí)例分析Java Class的文件結(jié)構(gòu)
今天把之前在Evernote中的筆記重新整理了一下,發(fā)上來供對java class 文件結(jié)構(gòu)的有興趣的同學(xué)參考一下2013-04-04java管道piped輸入流與輸出流應(yīng)用場景案例分析
這篇文章主要介紹了java管道流PipedInputStream與PipedOutputStream(輸入流與輸出流)的應(yīng)用場景案例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02