国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

SpringBoot @ModelAttribute使用場景分析

瀏覽:55日期:2023-02-18 09:57:56
前言

項目中遇到這么一個使用場景,用戶的登錄信息給予token保存,在需要有登錄信息的地方,每次都要去獲取用戶Id,但每次在請求方法中去獲取用戶信息,代碼重復,冗余,很low于是想到了用@ModelAttribute 這個屬性

@ModelAttribute有三種用法:- 可以標注在方法上;- 可以標注在方法中的參數上;- 還可以和@RequestMapping一起標注在方法上;

使用場景

不用@ModelAttribute 時候在需要用戶信息的請求中每次需要單獨獲取用戶信息

String token = request.getAttribute('token').toString(); User LoginUser = tokenService.decodeToken(token);

代碼重復每次都需要單獨去寫,

于是我想到了去優化一下代碼,在需要使用戶信息的controller中寫一個公共方法,每次直接獲取就可以了

private User gerUserInfo(HttpServletRequest request){String token = request.getAttribute('token').toString();User LoginUser = tokenService.decodeToken(token);return LoginUser; }

這樣寫代碼是簡化了一些,但是沒什么特別大的改觀,還是要在每個需求用戶信息的請求Controller中調用此方法取獲取用用戶信息,如果多個Controller需要獲取用戶信息的話還需要重復寫

也是想到繼承,寫一個公共的controller叫BaseController,每次在需要用戶信息的controller中繼承BaseController 然后在調用就可以了

@RestControllerpublic class BaseController { @Autowired private TokenService tokenService; private User gerUserInfo(HttpServletRequest request){String token = request.getAttribute('token').toString();User LoginUser = tokenService.decodeToken(token);return LoginUser; }}

這樣看上去似乎比之前兩種做法都簡單便捷很多,在需要使用用戶信息的controller中直接繼承調用就可以啦,但是并沒有根本解決我們的問題,我們還是需要寫重復代碼,在每個controller單獨獲取用戶信息,這是最優嘛?并不是!!!

其實呢springboot提供@ModelAttribute這個注解屬性使用這個通過參數注入就可獲取啦

我們把上面的稍微調整一下如:

@RestControllerpublic class BaseController { @Autowired private TokenService tokenService; @ModelAttribute public void userInfo(ModelMap modelMap, HttpServletRequest request) {String token = request.getAttribute('token').toString();User LoginUser = tokenService.decodeToken(token); modelMap.addAttribute('LoginUser', LoginUser);modelMap.addAttribute('userId', LoginUser.getUserId()); }}

然后在需要使用用戶信息的controller中進行參數映射就行啦

@ApiOperation(value = '用戶快過期優惠卷信息',tags = '優惠卷接口') @GetMapping('/expiredCoupon') public List<Coupon> userExpiredCoupon(@ModelAttribute('userId') @ApiParam(hidden = true) String userId){return couponService.getUserExpiredCoupon(userId); }@GetMapping('/info') @ApiOperation('獲取用戶信息') public User getUseInfo(@ModelAttribute('LoginUser') User user) {return user; }

這樣用戶信息通過形參直接注入到controller中,我們直接在請求中使用就可以啦

@ModelAttribute詳解

被@ModelAttribute注釋的方法會在此controller每個方法執行前被執行 標注在方法上面的注解,將方法返回的對象存儲在model中,該方法在這個控制器其他映射方法執行之前調用@ModelAttribute注釋一個方法的參數 從model中獲取參數@ModelAttribute('LoginUser') User user參數user的值來源于BaseControlleruserInfo()方法中的model屬性

具體更詳細使用參考 @ModelAttribute注解的使用總結

到此這篇關于SpringBoot @ModelAttribute 用法的文章就介紹到這了,更多相關SpringBoot @ModelAttribute 內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产精品久久久久久久久免费 | 欧美在线做爰高清视频 | 国产伦精品一区二区三区免费 | 亚洲精品午夜一区二区在线观看 | 免费看成人频视在线视频 | 99热久久精品免费精品 | 久草在线新免费 | 美女被免费网站在线软件 | 美国第一毛片 | 欧美视频在线观看免费精品欧美视频 | 亚洲国产成人久久午夜 | 免费韩国一级毛片 | 成年人看的黄色片 | 久久九九精品一区二区 | 亚洲免费高清视频 | 国产精品视频成人 | 国产美女啪 | 香蕉久久夜色精品国产 | 美女舒服好紧太爽了视频 | 亚洲最大成人 | 国产美女高清一级a毛片 | 成年人网站免费 | 亚洲欧美久久一区二区 | 欧美国产成人精品一区二区三区 | 日韩毛片欧美一级国产毛片 | 日韩免费黄色片 | 日韩一区二区精品久久高清 | 精品亚洲福利一区二区 | 国产成人亚洲综合一区 | 极品精品国产超清自在线观看 | 国产精品18久久久久久久久久 | 美国三级在线观看 | 国产色视频在线观看免费 | 成人午夜网站 | 京东一热本色道久久爱 | 久久综合久久美利坚合众国 | 精品国产欧美一区二区五十路 | freese×video性欧美丝袜 | 可以看毛片的网站 | 99久久综合狠狠综合久久一区 | 香蕉521av网站永久地址 |