使用 Spring 的 @RequestMapping 和通配符

发布于 2024-11-07 00:51:32 字数 1032 浏览 2 评论 0原文

这类似于这个问题,但我仍然对我的情况感到困惑。我想将这个蚂蚁风格的模式映射到控制器方法:

/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() {...}

这可以将请求引导到我的方法,但导致我提出几个问题:

  1. 如果我添加另一个 servlet 映射,例如 /another-mapping/*???它也将被映射到该方法!我怎样才能将两者分开?
  2. 为什么 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:

  1. 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?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落□心尘 2024-11-14 00:51:32

也许在您的 servlet 映射中您希望将所有流量定向到“/*”。这样,您可以在控制器中区分使用不同的 @RequestMapping 的方法。

<servlet-mapping>
  <servlet-name>MyServlet</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

希望

@RequestMapping(value="/results/**", method=RequestMethod.GET)
public ModelAndView handleResults() {...}

@RequestMapping(value="/another-mapping/**", method=RequestMethod.GET)
public ModelAndView handleAnotherMapping() {...}

上述内容对第 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.

<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.

腻橙味 2024-11-14 00:51:32

如果我添加另一个 servlet 映射(例如 /another-mapping/*)会怎样???它也将被映射到该方法!如何将两者分开?

根据您当前的配置,您不能。如果要将 DispatcherServlet 映射到多个 URL 模式并区分它们,可以使用 alwaysUseFullPath = "true" 声明 DefaultAnnotationHandlerMapping 并使用完整路径在@RequestMapping中。

或者,您可以将 DispatcherServlet 映射为 /* 并在 @RequestMapping 中使用完整路径无需重新配置 DefaultAnnotationHandlerMapping。尽管在这种情况下,您需要 配置静态内容的排除

为什么 url-pattern /results/* 有效,而 /results/** 无效?根据 ant 路径样式,** 表示包含嵌套的 / 字符,而 * 停在下一个 / 处。因此,它应该只能成功映射像 /results/123 这样的 URL,而不是 /results/123/abc/。对吗?

web.xml 中的 URL 模式不是 ant 风格的模式,因此仅允许使用 .../**.xxx 通配符他们。

What if I add another servlet mapping, like /another-mapping/*??? It will also get mapped to that method! How can I separate the two?

With your current configuration you cannot. If you want to map DispatcherServlet to multiple URL patterns and distinguish between them, you can declare DefaultAnnotationHandlerMapping with alwaysUseFullPath = "true" and use full path in @RequestMapping.

Alternatively, you can map DispatcherServlet as <url-pattern>/*</url-pattern> and use full path in @RequestMapping without reconfiguring DefaultAnnotationHandlerMapping. Though in this case you'll need to configre exclusions for static content.

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 patterns in web.xml are not ant-style patterns, so that only .../* and *.xxx wildcards are allowed in them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文