问题描述
我想知道使用单个或双通配符来描述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.
在这种情况下,它是有道理的。根据,它使用,其中声明:
In that case, it makes sense. As per the class doc, it uses AntPathMatcher, which states:
推荐答案
servlet规范(版本2.5)的第11.2节规定如下:
Section 11.2 of servlet specification (version 2.5) states the following:
- 以'/开头的字符串'字符和以'/ *'后缀结尾使用
进行路径映射。 - 以'*。'前缀开头的字符串用作扩展映射。
- 仅包含'/'字符的字符串表示应用程序
的默认servlet。在这种情况下,servlet路径是请求URI减去上下文
路径,路径信息为null。 - 所有其他字符串仅用于完全匹配。
- A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.
- A string beginning with a ‘*.’ prefix is used as an extension mapping.
- A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
- All other strings are used for exact matches only.
所以我猜第二个变种( **
)没有意义。
So I guess the second variant (**
) doesn't make sense.
PS我刚刚尝试设置这样的映射,似乎只有这个精确的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上的双通配符(*)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!