使用spring的AntPathMatcher匹配url路徑

spring的AntPathMatcher用來匹配url比較好用,該類的部分代碼借鑑與apache ant故命名爲AntPathMatcher,

The mapping matches URLs using the following rules:

匹配url遵循如下規則:

  • ? matches one character(匹配一個字符)
  • * matches zero or more characters(匹配0個或多個字符)
  • ** matches zero or more directories in a path(匹配0個或多個目錄)
  • {spring:[a-z]+}} matches the regexp [a-z]+ as a path variable named "spring",意思是將使用[a-z]+ 正則去匹配spring的變量值

栗子:

  • com/t?st.jsp — matches com/test.jsp but also com/tast.jsp or com/txst.jsp
  • com/*.jsp — matches all .jsp files in the com directory
  • com/**/test.jsp — matches all test.jsp files underneath the com path
  • org/springframework/**/*.jsp — matches all .jsp files underneath the org/springframework path
  • org/**/servlet/bla.jsp — matches org/springframework/servlet/bla.jsp but also org/springframework/testing/servlet/bla.jsp and org/servlet/bla.jsp
  • com/{filename:\\w+}.jsp} will match com/test.jsp and assign the value test to the filename variable

注意的是:

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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章