使用 Spring 的 @RequestMapping 和通配符
这类似于这个问题,但我仍然对我的情况感到困惑。我想将这个蚂蚁风格的模式映射到控制器方法:
/results/**
也就是说,我想要像 www.hostname.com/MyServlet/results/123/abc/456/def/
这样的任何 URL 转到这个方法。我有:
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/results/*</url-pattern>
</servlet-mapping>
并且:
@RequestMapping(value="/**", method=RequestMethod.GET)
public ModelAndView handleRequest() {...}
这可以将请求引导到我的方法,但导致我提出几个问题:
- 如果我添加另一个 servlet 映射,例如
???它也将被映射到该方法!我怎样才能将两者分开?/another-mapping/* - 为什么 url 模式
/results/*
有效,而/results/**
无效?根据 ant 路径样式,**
表示包含嵌套的/
字符,而*
停在下一个/
处。因此,它应该只能成功映射/results/123
这样的 URL,而不是/results/123/abc/
。正确的?
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:
/results/**
That is, I want any URL like www.hostname.com/MyServlet/results/123/abc/456/def/
to go to this method. I have:
<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许在您的 servlet 映射中您希望将所有流量定向到“/*”。这样,您可以在控制器中区分使用不同的 @RequestMapping 的方法。
希望
上述内容对第 1 点有所帮助。就第 2 点而言,我认为您不能在 web.xml 域描述符中使用“ant 风格”模式匹配器(特别是 **)。
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.
and
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.
根据您当前的配置,您不能。如果要将
DispatcherServlet
映射到多个 URL 模式并区分它们,可以使用alwaysUseFullPath = "true"
声明DefaultAnnotationHandlerMapping
并使用完整路径在@RequestMapping中。或者,您可以将
DispatcherServlet
映射为/*
并在@RequestMapping
中使用完整路径无需重新配置DefaultAnnotationHandlerMapping
。尽管在这种情况下,您需要 配置静态内容的排除。web.xml
中的 URL 模式不是 ant 风格的模式,因此仅允许使用.../*
和*.xxx
通配符他们。With your current configuration you cannot. If you want to map
DispatcherServlet
to multiple URL patterns and distinguish between them, you can declareDefaultAnnotationHandlerMapping
withalwaysUseFullPath = "true"
and use full path in@RequestMapping
.Alternatively, you can map
DispatcherServlet
as<url-pattern>/*</url-pattern>
and use full path in@RequestMapping
without reconfiguringDefaultAnnotationHandlerMapping
. Though in this case you'll need to configre exclusions for static content.URL patterns in
web.xml
are not ant-style patterns, so that only.../*
and*.xxx
wildcards are allowed in them.