蚂蚁路径样式图案

IT小君   2021-10-13T22:25:05

Ant 路径样式模式的规则是什么

Ant 站点本身的信息量出奇地少。

评论(5)
IT小君

Ant 风格的路径模式匹配

映射使用以下规则匹配 URL:

  • ? 匹配一个字符
  • * 匹配零个或多个字符
  • ** 匹配路径中的零个或多个“目录”
  • {spring:[a-z]+}匹配正则表达式[a-z]+作为名为“spring”的路径变量

一些例子:

  • com/t?st.jsp- 匹配 com/test.jsp 但也匹配com/tast.jspcom/txst.jsp
  • com/*.jsp- 匹配目录中的所有.jsp文件com
  • com/**/test.jsp- 匹配路径test.jsp下的所有文件com
  • org/springframework/**/*.jsp- 匹配.jsp下面的所有文件org/springframework path
  • org/**/servlet/bla.jsp- 匹配org/springframework/servlet/bla.jsp但也org/springframework/testing/servlet/bla.jsporg/servlet/bla.jsp
  • com/{filename:\\w+}.jsp将匹配com/test.jsp和的值分配testfilename变量

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html

2021-10-13T22:25:06   回复
IT小君

我想你的意思是如何使用路径模式

如果是关于使用斜杠还是反斜杠,这些将被转换为执行时使用的平台上的路径分隔符。

2021-10-13T22:25:06   回复
IT小君

ANT 样式模式匹配器

通配符

该实用程序使用三种不同的通配符。

+----------+-----------------------------------+
| Wildcard |            Description            |
+----------+-----------------------------------+
| *        | Matches zero or more characters.  |
| ?        | Matches exactly one character.    |
| **       | Matches zero or more directories. |
+----------+-----------------------------------+
2021-10-13T22:25:06   回复
IT小君

通过@user11153使用表格来获得更易读的格式,最受好评的答案


映射使用以下规则匹配 URL:

+-----------------+---------------------------------------------------------+
| Wildcard        |            Description                                  |
+-----------------+---------------------------------------------------------+
| ?               | Matches exactly one character.                          |
| *               | Matches zero or more characters.                        |
| **              | Matches zero or more 'directories' in a path            |
| {spring:[a-z]+} | Matches regExp [a-z]+ as a path variable named "spring" |
+-----------------+---------------------------------------------------------+

一些例子:

+------------------------------+--------------------------------------------------------+
| Example                      | Matches:                                               |
+------------------------------+--------------------------------------------------------+
| com/t?st.jsp                 | com/test.jsp but also com/tast.jsp or com/txst.jsp     |
| com/*.jsp                    | All .jsp files in the com directory                    |
| com/**/test.jsp              | All test.jsp files underneath the com path             |
| org/springframework/**/*.jsp | All .jsp files underneath the org/springframework path |
| org/**/servlet/bla.jsp       | org/springframework/servlet/bla.jsp                    |
|                       also:  | org/springframework/testing/servlet/bla.jsp            |
|                       also:  | org/servlet/bla.jsp                                    |
| com/{filename:\\w+}.jsp      | com/test.jsp & assign value test to filename variable  |
+------------------------------+--------------------------------------------------------+
2021-10-13T22:25:06   回复
IT小君

正如@user11153 提到的,Spring 的AntPathMatcher实现并记录了 Ant 样式路径模式匹配的基础知识。

此外,Java 7 的 nio API 通过FileSystem.getPathMatcher添加了一些对基本模式匹配的内置支持

2021-10-13T22:25:06   回复