servlet映射url-pattern上的双通配符(*)是什么意思? [英] What does the double wildcard (*) on a servlet mapping url-pattern mean?

查看:169
本文介绍了servlet映射url-pattern上的双通配符(*)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用单个双通配符来描述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.

在这种情况下,它是有道理的。根据类doc ,它使用 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减去上下文
    路径,路径信息为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上的双通配符(*)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