我想知道在 servlet 映射中使用 single 或 double 通配符 来描述 url 模式有什么区别。
例如:下面有什么区别?
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 是对的,规范只讨论了一个通配符 (*)。
我仔细检查了我的代码,发现我发现双通配符 (**) 的位置是在 Spring SimpleUrlHandlerMapping bean 中。
在这种情况下,这是有道理的。根据 the class doc , 它使用 AntPathMatcher ,其中指出:
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 节规定如下:
In the Web application deployment descriptor, the following syntax is used to define mappings:
- 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.
所以我猜第二个变体(**
)没有意义。
附:我刚刚尝试设置这样的映射,似乎只有这个确切的 url /shop/**
会被匹配(Tomcat 6.0.32)。
https://stackoverflow.com/questions/13843294/
相关文章:
java - 通过 Spring 将字段注入(inject) Hibernate 加载的实体中
java - 使用 Spring 将文本文件直接注入(inject) String
spring - 是否可以在表单 :select/form:option 的 itemLabel 中
spring - 不使用 Maven 下载 Spring Jars
java - Spring 的 JdbcTemplate 和事务
mysql - spring jpa application.properties useSSL
java - Spring Redis - 从 application.properties 文件中