类 AntPathMatcher

  • 所有已实现的接口:
    PathMatcher

    public class AntPathMatcher
    extends Object
    implements PathMatcher
    Ant 风格的路径匹配器

    匹配URL的规则如下:

    • ? 匹配单个字符
    • * 匹配0个或多个字符
    • ** 0个或多个路径中的目录节点
    • {mirage:[a-z]+} 匹配以"mirage"命名的正则 [a-z]+

    例子:

    • com/t?st.jsp — 匹配 com/test.jspcom/tast.jspcom/txst.jsp
    • com/*.jsp — 匹配com目录下全部 .jsp文件
    • com/**/test.jsp — 匹配com目录下全部 test.jsp文件
    • cn/mirage/**/*.jsp — 匹配cn/mirage路径下全部.jsp 文件
    • org/**/servlet/bla.jsp — 匹配cn/mirage/servlet/bla.jspcn/mirage/testing/servlet/bla.jsporg/servlet/bla.jsp
    • com/{filename:\\w+}.jsp 匹配 com/test.jsp 并将 test 关联到 filename 变量

    注意: 表达式和路径必须都为绝对路径或都为相对路径。

    • 字段详细资料

      • DEFAULT_PATH_SEPARATOR

        public static final String DEFAULT_PATH_SEPARATOR
        默认的路径分割符: "/".
        另请参阅:
        常量字段值
    • 构造器详细资料

      • AntPathMatcher

        public AntPathMatcher​(String pathSeparator)
        使用自定义的分隔符构造
        参数:
        pathSeparator - 要使用的路径分隔符,不能是null
    • 方法详细资料

      • setPathSeparator

        public AntPathMatcher setPathSeparator​(String pathSeparator)
        设置路径分隔符
        参数:
        pathSeparator - 分隔符,null表示使用默认分隔符DEFAULT_PATH_SEPARATOR
        返回:
        this
      • setCaseSensitive

        public AntPathMatcher setCaseSensitive​(boolean caseSensitive)
        设置是否大小写敏感,默认为true
        参数:
        caseSensitive - 是否大小写敏感
        返回:
        this
      • setTrimTokens

        public AntPathMatcher setTrimTokens​(boolean trimTokens)
        设置是否去除路径节点两边的空白符,默认为false
        参数:
        trimTokens - 是否去除路径节点两边的空白符
        返回:
        this
      • isPattern

        public boolean isPattern​(@Nullable
                                 @Nullable String path)
        判断给定路径是否是表达式
        指定者:
        isPattern 在接口中 PathMatcher
        参数:
        path - 路径
        返回:
        是否为表达式
      • match

        public boolean match​(String pattern,
                             String path)
        给定路径是否匹配表达式
        指定者:
        match 在接口中 PathMatcher
        参数:
        pattern - 表达式
        path - 路径
        返回:
        是否匹配
      • matchStart

        public boolean matchStart​(String pattern,
                                  String path)
        前置部分匹配
        指定者:
        matchStart 在接口中 PathMatcher
        参数:
        pattern - 表达式
        path - 路径
        返回:
        是否匹配
      • doMatch

        protected boolean doMatch​(String pattern,
                                  String path,
                                  boolean fullMatch,
                                  Map<String,​String> uriTemplateVariables)
        执行匹配,判断给定的path是否匹配pattern
        参数:
        pattern - 表达式
        path - 路径
        fullMatch - 是否全匹配。true 表示全路径匹配,false表示只匹配开始
        uriTemplateVariables - 变量映射
        返回:
        true 表示提供的 path 匹配, false 表示不匹配
      • tokenizePattern

        protected String[] tokenizePattern​(String pattern)
        根据这个匹配器的设置,将给定的路径模式标记为各个部分

        基于setCachePatterns(boolean)执行缓存,将实际的标记化算法委托给tokenizePath(String)

        参数:
        pattern - 要标记的模式
        返回:
        标记化的模式部分
      • tokenizePath

        protected String[] tokenizePath​(String path)
        根据这个匹配器的设置,将给定的路径标记为多个部分
        参数:
        path - 标记化的路径
        返回:
        标记化的路径部分
      • extractPathWithinPattern

        public String extractPathWithinPattern​(String pattern,
                                               String path)
        给定一个模式和一个完整路径,确定模式映射部分。

        例如:

        • '/docs/cvs/commit.html' and '/docs/cvs/commit.html → ''
        • '/docs/*' and '/docs/cvs/commit → 'cvs/commit'
        • '/docs/cvs/*.html' and '/docs/cvs/commit.html → 'commit.html'
        • '/docs/**' and '/docs/cvs/commit → 'cvs/commit'
        • '/docs/**\/*.html' and '/docs/cvs/commit.html → 'cvs/commit.html'
        • '/*.html' and '/docs/cvs/commit.html → 'docs/cvs/commit.html'
        • '*.html' and '/docs/cvs/commit.html → '/docs/cvs/commit.html'
        • '*' and '/docs/cvs/commit.html → '/docs/cvs/commit.html'

        指定者:
        extractPathWithinPattern 在接口中 PathMatcher
        参数:
        pattern - 表达式
        path - 路径
        返回:
        表达式匹配到的部分
      • extractUriTemplateVariables

        public Map<String,​String> extractUriTemplateVariables​(String pattern,
                                                                    String path)
        从接口复制的说明: PathMatcher
        给定模式和完整路径,提取URI模板变量。URI模板变量通过(:xxx)表示。
        指定者:
        extractUriTemplateVariables 在接口中 PathMatcher
        参数:
        pattern - 路径模式,可能包含URI模板
        path - 要从中提取模板变量的完整路径
        返回:
        映射,包含变量名作为键;变量值作为值
      • combine

        public String combine​(String pattern1,
                              String pattern2)
        这个实现只是简单地连接两个模式,除非第一个模式包含文件扩展名匹配(例如,.html)。 在这种情况下,第二个模式将合并到第一个模式中。否则,将抛出IllegalArgumentException

        例子

        Pattern 1Pattern 2Result
        nullnull 
        /hotelsnull/hotels
        null/hotels/hotels
        /hotels/bookings/hotels/bookings
        /hotelsbookings/hotels/bookings
        /hotels/*/bookings/hotels/bookings
        /hotels/**/bookings/hotels/**/bookings
        /hotels{hotel}/hotels/{hotel}
        /hotels/*{hotel}/hotels/{hotel}
        /hotels/**{hotel}/hotels/**/{hotel}
        /*.html/hotels.html/hotels.html
        /*.html/hotels/hotels.html
        /*.html/*.txtIllegalArgumentException
        指定者:
        combine 在接口中 PathMatcher
        参数:
        pattern1 - 第一种模式
        pattern2 - 第二种模式
        返回:
        两种模式的结合
      • getPatternComparator

        public Comparator<String> getPatternComparator​(String path)
        给定一个完整的路径,返回一个Comparator,适合按显式顺序对模式进行排序。

        这个ComparatorList.sort(Comparator) 对列表进行排序, 以便更具体的模式(没有URI模板或通配符)出现在泛型模式之前。 因此,给定一个具有以下模式的列表,返回的比较器将对该列表进行排序,以便按照指示的顺序进行排序。

        1. /hotels/new
        2. /hotels/{hotel}
        3. /hotels/*

        作为参数给出的完整路径用于测试精确匹配。因此,当给定的路径是hotels2时,模式hotels2将被排序在hotels1之前。

        指定者:
        getPatternComparator 在接口中 PathMatcher
        参数:
        path - 用于比较的完整路径
        返回:
        一种能够按显式顺序对模式进行排序的比较器