使用controller傳boolean形式值
controller傳boolean形式值
@GetMapping("/check-cart") public List<CartViewDto> checkCart(@RequestParam(value = "requirePrice", required = false) boolean requirePrice) { ?? ??? ?return service.checkCart(requirePrice); ?? ?}
controller傳入boolean類型的值,參數(shù)為非必填
我們可以傳三種類型
- http://端口:ip/cart/check-cart
- http://端口:ip/cart/check-cart?requirePrice=true
- http://端口:ip/cart/check-cart?requirePrice=false
這三種都可以。
其他如:
- http://端口:ip/cart/check-cart?requirePrice=
- http://端口:ip/cart/check-cart?requirePrice=111
都會(huì)報(bào)錯(cuò)
controller層接收各種參數(shù)和文件
在構(gòu)建一個(gè)系統(tǒng)時(shí),前端和后臺(tái)總是需要對(duì)接,在springmvc架構(gòu)里,這種對(duì)接一般發(fā)生在Controller層中。方法參數(shù)綁定首先支持Java所有基本類型(包括: byte、short、int、long、float、double、char、string、boolean),以及基本類型對(duì)應(yīng)封裝高級(jí)類(包括:StringBuilder、StringBuffer),也包含 了我們自己定義的各種JavaBean類型。接受的方式有很多,但是也要在接受數(shù)據(jù)時(shí)也要體現(xiàn)面向?qū)ο蟮乃枷搿?/p>
簡(jiǎn)單傳入
以用戶登陸為例,在前臺(tái)傳入數(shù)據(jù),做一個(gè)傳入的例子。
前臺(tái)傳入數(shù)據(jù)的形式為 username=10&password=10
<html> <head> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script> //簡(jiǎn)單形式的傳參 $.ajax({ type: "POST", url: "http://localhost:8080/test/requesetParamGet", contentType: "application/x-www-form-urlencoded", data:"username=10&password=10", dataType: "json", success: function (result) { if (result.code == 0) { console.log(result) } else { } } }); </script> </head> </html>
在這種情況下,如何在后臺(tái)進(jìn)行接受呢?可以采用如下的方法。
? ? @ResponseBody ? ? @RequestMapping("/simple") ? ? public R list(String name,String age){ ? ? ? ? System.out.println("name:"+name+",age:"+age); ? ? ? ? return R.ok(); ? ? }
當(dāng)然也可以采用HttpServeletRequest的形式進(jìn)行接受
? ? @ResponseBody ? ? @RequestMapping("/simple") ? ? public R requestGet(HttpServletRequest request){ ? ? ? ? System.out.println("reqname:"+request.getParameter("name")+",reqage:"+request.getParameter("age")); ? ? ? ? return R.ok(); ? ? }
以json對(duì)象的形式傳入
還是以用戶登陸為例,在前臺(tái)傳入數(shù)據(jù),做一個(gè)傳入的例子。
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script> //class獲取,需要用對(duì)象的形式 var s={ name:'liMin', age:'10' } $.ajax({ type: "POST", url: "http://localhost:8080/test/classGet", contentType: "application/json", data:JSON.stringify(s), dataType: "json", success: function (result) { if (result.code == 0) { console.log(result) } else { } } }); </script>
這種情況下,controller層建議使用對(duì)象的形式進(jìn)行接受。
? ? @ResponseBody ? ? @RequestMapping("/classGet") ? ? public R classGet(@RequestBody TestEntity testEntity){ ? ? ? ? System.out.println("className:"+testEntity.getName()+"classAge:"+testEntity.getAge()); ? ? ? ? return R.ok(); ? ? }
在傳參時(shí),需要添加注解@RequsetBody用來(lái)接收contentType為application/json的傳入對(duì)象。如果傳過(guò)來(lái)的時(shí)contentType頭為application/x-www-form-urlencoded,那么建議使用另外一個(gè)注解接受@RequestParam來(lái)接受。
? ? @RequestMapping("/requesetParamGet") ? ? public R addUser6(@RequestParam("username") String username,@RequestParam("password") String password) { ? ? ? ? System.out.println("username is:"+username); ? ? ? ? System.out.println("password is:"+password); ? ? ? ? return R.ok(); ? ? }
如果啥都不寫,那么就得不到這個(gè)對(duì)象,接受到的對(duì)象為NULL。
當(dāng)然,你可以不接受為一個(gè)對(duì)象,可以把傳過(guò)來(lái)的json對(duì)象轉(zhuǎn)化為json字符串,然后用各種工具進(jìn)行解析,也是可以的。當(dāng)然也是要加上@RequestBody或者@RequestParam的。
? ? @ResponseBody ? ? @RequestMapping("/stringGet") ? ? public R stringGet(@RequestBody ?String string){ ? ? ? ? System.out.println("String:"+string); ? ? ? ? return R.ok(); ? ? }
文件傳輸
在項(xiàng)目中,文件上傳有別于對(duì)象的上傳。
<html> <head> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script> function savePic(){ var formData = new FormData($( "#uploadPic" )[0]); var ajaxUrl = "http://localhost:8080/test/fileUpload"; //alert(ajaxUrl); //$('#uploadPic').serialize() 無(wú)法序列化二進(jìn)制文件,這里采用formData上傳 //需要瀏覽器支持:Chrome 7+、Firefox 4+、IE 10+、Opera 12+、Safari 5+。 $.ajax({ type: "POST", //dataType: "text", url: ajaxUrl, data: formData, //async: false, //cache: false, contentType: false, //上傳文件 processData: false,//序列化處理,默認(rèn)為true,上傳文件需要改成false success: function (data) { alert(data); }, error: function(data) { alert("error:"+data.responseText); } }); return false; } function jiance(){ var formData = new FormData(); formData.append() } </script> </head> <body> <form id="uploadPic" action="" enctype="multipart/form-data"> <input type="file" name="multipartFile" id="file"> <a href="javascript:savePic();" class="btn green"> 提交 </a> <a href="javascript:jiance();" class="btn green"> jiance </a> </form> </body> </html>
在后臺(tái)接受參數(shù)的例子:
? ? @RequestMapping("/fileUpload") ? ? @ResponseBody ? ? public R upload(MultipartFile multipartFile){ ? ? ? ? String filePath=""; ? ? ? ? if(!multipartFile.isEmpty()){ ? ? ? ? ? ? System.out.println(multipartFile.getOriginalFilename()); ? ? ? ? } ? ? ? ? return R.ok().put("filePath",filePath); ? ? }
? ? @RequestMapping("/fileUpload2") ? ? @ResponseBody ? ? public R upload2(@RequestParam("multipartFile") MultipartFile multipartFile){ ? ? ? ? String filePath=""; ? ? ? ? if(!multipartFile.isEmpty()){ ? ? ? ? ? ? System.out.println(multipartFile.getOriginalFilename()); ? ? ? ? } ? ? ? ? return R.ok().put("filePath",filePath); ? ? }
? ? @RequestMapping("/fileUpload3") ? ? @ResponseBody ? ? public R upload3(@RequestBody MultipartFile multipartFile){ ? ? ? ? String filePath=""; ? ? ? ? if(!multipartFile.isEmpty()){ ? ? ? ? ? ? System.out.println(multipartFile.getOriginalFilename()); ? ? ? ? } ? ? ? ? return R.ok().put("filePath",filePath); ? ? }
這里需要注意一點(diǎn),文件名必須和參數(shù)名保持一致,在本項(xiàng)目中file文件的名字必須為multipartFile。
相關(guān)文章
java自帶命令行工具jmap、jhat與jinfo的使用實(shí)例代碼詳解
本篇文章主要通過(guò)代碼實(shí)例對(duì)java自帶命令行工具jmap、jhat與jinfo的使用做出了詳解,需要的朋友可以參考下2017-04-04Spring Boot 實(shí)現(xiàn)配置文件加解密原理
這篇文章主要介紹了Spring Boot 實(shí)現(xiàn)配置文件加解密原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Redisson分布式信號(hào)量RSemaphore的使用超詳細(xì)講解
這篇文章主要介紹了Redisson分布式信號(hào)量RSemaphore的使用,基于Redis的Redisson的分布式信號(hào)量RSemaphore采用了與java.util.concurrent.Semaphore相似的接口和用法2023-02-02SpringBoot整合Gson 整合Fastjson的實(shí)例詳解
這篇文章主要介紹了SpringBoot整合Gson 整合Fastjson的實(shí)例詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11將Java程序的輸出結(jié)果寫入文件方法實(shí)例
這篇文章主要給大家介紹了關(guān)于將Java程序的輸出結(jié)果寫入文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02