使用 Spring 3 RequestMapping Annotation 匹配“URL 的其余部分”[重复]

2024-01-04

可能的重复:
Spring 3 RequestMapping:获取路径值 https://stackoverflow.com/questions/3686808/spring-3-requestmapping-get-path-value

在Spring 3中,有没有办法捕获rest/of/the/url在以下网址中:

/myapp/foo/bar/rest/of/the/url

通过使用像这样的 @RequestMapping 注释:

@RequestMapping(value="{appname}/{path1}/{path2}/{remainder}")
public String myRequestMethod(
    @PathVariable("appname") String appName, 
    PathVariable("path1") String path1, 
    PathVariable("path2") String path2,
    PathVariable("remainder") String remainder)

我希望 RequestMapping 像这样匹配

{appname}   -> myapp
{path1}     -> foo
{path2}     -> bar
{remainder} -> rest/of/the/url 

In the Javadocs http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html对于 RequestMapping,有一个关于使用备用正则表达式的注释:

默认情况下,URI 模板将与正则表达式 [^.]* 匹配(即句点以外的任何字符),但这可以通过指定另一个正则表达式来更改,如下所示: /hotels/{hotel:\d+}

但是当我像这样使用 RequestMapping 时,这不会按预期运行(我得到 404):

@RequestMapping(value="{appname}/{path1}/{path2}/{remainder:.[\\S]*}")

有谁知道如何将 URL 的其余部分与 Spring RequestMapping 相匹配?


有趣的是:我也刚刚发现需要这样做。我是这样解决的:

@RequestMapping(value = {"/someChildUrlIfYouWant/**"}

The "**"这里说“抓住我左边的任何子路径上的任何东西。”如果你想要它实际匹配的路径来到达这个方法,你可以使用:

request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE )

你会得到/someChildUrlIfYouWant/the/full/path/to/whatever.html因此,如果您只想要可变子路径,则必须修剪字符串的前部。

合理?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Spring 3 RequestMapping Annotation 匹配“URL 的其余部分”[重复] 的相关文章

随机推荐