服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Springmvc @PathVariable的用法解析

Springmvc @PathVariable的用法解析

2022-02-28 13:05zxy861114 Java教程

这篇文章主要介绍了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

延伸 · 阅读

精彩推荐