servlet 映射 url-pattern 上的双通配符 (*) 是什么意思? [英] What does the double wildcard (*) on a servlet mapping url-pattern mean?
问题描述
我想知道使用单或双通配符来描述servlet映射上的url模式有什么区别.
I'm wondering what is the difference of using a single or double wildcards to describe a url-pattern on a servlet mapping.
例如:下面有什么区别?
For example: what is the difference below?
1)
<servlet-mapping id="...">
<servlet-name>BuyServlet</servlet-name>
<url-pattern>/buy/*</url-pattern>
</servlet-mapping>
2)
<servlet-mapping id="...">
<servlet-name>ShopServlet</servlet-name>
<url-pattern>/shop/**</url-pattern>
</servlet-mapping>
@Andrew 是对的,规范只讨论了一个通配符 (*).
@Andrew is right, the specification talks about only one wildcard (*).
我仔细检查了我的代码并注意到我发现双通配符 (**) 的地方在 Spring SimpleUrlHandlerMapping bean 中.
I double checked my code and noticed that the place where I found double wildcards (**) was in a Spring SimpleUrlHandlerMapping bean.
在这种情况下,这是有道理的.根据 类文档,它使用 AntPathMatcher,其中说明:
In that case, it makes sense. As per the class doc, it uses AntPathMatcher, which states:
映射使用以下规则匹配 URL: ?匹配一个特点* 匹配零个或多个字符** 匹配路径中的零个或多个目录"
The mapping matches URLs using the following rules: ? matches one character * matches zero or more characters ** matches zero or more 'directories' in a path
推荐答案
servlet 规范(2.5 版)的第 11.2 节声明如下:
Section 11.2 of servlet specification (version 2.5) states the following:
在 Web 应用程序部署描述符中,使用以下语法来定义映射:
In the Web application deployment descriptor, the following syntax is used to define mappings:
- 使用以/"字符开头并以/*"后缀结尾的字符串用于路径映射.
- 以*."前缀开头的字符串用作扩展映射.
- 仅包含/"字符的字符串表示默认"servlet应用程序.在这种情况下,servlet 路径是请求 URI 减去上下文路径,路径信息为空.
- 所有其他字符串仅用于精确匹配.
所以我猜第二个变体(**
)没有意义.
So I guess the second variant (**
) doesn't make sense.
附言我刚刚尝试设置这样的映射,似乎只有这个确切的 url /shop/**
才会匹配(Tomcat 6.0.32).
P.S. I've just tried to set up such mapping and it seems that only this exact url /shop/**
will be matched (Tomcat 6.0.32).
这篇关于servlet 映射 url-pattern 上的双通配符 (*) 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!