使用 Spring 的 @RequestMapping 和通配符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5954793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Spring's @RequestMapping with wildcards
提问by Tony R
This is similar to this question, but I am still confused about my situation. I want to map this ant-style pattern to a controller method:
这与this question类似,但我仍然对我的情况感到困惑。我想将此蚂蚁样式的模式映射到控制器方法:
/results/**
That is, I want any URL like www.hostname.com/MyServlet/results/123/abc/456/def/to go to this method. I have:
也就是说,我希望任何 URL 都www.hostname.com/MyServlet/results/123/abc/456/def/可以转到此方法。我有:
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/results/*</url-pattern>
</servlet-mapping>
and:
和:
@RequestMapping(value="/**", method=RequestMethod.GET)
public ModelAndView handleRequest() {...}
This works to guide the request to my method, but leads me to several questions:
这可以将请求引导到我的方法,但会导致我提出几个问题:
- What if I add another servlet mapping, like
<url-pattern>/another-mapping/*</url-pattern>??? It will also get mapped to that method! How can I separate the two? - Why does the url-pattern
/results/*work, whereas/results/**doesn't? According to ant path styles,**means to include nested/characters, whereas*stops at the next/. So, it should only successfully map a URL like/results/123, bot NOT/results/123/abc/. Right?
- 如果我添加另一个 servlet 映射,比如
<url-pattern>/another-mapping/*</url-pattern>??? 它也将映射到该方法!我怎样才能将两者分开? - 为什么 url-pattern
/results/*有效,而/results/**无效?根据蚂蚁路径样式,**意味着包含嵌套/字符,而*在下一个/. 因此,它应该只成功映射一个 URL,如/results/123, bot NOT/results/123/abc/。对?
回答by nicholas.hauschild
Perhaps in your servlet mapping you would want to direct all traffic to '/*'. This way, you can distinguish in your controller what method to use with different @RequestMapping's.
也许在您的 servlet 映射中,您希望将所有流量定向到“/*”。通过这种方式,您可以在控制器中区分使用不同 @RequestMapping 的方法。
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
and
和
@RequestMapping(value="/results/**", method=RequestMethod.GET)
public ModelAndView handleResults() {...}
@RequestMapping(value="/another-mapping/**", method=RequestMethod.GET)
public ModelAndView handleAnotherMapping() {...}
Hopefully the above will help with number 1. As far as number 2 goes, I do not think that you can use 'ant-style' pattern matchers (specifically **) in your web.xml domain descriptor.
希望以上内容对第 1 点有所帮助。就第 2 点而言,我认为您不能在 web.xml 域描述符中使用“ant-style”模式匹配器(特别是 **)。
回答by axtavt
What if I add another servlet mapping, like /another-mapping/*??? It will also get mapped to that method! How can I separate the two?
如果我添加另一个 servlet 映射,比如 /another-mapping/*??? 它也将映射到该方法!我怎样才能将两者分开?
With your current configuration you cannot. If you want to map DispatcherServletto multiple URL patterns and distinguish between them, you can declare DefaultAnnotationHandlerMappingwith alwaysUseFullPath = "true"and use full path in @RequestMapping.
使用您当前的配置,您不能。如果你想映射DispatcherServlet到多个URL模式以及它们之间的区别,你可以声明DefaultAnnotationHandlerMapping与alwaysUseFullPath = "true"和使用的完整路径@RequestMapping。
Alternatively, you can map DispatcherServletas <url-pattern>/*</url-pattern>and use full path in @RequestMappingwithout reconfiguring DefaultAnnotationHandlerMapping. Though in this case you'll need to configre exclusions for static content.
或者,您可以映射DispatcherServlet为<url-pattern>/*</url-pattern>并使用完整路径,@RequestMapping而无需重新配置DefaultAnnotationHandlerMapping. 尽管在这种情况下,您需要为静态内容配置排除项。
Why does the url-pattern /results/* work, whereas /results/** doesn't? According to ant path styles, ** means to include nested / characters, whereas * stops at the next /. So, it should only successfully map a URL like /results/123, bot NOT /results/123/abc/. Right?
为什么 url-pattern /results/* 有效,而 /results/** 无效?根据蚂蚁路径样式,** 表示包含嵌套的 / 字符,而 * 停止在下一个 /。所以,它应该只成功映射一个像 /results/123 这样的 URL,而不是 /results/123/abc/。对?
URL patterns in web.xmlare not ant-style patterns, so that only .../*and *.xxxwildcards are allowed in them.
中的 URL 模式web.xml不是 ant 风格的模式,因此它们中只允许使用.../*和*.xxx通配符。

