1. 程式人生 > >SpringMVC路徑匹配規則AntPathMatcher(轉)

SpringMVC路徑匹配規則AntPathMatcher(轉)

net false tar 但是 正則表達式 pattern true regexp 測試用例

SpringMVC的路徑匹配規則是依照Ant的來的.

實際上不只是SpringMVC,整個Spring框架的路徑解析都是按照Ant的風格來的.

在Spring中的具體實現,詳情參見 org.springframework.util.AntPathMatcher.

具體規則如下(來自Spring AntPathMatcher源碼註釋):

 * [email protected] PathMatcher} implementation for Ant-style path patterns.
 *
 * <p>Part of this mapping code has been kindly borrowed from <a href="http://ant.apache.org">Apache Ant</a>.
 *
 * <p>The mapping matches URLs using the following rules:<br>
 * <ul>
 * <li>[email protected]
/* */ ?} matches one character</li> * <li>[email protected] *} matches zero or more characters</li> * <li>[email protected] **} matches zero or more <em>directories</em> in a path</li> * <li>[email protected] {spring:[a-z]+}} matches the regexp [email protected]
/* */ [a-z]+} as a path variable named "spring"</li> * </ul> * * <h3>Examples</h3> * <ul> * <li>[email protected] com/t?st.jsp} &mdash; matches [email protected] com/test.jsp} but also * [email protected] com/tast.jsp} or [email protected]
/* */ com/txst.jsp}</li> * <li>[email protected] com/*.jsp} &mdash; matches all [email protected] .jsp} files in the * [email protected] com} directory</li> * <li><code>com/&#42;&#42;/test.jsp</code> &mdash; matches all [email protected] test.jsp} * files underneath the [email protected] com} path</li> * <li><code>org/springframework/&#42;&#42;/*.jsp</code> &mdash; matches all * [email protected] .jsp} files underneath the [email protected] org/springframework} path</li> * <li><code>org/&#42;&#42;/servlet/bla.jsp</code> &mdash; matches * [email protected] org/springframework/servlet/bla.jsp} but also * [email protected] org/springframework/testing/servlet/bla.jsp} and [email protected] org/servlet/bla.jsp}</li> * <li>[email protected] com/{filename:\\w+}.jsp} will match [email protected] com/test.jsp} and assign the value [email protected] test} * to the [email protected] filename} variable</li> * </ul> * * <p><strong>Note:</strong> a pattern and a path must both be absolute or must * both be relative in order for the two to match. Therefore it is recommended * that users of this implementation to sanitize patterns in order to prefix * them with "/" as it makes sense in the context in which they‘re used.

換成人話就是:

一個一個的分析.

符號 ?

和其它幾個不一樣的是,? 要求必須為一個字符,並且不能是代表路徑分隔符的/.

@RequestMapping("/index?")
@ResponseBody
public String index(){
    System.out.println("11");
    return "11";
}

結果:

index           false 404錯誤(必須要有一個字符)
index/          false 404錯誤(不能為"/")
indexab         false 404錯誤(不能是多個字符)
indexa          true  輸出 11

符號 *

*,雖然可以匹配多個任意的字符,但是,如果你以為 * 可以替代 ** 那就錯了,* 代表的多個任意字符組成的字符串不能是個 目錄 或者說 路徑.也就是說,* 並不能拿來替代 **.

示例代碼:

@RequestMapping("/index*")
@ResponseBody
public String index(){
    System.out.println("11");
    return "11";
}

結果:

index           true  輸出 11(可以為0字符)
index/          true  輸出 11(可以為"/")
indexa          true  輸出 11(可以為1個字符)
indexabc        true  輸出 11(可以為多個字符)
index/a         false 404錯誤("/a"是一個路徑)

符號 **

0個或多個目錄.** 代表的字符串本身不一定要包含 /

@RequestMapping("/index/**/a")
@ResponseBody
public String index(){
    System.out.println();
    return "11";
}

結果:

