Spring: 关于路径的/* * 和/* 的区别

当我们引用路径时,两个星号和一个星号的区别是什么?

早些时候我正在调试我的 Spring3项目

<spring:url var="flashy" value="/resources/images/flash.swf"/>

使我的 web.xml 的 ResourceServlet 看起来像

<servlet-name>Resource Servlet </servlet-name>
<url-pattern>/resources/*</url-pattern>

但不幸的是,我得到了这个错误:

WARN org.springframework.js.resources.ResourceServlet - An attempt to access a protected resource at /images/flash.swf was disallowed.

我发现这真的很奇怪,因为我所有的图像在 images文件夹被访问,但为什么我的。SWF 被“保护”了?

后来,我决定把 /resources/*改成 /resources/**,它终于起作用了。我的问题是... ... 为什么?

52403 次浏览

This is a path pattern that is used in Apache Ant library. Spring team implements it and uses it throughout the framework.


Back to your problem. According to the Javadoc for AntPathMatcher, it only has 3 rules:

  1. ? matches one character
  2. * matches zero or more characters
  3. ** matches zero or more 'directories' in a path

UPDATE 2022

In the latest Spring Framework versions there is a forth rule:

  1. {spring:[a-z]+} matches the regexp [a-z]+ as a path variable named "spring"

See the details in the latest (as of now) Spring Framework version 5 Javadoc: AntPathMathcer.