詳解Spring mvc ant path的使用方法
概要:
任何一個WEB都需要解決URL與請求處理器之間的映射,spring MVC也是一樣,但Spring MVC就像Spring所作的一切一樣(靈活,可以配置各種東西,但是也造成了很多復雜性),肯定不會只有一種方法來映射URL和 Controller之間的關系,并且在實際上,允許你自己創建映射規則和實現,而不僅僅依賴URL映射。
1、Spring path match
Spring MVC中的路徑匹配要比標準的web.xml要靈活的多。默認的策略實現了 org.springframework.util.AntPathMatcher,就像名字提示的那樣,路徑模式是使用了Apache Ant的樣式路徑,Apache Ant樣式的路徑有三種通配符匹配方法(在下面的表格中列出)這些可以組合出很多種靈活的路徑模式。
2、Apache ant type
下面就來描述一下apache ant匹配規則,其實還是挺簡單的。
Ant通配符描述
Wildcard | Description |
---|---|
? | 匹配任何單字符 |
* | 匹配0或者任意數量的字符 |
** | 匹配0或者更多的目錄 |
3、舉例說明:
1、 /views/products/**/*.cfm
# Matches/views/products/index.cfm/views/products/SE10/index.cfm/views/products/SE10/details.cfm/views/products/ST80/index.cfm/views/products/ST80/details.cfm# Does Not Match:/views/index.cfm/views/aboutUs/index.cfm/views/aboutUs/managementTeam.cfm
2、 /views/**/*.cfm
# Matches:/views/index.cfm/views/aboutUs/index.cfm/views/aboutUs/managementTeam.cfm/views/products/index.cfm/views/products/SE10/index.cfm/views/products/SE10/details.cfm/views/products/ST80/index.cfm/views/products/ST80/details.cfm# Does Not Match:/views/index.htm/views/readme.txt
3、 /views/index??.cfm
# Matches:/views/index01.cfm/views/index02.cfm/views/indexAA.cfm# Does Not Match:/views/index01.htm/views/index1.cfm/views/indexA.cfm/views/indexOther.cfm/views/anotherDir/index01.cfm
如果知道這些我們就能夠在Spring MVC靈活的配置url映射了。
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答