实际上不只是SpringMVC,整个Spring框架的路径解析都是按照Ant的风格来的,在Spring中的具体实现,详情参见 org.springframework.util.AntPathMatcher,具体规则如下
a")
@ResponseBody
public String index(){
return "index.html";
}
复制代码
结果:
index/a true 输出 index.html(可以为0个目录) index/x/a true 输出 index.html(可以为一个目录) index/x/z/c/a true 输出 index.html(可以为多个目录) 复制代码
符号 {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错误
需求:我在做rbac权限校验的时候,设置管理员的访问路径为/admin.jsp和/app/dir/.jsp,那么会根据模式/app/dir**", "/testing/testing")); assertTrue(pathMatcher.match("*", "/testing/testing")); assertTrue(pathMatcher.match("/blabla", "/bla/testing/testing/bla")); assertTrue(pathMatcher.match("/blabla", "/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", "/bla/bla/bla/bbb")); assertTrue(pathMatcher.match("**/bla**/bla**/bla**/bla**/bla**/bla**/bla**/blabla", "/x/x/x/")); assertTrue(pathMatcher.match("/foo/barexample 、/app/.x 类似于这样语法而负责真正判断是否匹配的工具类就是AntPathRequestMatcher



