Spring MVC ant路径匹配原则,RequestMapping

简介

匹配原则

Ant风格按以下规则匹配URL:

通配符 说明
? 匹配一个字符
* 匹配零个或多个字符
** 匹配路径中的零个或多个目录
{spring:[a-z]+}} 配置正则表达式 [a-z]+ 作为spring的路径变量(4.3)

例子

URL路径 说明
com/t?st.jsp 匹配com/test.jsp,但也匹配com/tast.jsp或com/txst.jsp
com/*.jsp 匹配com目录中的所有.jsp文件,不包括子文件夹
com/**/test.jsp 匹配com路径下的所有test.jsp文件包括子文件夹
org/springframework/**/*.jsp 匹配org/springframework路径下的所有.jsp文件,包括子文件夹
org/**/servlet/bla.jsp 匹配 org/springframework/servlet/bla.jsp 也包括 org/springframework/testing/servlet/bla.jsp 和 org/servlet/bla.jsp
com/{filename:\\w+}.jsp} 将匹配com/test.jsp并将值test分配给filename变量(4.3)

按最长匹配原则匹配

4.3表示Spring MVC4.3中才引入的特性。

URI模板模式可能包含正则表达式:

@RequestMapping (“/ {userId:[0-9] +}” )

URI模板可以通过使用@PathVariable以下方法注释的handler的方法参数捕获:

 @RequestMapping (“/ {userId}” )public void handle (@PathVariable (“userId” )String userId ){ // ... }

猜你喜欢

转载自blog.csdn.net/u010843868/article/details/81129227
今日推荐