Springmvc @PathVariable的用法解析
@PathVariable的用法解析
問(wèn)題描述
@RequestMapping(value = "/auth1/{uuid}/xxx", method = RequestMethod.GET) public void imageCode1(@PathVariable (value = "uuid") String uuid) { logger.info(uuid); }
見(jiàn)以上代碼,url中的uuid如何解析成為參數(shù)傳遞進(jìn)來(lái)。
解析過(guò)程
(接收請(qǐng)求:如/auth1/xxxx-xxx-xxx/xxx)
1. 將/auth1/{uuid}/xxx根據(jù)/拆成 auth1、{uuid}、xxx
2. 將{uuid}替換成(.*),并紀(jì)錄key為uuid
3. 同樣將/auth1/xxxx-xxx-xxx/xxx拆成auth1、xxxx-xxx-xxx、xxx
4. 進(jìn)行正則匹配,并根據(jù)group得到uuid=xxxx-xxx-xxx.
5. 將uuid=xxxx-xxx-xxx放入request的一個(gè)attribute中。
6. 根據(jù)反射和標(biāo)注得到pathvariable名為uuid
7. 去request得到這個(gè)uuid,然后進(jìn)行方法調(diào)用。
下面是測(cè)試springmvc的解析代碼。
public static void main(String[] args) { AntPathMatcher matcher = new AntPathMatcher(); System.out.println(matcher.match("{uuid}", "xxxx")); Map<String, String> result = matcher.extractUriTemplateVariables("{uuid}", "xxx"); System.out.println(result); }
當(dāng)上述問(wèn)題寫成:
@RequestMapping(value = "/auth1/{uuid}/xxx", method = RequestMethod.GET) public void imageCode1(@PathVariable String uuid) { logger.info(uuid); }
時(shí),以下代碼模擬測(cè)試了反射獲取uuid的過(guò)程
public static void main(String[] args) throws Exception { BeanInfo beanInfo = Introspector.getBeanInfo(A.class); MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors(); for (MethodDescriptor methodDescriptor : methodDescriptors) { System.out.println("method:" + methodDescriptor.getName()); ParameterDescriptor[] params = methodDescriptor.getParameterDescriptors(); if (params != null) { for (ParameterDescriptor param : params) { System.out.println("param:" + param.getName()); } } } Method[] methods = A.class.getMethods(); for (Method method : methods) { if (method.getName().equals("hello")) { LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); String[] methodNames = discoverer.getParameterNames(method); for (String methodName : methodNames) { System.out.println(methodName); } } } }
動(dòng)態(tài)參數(shù)使用@PathVariable
現(xiàn)在有如下的一條超鏈接
<a href="<c:url value="/actions/article/readArticle/${article.id}"/> " target="_blank">${article.title}</a>
這條超鏈接的特點(diǎn)就是在URL路徑中添加了EL表達(dá)式解析出來(lái)的id值。
因此,在SpringMVC的Controller層中,需要解析它,使用@PathVariable("articleId") Long articleId 來(lái)解析。
@PathVariable是專門用來(lái)解析URL請(qǐng)求中的動(dòng)態(tài)參數(shù)。
在Controller層的代碼如下
public static final String URL_ARTICLE_READ = "article/readArticle/{articleId}"; /** * 去文章詳情頁(yè)面 * 根據(jù)URL路徑中指定的文章ID號(hào),去獲取制定文章的內(nèi)容 * * @param articleId 指定的文章的ID號(hào) * @return 獲取此文章的數(shù)據(jù),并去文章詳情頁(yè)面 */ @RequestMapping(value = {URL_ARTICLE_READ} ) public ModelAndView readArticle(@PathVariable("articleId") Long articleId){ LOGGER.info("enter article detail page, articleId = {}",articleId); final Article article = articleService.getArticleById(articleId); ... }
這樣,頁(yè)面上的${article.id}的值,就最終映射到了Java中的Long articleId 上了。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot中登錄后關(guān)于cookie和session攔截問(wèn)題的案例分析
這篇文章主要介紹了Springboot中登錄后關(guān)于cookie和session攔截案例,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08mybatis插入數(shù)據(jù)不返回主鍵id的可能原因及解決方式
這篇文章主要介紹了mybatis插入數(shù)據(jù)不返回主鍵id的可能原因及解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08SpringBoot啟動(dòng)并初始化執(zhí)行sql腳本問(wèn)題
這篇文章主要介紹了SpringBoot啟動(dòng)并初始化執(zhí)行sql腳本問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01基于@RequestBody注解只能注入對(duì)象和map的解決
這篇文章主要介紹了@RequestBody注解只能注入對(duì)象和map的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10java 通過(guò)反射遍歷所有字段修改值的實(shí)例代碼
這篇文章主要介紹了java 通過(guò)反射遍歷所有字段修改值,通過(guò)java 的反射,遍歷所有字段,進(jìn)行一個(gè)判斷,取出來(lái)的值是帶有圖片鏈接的,進(jìn)行操作,省去了很多代碼,理解也很容易,下面跟隨小編看下實(shí)例代碼吧2021-05-05SpringBoot的@RestControllerAdvice作用詳解
這篇文章主要介紹了SpringBoot的@RestControllerAdvice作用詳解,@RestContrllerAdvice是一種組合注解,由@ControllerAdvice,@ResponseBody組成,本質(zhì)上就是@Component,需要的朋友可以參考下2024-01-01java判斷一個(gè)文件是否為二進(jìn)制文件的方法
這篇文章主要介紹了java判斷一個(gè)文件是否為二進(jìn)制文件的方法,涉及java針對(duì)文件的讀取及編碼判斷技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Java之SpringBoot定時(shí)任務(wù)案例講解
這篇文章主要介紹了Java之SpringBoot定時(shí)任務(wù)案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08