Vue中傳遞自定義參數(shù)到后端、后端獲取數(shù)據(jù)并使用Map接收參數(shù)
在對(duì)axios進(jìn)行二次封裝的時(shí)候、為了統(tǒng)一接口的設(shè)計(jì)。有些傳遞的參數(shù)是直接拼接到URL地址欄中的、但是為了統(tǒng)一管理、不能將傳遞的參數(shù)直接拼接到地址欄中。
如何自定義傳遞的參數(shù)到后端,后端如何獲取到這些自定義的參數(shù)?如果解決這一問(wèn)題,我就能隨便傳遞參數(shù),不在局限于將參數(shù)拼接到URL中,不局限只能后端的javaBean對(duì)象傳遞表單的形式
1、未進(jìn)行二次封裝之前的操作
1.1 前端調(diào)用接口設(shè)計(jì)
(傳遞的參數(shù)使用拼接的形式):
showAllUserInfo(currentPage, pageSize) { //異步請(qǐng)求顯示所有數(shù)據(jù) currentPage = currentPage ? currentPage : this.now; pageSize = pageSize ? pageSize : this.size; axios .get("user/findAllUser/" + currentPage + "/" + pageSize) .then((res) => { if (res.code === 200) { this.tableDatas = res.data.result.userList; this.total = res.data.result.totals; } else { _this.$message.error(resp.data.errMessage); } }); },
1.2 后端接口設(shè)計(jì)
@RequestMapping( value = {"/user/findAllUser/{page}/{size}"}, method = {RequestMethod.GET} ) public Result findAllUser(@PathVariable("page") Integer page, @PathVariable("size") Integer size) { int currentPage = page; int pageSize = size; Map<String, Integer> map = new HashMap(); map.put("startIndex", (currentPage - 1) * pageSize); map.put("pageSize", pageSize); List<User> userList = this.userService.findAllUser(map); Long totals = this.userService.findUserTotals(); HashMap<String, Object> result = new HashMap(); result.put("userList", userList); result.put("totals", totals); return Result.ok().data("result", result); }
2、使用二次封裝axios后的設(shè)計(jì)
2.1 封裝的接口類(lèi)型(只展示關(guān)鍵的接口調(diào)用部分)
findAllUser: (params) => { return Get('http://localhost:8282/user/findAllUser', params) }
2.2 前端調(diào)用接口設(shè)計(jì)
將要傳遞的參數(shù)放入這里: const params = { page: currentPage, size: pageSize };
showAllUserInfo(currentPage, pageSize) { //異步請(qǐng)求顯示所有數(shù)據(jù) currentPage = currentPage ? currentPage : this.now; pageSize = pageSize ? pageSize : this.size; const params = { page: currentPage, size: pageSize, }; this.$axios.findAllUser(params).then((res) => { if (res.code === 200) { this.tableDatas = res.data.result.userList; this.total = res.data.result.totals; } else { _this.$message.error(resp.data.errMessage); } }); },
2.3 后端接口設(shè)計(jì)
@RequestMapping( value = {"/user/findAllUser"}, method = {RequestMethod.GET} ) public Result findAllUser(@RequestParam Map<String, Object> maps) { System.out.println(maps.get("page")); System.out.println(maps.get("size")); Integer currentPage = Integer.parseInt(maps.get("page").toString()); Integer pageSize = Integer.parseInt(maps.get("size").toString()); Map<String, Integer> map = new HashMap(); map.put("startIndex", (currentPage - 1) * pageSize); map.put("pageSize", pageSize); List<User> userList = this.userService.findAllUser(map); Long totals = this.userService.findUserTotals(); HashMap<String, Object> result = new HashMap(); result.put("userList", userList); result.put("totals", totals); return Result.ok().data("result", result); }
3、友情提示
這個(gè)真的是巨坑、總是報(bào)轉(zhuǎn)換不對(duì)、通過(guò)以下方式可以將stirng類(lèi)型的轉(zhuǎn)換為int類(lèi)型
Integer currentPage = Integer.parseInt(maps.get(“page”).toString());
- 使用Map接收參數(shù),必須使用
@RequestParam
修飾。 - 使用data傳遞參數(shù),必須使用一個(gè)實(shí)體類(lèi)接收參數(shù),而且需要添加注解
@RequestBody
進(jìn)行修飾
4、效果展示
到此這篇關(guān)于Vue中傳遞自定義參數(shù)到后端、后端獲取數(shù)據(jù)并使用Map接收參數(shù)的文章就介紹到這了,更多相關(guān)Vue傳遞參數(shù)給后端內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vant-ui組件庫(kù)中如何修改NavBar導(dǎo)航欄的樣式
這篇文章主要介紹了vant-ui組件庫(kù)中如何修改NavBar導(dǎo)航欄的樣式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08淺析vue中常見(jiàn)循環(huán)遍歷指令的使用 v-for
這篇文章主要介紹了vue中常見(jiàn)循環(huán)遍歷指令的使用 v-for,包括v-for遍歷數(shù)組,v-for遍歷json對(duì)象,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-04-04Vue實(shí)現(xiàn)單點(diǎn)登錄控件的完整代碼
這里提供一個(gè)Vue單點(diǎn)登錄的demo給大家參考,對(duì)Vue實(shí)現(xiàn)單點(diǎn)登錄控件的完整代碼感興趣的朋友跟隨小編一起看看吧2021-11-11在vue中動(dòng)態(tài)添加class類(lèi)進(jìn)行顯示隱藏實(shí)例
今天小編就為大家分享一篇在vue中動(dòng)態(tài)添加class類(lèi)進(jìn)行顯示隱藏實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11vue中實(shí)現(xiàn)拖動(dòng)調(diào)整左右兩側(cè)div的寬度的示例代碼
這篇文章主要介紹了vue中實(shí)現(xiàn)拖動(dòng)調(diào)整左右兩側(cè)div的寬度的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Vue中常用rules校驗(yàn)規(guī)則(實(shí)例代碼)
這篇文章主要介紹了Vue中常用rules校驗(yàn)規(guī)則,本文通過(guò)實(shí)例代碼個(gè)大家介紹了一些校驗(yàn)方法,需要的朋友可以參考下2019-11-11