index/a         true  輸出 11(可以為0個目錄)
index/x/a       true  輸出 11(可以為一個目錄)
index/x/z/c/a   true  輸出 11(可以為多個目錄)

符號 {spring:[a-z]+}

其它的關於 AntPathMatcher 的文章裏,對 {spring:[a-z]+} 的匹配大多是只字未提.這裏補充下.

示例代碼:

@RequestMapping("/index/{username:[a-b]+}")
@ResponseBody
public String index(@PathVariable("username") String username){
    System.out.println(username);
    return username;
}

結果:

index/ab        true  輸出 ab
index/abbaaa    true  輸出 abbaaa
index/a         false 404錯誤
index/ac        false 404錯誤

附錄(完整測試用例)

節選自 AntPathMatcherTests.不得不說 Spring 的測試用例寫的實在是太完善了.

// test exact matching
assertTrue(pathMatcher.match("test", "test"));
assertTrue(pathMatcher.match("/test", "/test"));
assertTrue(pathMatcher.match("http://example.org", "http://example.org")); // SPR-14141
assertFalse(pathMatcher.match("/test.jpg", "test.jpg"));
assertFalse(pathMatcher.match("test", "/test"));
assertFalse(pathMatcher.match("/test", "test"));

// test matching with ?‘s
assertTrue(pathMatcher.match("t?st", "test"));
assertTrue(pathMatcher.match("??st", "test"));
assertTrue(pathMatcher.match("tes?", "test"));
assertTrue(pathMatcher.match("te??", "test"));
assertTrue(pathMatcher.match("?es?", "test"));
assertFalse(pathMatcher.match("tes?", "tes"));
assertFalse(pathMatcher.match("tes?", "testt"));
assertFalse(pathMatcher.match("tes?", "tsst"));

// test matching with *‘s
assertTrue(pathMatcher.match("*", "test"));
assertTrue(pathMatcher.match("test*", "test"));
assertTrue(pathMatcher.match("test*", "testTest"));
assertTrue(pathMatcher.match("test/*", "test/Test"));
assertTrue(pathMatcher.match("test/*", "test/t"));
assertTrue(pathMatcher.match("test/*", "test/"));
assertTrue(pathMatcher.match("*test*", "AnothertestTest"));
assertTrue(pathMatcher.match("*test", "Anothertest"));
assertTrue(pathMatcher.match("*.*", "test."));
assertTrue(pathMatcher.match("*.*", "test.test"));
assertTrue(pathMatcher.match("*.*", "test.test.test"));
assertTrue(pathMatcher.match("test*aaa", "testblaaaa"));
assertFalse(pathMatcher.match("test*", "tst"));
assertFalse(pathMatcher.match("test*", "tsttest"));
assertFalse(pathMatcher.match("test*", "test/"));
assertFalse(pathMatcher.match("test*", "test/t"));
assertFalse(pathMatcher.match("test/*", "test"));
assertFalse(pathMatcher.match("*test*", "tsttst"));
assertFalse(pathMatcher.match("*test", "tsttst"));
assertFalse(pathMatcher.match("*.*", "tsttst"));
assertFalse(pathMatcher.match("test*aaa", "test"));
assertFalse(pathMatcher.match("test*aaa", "testblaaab"));

// test matching with ?‘s and /‘s
assertTrue(pathMatcher.match("/?", "/a"));
assertTrue(pathMatcher.match("/?/a", "/a/a"));
assertTrue(pathMatcher.match("/a/?", "/a/b"));
assertTrue(pathMatcher.match("/??/a", "/aa/a"));
assertTrue(pathMatcher.match("/a/??", "/a/bb"));
assertTrue(pathMatcher.match("/?", "/a"));

