欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring中PathMatcher路徑匹配器的實(shí)現(xiàn)

 更新時(shí)間:2024年07月11日 11:31:33   作者:濤哥是個(gè)大帥比  
Spring框架中的PathMatcher是一個(gè)接口,本文主要介紹了Spring中PathMatcher路徑匹配器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

概述:

PathMatcher是Spring的一個(gè)概念模型接口,該接口抽象建模了概念"路徑匹配器",一個(gè)"路徑匹配器"是一個(gè)用于路徑匹配的工具。位于 Spring-core 包中 util 包下。

PathMatcher 接口源碼

package org.springframework.util;
 
import java.util.Comparator;
import java.util.Map;
 
public interface PathMatcher {
 
    /**
     * Does the given path represent a pattern that can be matched
     * by an implementation of this interface?
     * 判斷指定的路徑 path 是否是一個(gè) pattern(模式)
     * 如果返回值是 false,也就是說 path 不是一個(gè)模式,而是一個(gè)靜態(tài)路徑(真正的路徑字符串),
     * 那么就不用調(diào)用方法 #match 了,因?yàn)閷?duì)于靜態(tài)路徑的匹配,直接使用字符串等號(hào)比較就足夠了。
     * @param path the path String to check
     * @return true if the given path represents a pattern
     */
    boolean isPattern(String path);
 
    /**
     * Match the given path against the given pattern,
     * according to this PathMatcher's matching strategy.
     * 根據(jù)當(dāng)前 PathMatcher 的匹配策略,檢查指定的徑 path 和指定的模式 pattern 是否匹配
     * @param 用于檢測(cè)路徑字符串是否匹配于某個(gè)模式時(shí)所用的模式
     * @param path 需要被檢測(cè)的路徑字符串
     * @return true 表示匹配, false 表示不匹配
     */
    boolean match(String pattern, String path);
 
    /**
     * Match the given  path against the corresponding part of the given
     * pattern, according to this PathMatcher's matching strategy.
     * 根據(jù)當(dāng)前 PathMatcher 的匹配策略,檢查指定的徑 path 和指定的模式 pattern 是否之間
     * 是否為前綴匹配
     * @param pattern the pattern to match against
     * @param path the path String to test
     * @return true 表示匹配, false 表示不匹配
     */
    boolean matchStart(String pattern, String path);
 
    /**
     * Given a pattern and a full path, determine the pattern-mapped part.
     * 給定一個(gè)模式 pattern 和一個(gè)全路徑 path,判斷路徑中和模式匹配的部分。
     *
     * This method is supposed to find out which part of the path is matched
     * dynamically through an actual pattern, that is, it strips off a statically
     * defined leading path from the given full path, returning only the actually
     * pattern-matched part of the path.
     * 該方法用于發(fā)現(xiàn)路徑中的哪一部分是和模式能動(dòng)態(tài)匹配上的部分。它會(huì)去除路徑中開頭靜態(tài)部分,
     * 僅僅返回那部分真正和模式匹配的上的部分。
     * 例子 : "myroot/*.html" 為 pattern , "myroot/myfile.html" 為路徑,
     *  則該方法返回 "myfile.html".  
     * 具體的檢測(cè)規(guī)則根據(jù)當(dāng)前 PathMatcher 的匹配策略來頂。    
     * A simple implementation may return the given full path as-is in case
     * of an actual pattern, and the empty String in case of the pattern not
     * containing any dynamic parts (i.e. the pattern parameter being
     * a static path that wouldn't qualify as an actual #isPattern pattern.
     * A sophisticated implementation will differentiate between the static parts
     * and the dynamic parts of the given path pattern.
     * @param pattern the path pattern
     * @param path the full path to introspect
     * @return the pattern-mapped part of the given path
     * (never null)
     */
    String extractPathWithinPattern(String pattern, String path);
 
    /**
     * Given a pattern and a full path, extract the URI template variables. URI template
     * variables are expressed through curly brackets ('{' and '}').
     * 給定一個(gè)模式和一個(gè)路徑,提取其中的 URI 模板變量信息。URI模板變量表達(dá)式格式為 "{variable}"
     *      
     * 例子 : pattern  為 "/hotels/{hotel}" ,路徑為 "/hotels/1", 則該方法會(huì)返回一個(gè) map ,
     * 內(nèi)容為 : "hotel"->"1".
     * @param pattern the path pattern, possibly containing URI templates
     * @param path the full path to extract template variables from
     * @return a map, containing variable names as keys; variables values as values
     */
    Map<String, String> extractUriTemplateVariables(String pattern, String path);
 
    /**
     * Given a full path, returns a Comparator suitable for sorting patterns
     * in order of explicitness for that path.
     * The full algorithm used depends on the underlying implementation,
     * but generally, the returned Comparator will sort a list so that more 
     * specific patterns come before generic patterns.
     * @param path the full path to use for comparison
     * @return a comparator capable of sorting patterns in order of explicitness
     */
    Comparator<String> getPatternComparator(String path);
 
