首页  :: 新随笔  :: 管理

Spring源码解析 - AntPathMatcher

Posted on 2016-09-28 11:07  季枫  阅读(964)  评论(0编辑  收藏  举报

文章摘要:

  1. ant匹配规则

  2. PathMatcher接口

  3. 通过测试用例看AntPathMatcher的使用

 

ant匹配规则

AntPathMatcher如名使用的ant 的匹配规则,我们先看看吧.

  字符wildcard    描述

   ?         匹配一个字符

   *         匹配0个及以上字符

   **         匹配0个及以上目录directories

看几个官方的例子吧:

  com/t?st.jsp -             匹配: com/test.jsp  ,  com/tast.jsp  ,  com/txst.jsp
  com/*.jsp -             匹配: com文件夹下的全部.jsp文件
  com/**/test.jsp -          匹配: com文件夹和子文件夹下的全部.jsp文件,
  org/springframework/**/*.jsp -    匹配: org/springframework文件夹和子文件夹下的全部.jsp文件
  org/**/servlet/bla.jsp -       匹配: org/springframework/servlet/bla.jsp  ,  org/springframework/testing/servlet/bla.jsp  ,  org/servlet/bla.jsp

 

package org.springframework.util;

public interface PathMatcher {

    /**
     * 判断传入的path是否可以作为pattern使用
     */
    boolean isPattern(String path);

    /**
     * 使用pattern匹配path
     */
    boolean match(String pattern, String path);

    /**
     * 如名,是否开始部分匹配
     */
    boolean matchStart(String pattern, String path);

    /**
     * 提取path中匹配到的部分,如pattern(myroot/*.html),path(myroot/myfile.html),返回myfile.html
     */
    String extractPathWithinPattern(String pattern, String path);

    /**
     * 提取path中匹配到的部分,只是这边还需跟占位符配对为map,
     * 如pattern(/hotels/{hotel}),path(/hotels/1),解析出"hotel"->"1"
     */
    Map<String, String> extractUriTemplateVariables(String pattern, String path);

    /**
     * 提供比较器
     */
    Comparator<String> getPatternComparator(String path);

    /**
     * 合并pattern,pattern1然后pattern2
     */
    String combine(String pattern1, String pattern2);

}

 

 

http://www.cnblogs.com/leftthen/p/5212221.html

 

智读 | 成都会领科技有限公司官网 | 智读App下载 | 每天听本书的博客 | |