Springmvc @PathVariable的用法解析
@PathVariable的用法解析
問題描述
@RequestMapping(value = "/auth1/{uuid}/xxx", method = RequestMethod.GET)
public void imageCode1(@PathVariable (value = "uuid") String uuid) {
logger.info(uuid);
}
見以上代碼,url中的uuid如何解析成為參數(shù)傳遞進(jìn)來。
解析過程
(接收請求:如/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)用。
下面是測試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)上述問題寫成:
@RequestMapping(value = "/auth1/{uuid}/xxx", method = RequestMethod.GET)
public void imageCode1(@PathVariable String uuid) {
logger.info(uuid);
}
時(shí),以下代碼模擬測試了反射獲取uuid的過程
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);
}
}
}
}
動態(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á)式解析出來的id值。
因此,在SpringMVC的Controller層中,需要解析它,使用@PathVariable("articleId") Long articleId 來解析。
@PathVariable是專門用來解析URL請求中的動態(tài)參數(shù)。
在Controller層的代碼如下
public static final String URL_ARTICLE_READ = "article/readArticle/{articleId}";
/**
* 去文章詳情頁面
* 根據(jù)URL路徑中指定的文章ID號,去獲取制定文章的內(nèi)容
*
* @param articleId 指定的文章的ID號
* @return 獲取此文章的數(shù)據(jù),并去文章詳情頁面
*/
@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);
...
}
這樣,頁面上的${article.id}的值,就最終映射到了Java中的Long articleId 上了。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
在SpringBoot中通過jasypt進(jìn)行加密解密的方法
今天小編就為大家分享一篇關(guān)于在SpringBoot中通過jasypt進(jìn)行加密解密的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
SpringBoot集成Spring Security用JWT令牌實(shí)現(xiàn)登錄和鑒權(quán)的方法
這篇文章主要介紹了SpringBoot集成Spring Security用JWT令牌實(shí)現(xiàn)登錄和鑒權(quán)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
maven 中relativePath標(biāo)簽的作用
元素在 Maven 的 pom.xml 文件中用于指定父項(xiàng)目的相對路徑,這篇文章主要介紹了maven 中relativePath標(biāo)簽的作用,需要的朋友可以參考下2024-11-11
Spring Security方法鑒權(quán)的實(shí)現(xiàn)
在Spring Security中,主要有兩種鑒權(quán)方式,一個(gè)是基于web請求的鑒權(quán),一個(gè)是基于方法的鑒權(quán),本文就來介紹一下Spring Security方法鑒權(quán)的實(shí)現(xiàn),感興趣的可以了解一下2023-12-12
完美解決idea moudle沒有藍(lán)色的小方塊的問題
這篇文章主要介紹了完美解決idea moudle沒有藍(lán)色的小方塊的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
springboot創(chuàng)建多module項(xiàng)目的實(shí)例
這篇文章主要介紹了springboot創(chuàng)建多module項(xiàng)目的實(shí)例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring Security中successHandler和failureHandler使用方式
這篇文章主要介紹了Spring Security中successHandler和failureHandler使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

