Springmvc @PathVariable的用法解析
@PathVariable的用法解析
問題描述
@RequestMapping(value = "/auth1/{uuid}/xxx", method = RequestMethod.GET)
public void imageCode1(@PathVariable (value = "uuid") String uuid) {
logger.info(uuid);
}
見以上代碼,url中的uuid如何解析成為參數傳遞進來。
解析過程
(接收請求:如/auth1/xxxx-xxx-xxx/xxx)
1. 將/auth1/{uuid}/xxx根據/拆成 auth1、{uuid}、xxx
2. 將{uuid}替換成(.*),并紀錄key為uuid
3. 同樣將/auth1/xxxx-xxx-xxx/xxx拆成auth1、xxxx-xxx-xxx、xxx
4. 進行正則匹配,并根據group得到uuid=xxxx-xxx-xxx.
5. 將uuid=xxxx-xxx-xxx放入request的一個attribute中。
6. 根據反射和標注得到pathvariable名為uuid
7. 去request得到這個uuid,然后進行方法調用。
下面是測試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);
}
當上述問題寫成:
@RequestMapping(value = "/auth1/{uuid}/xxx", method = RequestMethod.GET)
public void imageCode1(@PathVariable String uuid) {
logger.info(uuid);
}
時,以下代碼模擬測試了反射獲取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)參數使用@PathVariable
現在有如下的一條超鏈接
<a href="<c:url value="/actions/article/readArticle/${article.id}"/> "
target="_blank">${article.title}</a>
這條超鏈接的特點就是在URL路徑中添加了EL表達式解析出來的id值。
因此,在SpringMVC的Controller層中,需要解析它,使用@PathVariable("articleId") Long articleId 來解析。
@PathVariable是專門用來解析URL請求中的動態(tài)參數。
在Controller層的代碼如下
public static final String URL_ARTICLE_READ = "article/readArticle/{articleId}";
/**
* 去文章詳情頁面
* 根據URL路徑中指定的文章ID號,去獲取制定文章的內容
*
* @param articleId 指定的文章的ID號
* @return 獲取此文章的數據,并去文章詳情頁面
*/
@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 上了。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Springboot中登錄后關于cookie和session攔截問題的案例分析
這篇文章主要介紹了Springboot中登錄后關于cookie和session攔截案例,本文通過實例圖文相結合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
SpringBoot啟動并初始化執(zhí)行sql腳本問題
這篇文章主要介紹了SpringBoot啟動并初始化執(zhí)行sql腳本問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
SpringBoot的@RestControllerAdvice作用詳解
這篇文章主要介紹了SpringBoot的@RestControllerAdvice作用詳解,@RestContrllerAdvice是一種組合注解,由@ControllerAdvice,@ResponseBody組成,本質上就是@Component,需要的朋友可以參考下2024-01-01