    /**
     * Combines two patterns into a new pattern that is returned.
     * The full algorithm used for combining the two pattern depends on the underlying implementation.
     * 合并兩個(gè)模式。具體合并的算法由實(shí)現(xiàn)類決定。
     * @param pattern1 the first pattern
     * @param pattern2 the second pattern
     * @return the combination of the two patterns
     * @throws IllegalArgumentException when the two patterns cannot be combined
     */
    String combine(String pattern1, String pattern2);
 
}

AntPathMatcher:

Spring框架自身對(duì)概念模型接口也提供了一個(gè)缺省的實(shí)現(xiàn)AntPathMatcher,用于匹配Ant風(fēng)格的路徑。我們平時(shí)使用的路徑匹配基本用的都是這個(gè)。

1.通配符:

? 表示匹配任意一個(gè)字符

System.out.println( matcher.match("/usr/name?", "/usr/name1") );  //true
System.out.println( matcher.match("/usr/name?", "/usr/name111") );  //false

* 表示匹配單個(gè)路徑

System.out.println( matcher.match("/usr/*", "/usr/list")); //true
System.out.println( matcher.match("/usr/*", "/usr/list/again")); //false
System.out.println( matcher.match("/usr/*.app", "/usr/list.app")); //true
System.out.println( matcher.match("/usr/*.app", "/usr/list.html")); //false

 ** 表示匹配多級(jí)路徑

System.out.println( matcher.match("/usr/**", "/usr/list") );  //true
System.out.println( matcher.match("/usr/**", "/usr/list/again") );  //true

變量綁定匹配

System.out.println( matcher.match("/usr/{name}/{age}", "/usr/張三/12") );  //true
System.out.println( matcher.match("/usr/{name}/{age}", "/usr/張三") );  //false

 通過 extractPathWithinPattern() 方法匹配

/**
 * extractPathWithinPattern 返回符合匹配的部分路徑,去除路徑開頭靜態(tài)部分
 **/
System.out.println( matcher.extractPathWithinPattern("/usr/*", "/usr/list") );   //list
System.out.println( matcher.extractPathWithinPattern("/usr/*/again", "/usr/list/again/age") );   // list/again/age
System.out.println( matcher.extractPathWithinPattern("/usr/*.html", "/usr/list") );  //list
System.out.println( matcher.extractPathWithinPattern("/usr/*.html", "/usr/list.html") );  //list.html

2.變量綁定

變量綁定可以讓我們?cè)谄ヅ溥^程中提取路徑中的參數(shù),并將其賦值給指定的變量。

System.out.println( matcher.extractUriTemplateVariables("/usr/{name}/{age}", "/usr/張三/12") ); //{name=張三, age=12}

到此這篇關(guān)于Spring中PathMatcher路徑匹配器的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Spring PathMatcher路徑匹配器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Java代碼實(shí)現(xiàn)矩形覆蓋實(shí)例

    Java代碼實(shí)現(xiàn)矩形覆蓋實(shí)例

    這篇文章主要介紹了Java代碼實(shí)現(xiàn)矩形覆蓋實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下
    2019-06-06
  • SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問題

    SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問題

    這篇文章主要介紹了SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java日志框架之logback使用詳解

    Java日志框架之logback使用詳解

    這篇文章主要介紹了Java日志框架之logback使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Java class文件格式之常量池_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java class文件格式之常量池_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了Java class文件格式之常量池的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處

    java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處

    java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處,需要的朋友可以參考一下
    2013-03-03
  • Java內(nèi)存溢出的幾個(gè)區(qū)域總結(jié)(注意避坑!)

    Java內(nèi)存溢出的幾個(gè)區(qū)域總結(jié)(注意避坑!)

    內(nèi)存溢出是指應(yīng)用系統(tǒng)中存在無法回收的內(nèi)存或使用的內(nèi)存過多,最終使得程序運(yùn)行要用到的內(nèi)存大于虛擬機(jī)能提供的最大內(nèi)存,下面這篇文章主要給大家介紹了關(guān)于Java內(nèi)存溢出的幾個(gè)區(qū)域,總結(jié)出來給大家提醒注意避坑,需要的朋友可以參考下
    2022-11-11
  • 如何利用Java正則表達(dá)式校驗(yàn)密碼規(guī)則

    如何利用Java正則表達(dá)式校驗(yàn)密碼規(guī)則

    正則表達(dá)式正則表達(dá)式是用來指定字符串模式的,可以方便的處理文本信息,這篇文章主要給大家介紹了關(guān)于如何利用Java正則表達(dá)式校驗(yàn)密碼規(guī)則的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • 初學(xué)者Android studio安裝圖文詳解

    初學(xué)者Android studio安裝圖文詳解

    本文給大家分享android studio 安裝圖文詳解包括下載安裝及遇到的問題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-09-09
  • 適用于Java初學(xué)者的學(xué)習(xí)路線圖

    適用于Java初學(xué)者的學(xué)習(xí)路線圖

    這篇文章主要介紹了學(xué)習(xí)Java的路線圖的五個(gè)必經(jīng)階段,還有一些作者的想法分享給大家,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Java如何獲取相對(duì)路徑文件

    Java如何獲取相對(duì)路徑文件

    這篇文章主要介紹了Java如何獲取相對(duì)路徑文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06

最新評(píng)論