Spring3属性之useDefaultSuffixPattern

本文探讨了SpringMVC框架中URL与Controller方法之间的映射机制。特别关注了使用@RequestMapping注解时,如何通过DefaultAnnotationHandlerMapping类实现对不同URL模式的支持。文中详细解释了useDefaultSuffixPattern属性的作用及如何通过它扩展URL匹配范围。

今天从网上抄了个Spring MVC的demo. 发现Controller方法上采用

@RequestMapping("/hello")作为Request与Controller的映射。但对于/hello.html居然也会走这个方法。不解,调查发现在DefaultAnnotationHandlerMapping类有一个addUrlsForPath方法:


/**
     * Add URLs and/or URL patterns for the given path.
     * @param urls the Set of URLs for the current bean
     * @param path the currently introspected path
     */
    protected void addUrlsForPath(Set<String> urls, String path) {
        urls.add(path);
        if (this.useDefaultSuffixPattern && path.indexOf('.') == -1 && !path.endsWith("/")) {
            urls.add(path + ".*");
            urls.add(path + "/");
        }
    }

从代码中可以看出类似/hello这种,没有点又非“/”的结尾的URL,会衍生为三个URL:

1. /hello  2. /hello.* 3. /hello/

即只要是这三种URL模式的请求,都会映射到这个Controller上。该功能的启用与停止,由属性useDefaultSuffixPattern来控制。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值