mpmp
mpmp

Reputation: 2459

Spring: Difference of /** and /* with regards to paths

What's the difference between two asterisks instead of one asterisk when we refer to paths?

Earlier I was debugging my Spring 3 project. I was trying to add a .swf using

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

With my web.xml's ResourceServlet looking like

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

But unfortunately I was getting this error:

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

I found it really strange since all my images in the images folder were accessed but how come my .swf was "protected"?

Afterwards, I decided to change the /resources/* to /resources/** and it finally worked. My question is... why?

Upvotes: 92

Views: 58091

Answers (1)

Rangi Lin
Rangi Lin

Reputation: 9451

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.

Upvotes: 63

Related Questions