SpringMVC路徑匹配規則AntPathMatcher(轉)

來源:互聯網
上載者:User

標籤:reg   assign   替代   att   als   directory   title   方法   運算式   

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

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

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

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

 * {@link 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>{@code ?} matches one character</li> * <li>{@code *} matches zero or more characters</li> * <li>{@code **} matches zero or more <em>directories</em> in a path</li> * <li>{@code {spring:[a-z]+}} matches the regexp {@code [a-z]+} as a path variable named "spring"</li> * </ul> * * <h3>Examples</h3> * <ul> * <li>{@code com/t?st.jsp} &mdash; matches {@code com/test.jsp} but also * {@code com/tast.jsp} or {@code com/txst.jsp}</li> * <li>{@code com/*.jsp} &mdash; matches all {@code .jsp} files in the * {@code com} directory</li> * <li><code>com/&#42;&#42;/test.jsp</code> &mdash; matches all {@code test.jsp} * files underneath the {@code com} path</li> * <li><code>org/springframework/&#42;&#42;/*.jsp</code> &mdash; matches all * {@code .jsp} files underneath the {@code org/springframework} path</li> * <li><code>org/&#42;&#42;/servlet/bla.jsp</code> &mdash; matches * {@code org/springframework/servlet/bla.jsp} but also * {@code org/springframework/testing/servlet/bla.jsp} and {@code org/servlet/bla.jsp}</li> * <li>{@code com/{filename:\\w+}.jsp} will match {@code com/test.jsp} and assign the value {@code test} * to the {@code 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.

換成人話就是:

  • ? 匹配1個字元
  • * 匹配0個或多個字元
  • ** 匹配路徑中的0個或多個目錄
  • {spring:[a-z]+} 將Regex[a-z]+匹配到的值,賦值給名為 spring 的路徑變數.(PS:必須是完全符合才行,在SpringMVC中只有完全符合才會進入controller層的方法)

一個一個的分析.

符號  ?

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

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

結果:

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

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

範例程式碼:

@RequestMapping("/index*")@ResponseBodypublic 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")@ResponseBodypublic 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]+}")@ResponseBodypublic String index(@PathVariable("username") String username){    System.out.println(username);    return username;}

結果:

index/ab        true  輸出 abindex/abbaaa    true  輸出 abbaaaindex/a         false 404錯誤index/ac        false 404錯誤
附錄(完整測試案例)

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

// test exact matchingassertTrue(pathMatcher.match("test", "test"));assertTrue(pathMatcher.match("/test", "/test"));assertTrue(pathMatcher.match("http://example.org", "http://example.org")); // SPR-14141assertFalse(pathMatcher.match("/test.jpg", "test.jpg"));assertFalse(pathMatcher.match("test", "/test"));assertFalse(pathMatcher.match("/test", "test"));// test matching with ?‘sassertTrue(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 *‘sassertTrue(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 /‘sassertTrue(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 **‘sassertTrue(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(轉)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.