gpt4 book ai didi

Springmvc @PathVariable的用法解析

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 35 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Springmvc @PathVariable的用法解析由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

@PathVariable的用法解析

问题描述

?
1
2
3
4
@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的解析代码.

?
1
2
3
4
5
6
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);
}

当上述问题写成:

?
1
2
3
4
@RequestMapping (value = "/auth1/{uuid}/xxx" , method = RequestMethod.GET)
public void imageCode1( @PathVariable String uuid) {
     logger.info(uuid);
}

时,以下代码模拟测试了反射获取uuid的过程 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);
             }
         }
     }
}

动态参数使用@PathVariable

现在有如下的一条超链接 。

?
1
2
<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请求中的动态参数.

在Controller层的代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 上了.

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我.

原文链接:https://blog.csdn.net/zxy861114/article/details/51459077 。

最后此篇关于Springmvc @PathVariable的用法解析的文章就讲到这里了,如果你想了解更多关于Springmvc @PathVariable的用法解析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com