欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Springmvc @PathVariable的用法解析

时间:2022-04-21 09:45:34|栏目:JAVA代码|点击:

@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);
                }
            }
        }
    }

动态参数使用@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请求中的动态参数。

在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 上了。

上一篇:Java红黑树的数据结构与算法解析

栏    目:JAVA代码

下一篇:springboot项目启动指定对应环境的方法

本文标题:Springmvc @PathVariable的用法解析

本文地址:http://www.codeinn.net/misctech/199797.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有