在Spring 3中,有没有办法rest/of/the/url在以下URL中捕获:
/myapp/foo/bar/rest/of/the/url
Run Code Online (Sandbox Code Playgroud)
通过使用这样的@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)
Run Code Online (Sandbox Code Playgroud)
我希望RequestMapping像这样匹配
{appname} -> myapp
{path1} -> foo
{path2} -> bar
{remainder} -> rest/of/the/url
Run Code Online (Sandbox Code Playgroud)
在RequestMapping 的Javadocs中,有一个关于使用备用正则表达式的注释:
默认情况下,URI模板将匹配正则表达式[^.]*(即句点以外的任何字符),但可以通过指定另一个正则表达式来更改,如:/ hotels/{hotel:\ d +}
但是当我像这样使用RequestMapping时,这不会像预期的那样(我得到404):
@RequestMapping(value="{appname}/{path1}/{path2}/{remainder:.[\\S]*}")
Run Code Online (Sandbox Code Playgroud)
有谁知道如何将URL的其余部分与Spring RequestMapping相匹配?
vin*_*bad 42
有趣的是:我也遇到了这样做的需要.这是我解决它的方式:
@RequestMapping(value = {"/someChildUrlIfYouWant/**"}
Run Code Online (Sandbox Code Playgroud)
在"**"这里说,"在一个什么样的我左侧任意子路径抢东西." 如果你想要实际匹配的路径来获得这个方法,你可以使用:
request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE )
Run Code Online (Sandbox Code Playgroud)
你会得到/someChildUrlIfYouWant/the/full/path/to/whatever.html,所以如果你只是想变子路径,你就必须修剪字符串的前部.
合理?
小智 7
对于上面的尝试:
@RequestMapping(value="{appname}/{path1}/{path2}/{remainder:.+}")
Run Code Online (Sandbox Code Playgroud)
Spring的默认行为是停止匹配'.' 在网址中不是很直观.我认为句点字符在路径的上下文中没有任何特殊含义(而不是说'/',';'或'?').
| 归档时间: |
|
| 查看次数: |
31115 次 |
| 最近记录: |