// test matching with **‘s
assertTrue(pathMatcher.match("/**", "/testing/testing"));
assertTrue(pathMatcher.match("/*/**", "/testing/testing"));
assertTrue(pathMatcher.match("/**/*", "/testing/testing"));
assertTrue(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla"));
assertTrue(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla/bla"));
assertTrue(pathMatcher.match("/**/test", "/bla/bla/test"));
assertTrue(pathMatcher.match("/bla/**/**/bla", "/bla/bla/bla/bla/bla/bla"));
assertTrue(pathMatcher.match("/bla*bla/test", "/blaXXXbla/test"));
assertTrue(pathMatcher.match("/*bla/test", "/XXXbla/test"));
assertFalse(pathMatcher.match("/bla*bla/test", "/blaXXXbl/test"));
assertFalse(pathMatcher.match("/*bla/test", "XXXblab/test"));
assertFalse(pathMatcher.match("/*bla/test", "XXXbl/test"));

assertFalse(pathMatcher.match("/????", "/bala/bla"));
assertFalse(pathMatcher.match("/**/*bla", "/bla/bla/bla/bbb"));

assertTrue(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing/"));
assertTrue(pathMatcher.match("/*bla*/**/bla/*", "/XXXblaXXXX/testing/testing/bla/testing"));
assertTrue(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing"));
assertTrue(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing.jpg"));

assertTrue(pathMatcher.match("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing/"));
assertTrue(pathMatcher.match("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing"));
assertTrue(pathMatcher.match("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing"));
assertFalse(pathMatcher.match("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing/testing"));

assertFalse(pathMatcher.match("/x/x/**/bla", "/x/x/x/"));

assertTrue(pathMatcher.match("/foo/bar/**", "/foo/bar")) ;

assertTrue(pathMatcher.match("", ""));

assertTrue(pathMatcher.match("/{bla}.*", "/testing.html"));    

SpringMVC路徑匹配規則AntPathMatcher(轉)

相關推薦

SpringMVC路徑匹配規則AntPathMatcher()

net false tar 但是 正則表達式 pattern true regexp 測試用例 SpringMVC的路徑匹配規則是依照Ant的來的. 實際上不只是SpringMVC,整個Spring框架的路徑解析都是按照Ant的風格來的. 在Spring中的具體實現,詳情參

SpringMVC路徑匹配規則AntPathMatcher

路徑 ont nbsp ron pat bsp mvc 正則表達式 mat ? 匹配1個字符 * 匹配0個或多個字符 ** 匹配路徑中的0個或多個目錄 {spring:[a-z]+} 將正則表達式[a-z]+匹配到的值,賦值給名為 spring 的路徑變量.(PS:必須是

Nginx 路徑匹配規則,萬用字元

Nginx路徑匹配符號 = 表示精確匹配 ^~ 表示uri以某個常規字串開頭,大多情況下用來匹配url路徑,nginx不對url做編碼,因此請求為/static/20%/aa,可以被規則^~ /static/ /aa匹配到(注意是空格)。 ~ 正則匹配(區分

springmvc 路徑對映規則、資料繫結

一、路徑對映 1. 一個action配置多個URL對映 @RequestMapping(value={“/index”, “/hello”}, method = {RequestMethod.GET}) 2. URL請求引數對映 @RequestMapping(value

Servlet/Filter路徑匹配規則

一、概述 在利用servlet或Filter進行url請求的匹配時,很關鍵的一點就是匹配規則,但servlet容器中的匹配規則既不是簡單的通配,也不是正則表示式,而是由自己的規則,比較容易混淆。本文來詳細舉例介紹下。下面的說明都是在tomcat伺服器中得到驗證的。 先介紹一

Nginx學習筆記04URL匹配規則和實際路徑

oca 定義 wid val style 例如 top font 相同 1.1.1. URL匹配規則 匹配規則配置總結: location [=|~|~*|^~] /uri/ { } 優先級 匹配方式 描述 1最高 = 精確匹配。

AntPathMatcher路徑匹配

dmi .get let lte ole neu request gets html 轉發自: http://www.cnblogs.com/leftthen/p/5212221.html 需要看詳細的請看上面的鏈接 這裏以我這裏的一個Filter 中需要對路徑做例外處

:servlet的url-pattern匹配規則詳細描述

原文地址:servlet的url-pattern匹配規則詳細描述   原文寫的很詳細 另外可以參考一下:Web.xml中設定Servlet和Filter時的url-pattern匹配規則   一、概述 在利用servlet或Filter進行url請求的匹配時,很關鍵的一點

Nginx 路徑符號匹配規則,萬用字元

1,Nginx路徑符號匹配介紹: = 表示精確匹配 ^~ 表示uri以某個常規字串開頭,大多情況下用來匹配url路徑,nginx不對url做編碼,因此請求為/static/20%/aa,可以被規則^~ /static/ /aa匹配到(注意是空格)。 ~ 正則匹配(

springmvc通過controller層自定義404頁面已經springmvc匹配規則

通常在springmvc中可以直接通過web.xml配置404自定義的頁面,但是缺少靈活性 比如如果需要根據對映地址去跳轉到不同

springmvc 路徑問題

images mage 路徑 .cn blog img image ima .com springmvc 路徑問題

iptables NAT規則

大寫 動態 www _for 之前 改變 -o -s spa nat表需要的三個鏈: 1.PREROUTING:可以在這裏定義進行目的NAT的規則,因為路由器進行路由時只檢查數據包的目的ip地址,所以為了使數據包得以正確路由,我們必須在路由之前就進行目的NAT; 2

運算放大器使用規則

布局 .com 範圍 dea src 正常 iyu 回路 容易 運算放大器,對於學理工科的學生來說是一個耳熟能詳的詞。這個知識領域在大學階段算是很有難度的了,學過電子線路的朋友一定對其不陌生,如計算增益,計算反饋等等。運算放大器作為最通用的模擬器件,廣泛運用於信號變換調理、

正則 路徑匹配

文件 目錄 bsp include url 路徑 正則表達式 http nbsp 最近一個項目需要正則匹配目錄,以實現根據路徑訪問文件或者實現下載功能! path = r‘(?P<path>[\w\d_ -/.]*)$‘urlpatterns = [ #u

【RegExp】JavaScript中正則表達式判斷匹配規則以及常用方法

返回 空字符串 tro true 正則表達式 str 本地 大小 表示範圍 字符串是編程時涉及到的最多的一種數據結構,對字符串進行操作的需求幾乎無處不在。 正則表達式是一種用來匹配字符串的強有力的武器。它的設計思想是用一種描述性的語言來給字符串定義一個規則,凡是符合規則的字

SpringMVC中遇到頁面跳出現404錯誤的問題

書寫 one 錯誤 spa size 成功 問題: scan con 今天遇到了一個問題: 使用SpringMVC時,出現頁面無法跳轉的情況(404錯誤), 出現這個異常的原因在於SpringMVC的配置文件中控制器的配置書寫錯誤: 原代碼: <context:co

xpath的匹配規則

pac 位置 文本 class xpath name baidu 匹配規則 百度搜 starts-with 匹配一個屬性開始位置的關鍵字 contains 匹配一個屬性值中包含的字符串 text() 匹配的是顯示文本信息,此處也可以用來做定位用 i.e. //input[s

struts2開發action 的三種方法以及通配符、路徑匹配原則、常量

ucc ces pan ide exce 三種 void 動態方法 div struts2開發action 的三種方法 1、繼承ActionSupport public class UserAction extends ActionSupport {

spring mvc路徑匹配原則

spring mvc路徑匹配原則在Spring MVC中經常要用到攔截器,在配置需要要攔截的路徑時經常用到<mvc:mapping/>子標簽,其有一個path屬性,它就是用來指定需要攔截的路徑的。例如:<mvc:interceptor> <mvc:mapping path

兄弟連學Python(06)---- 正則表達式匹配規則

驗證 列表 cas 斜杠 小數點 php 能夠 spa 超過 正則表達式 - 匹配規則 基本模式匹配 一切從最基本的開始。模式,是正則表達式最基本的元素,它們是一組描述字符串特征的字符。模式可以很簡單,由普通的字符串組成,也可以非常復雜,往往用特殊的字符表示一個範圍內的字