Ant风格的路径表达式在日常开发中进行一些系统配置的时候非常有用,如:在 web.xml 中配置 org.springframework.web.servlet.DispatcherServlet 的 contextConfigLocation 属性,因为有可能你的项目中存在多个Spring配置文件,此时你就需要通过Ant风格表达式来模糊匹配你的所有想要加载的配置文件。如下:
// controller org.springframework.web.servlet.DispatcherServlet // contextConfigLocation classpath*:controllerContext.xml
ANT通配符有三种:
通配符 | 说明 |
? | 匹配任何单字符。 |
* | 匹配0或者任意数量的字符。 |
** | 匹配0或者更多的目录。 |
实例:
URL路径 | 说明 |
/config/*.xml | 匹配config目录下面的所有后缀为.xml的文件 |
/config/p?ttern | 匹配/config/pattern 和 /config/pJttern等等,但是不包括/app/pttern |
/**/test | 匹配/com/bug315/test, com/bug315/web/test, 和 /test等等 |
/config/**/dir/readme.* | 匹配/com/bug315/dir/readme.text, /com/bug315/java/dir/readme.html,/com/bug315/web/dir/readme.pdf等等 |
/**/*.class | 匹配任何的.class 文件(注:.class是java的字节码文件) |
属性:
最长匹配原则(has more characters),URL请求/app/dir/file.jsp,现在存在两个路径匹配模式/**/*.jsp和/app/dir/*.jsp,那么会根据模式/app/dir/*.jsp来匹配。