Spring 3 RequestMapping:获取路径值
有没有办法在解析 requestMapping
@PathVariable
值后获取完整的路径值?
那是: /{id}/{restOfTheUrl}
应该能够将 /1/dir1/dir2/file.html
解析为 id=1
和 < code>restOfTheUrl=/dir1/dir2/file.html
任何想法将不胜感激。
Is there a way to get the complete path value after the requestMapping
@PathVariable
values have been parsed?
That is:/{id}/{restOfTheUrl}
should be able to parse /1/dir1/dir2/file.html
into id=1
and restOfTheUrl=/dir1/dir2/file.html
Any ideas would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我有一个类似的问题,我以这种方式解决:
请注意,
req.getPathInfo()
将返回完整路径(包含{siteCode}
和{fileName} .{fileExtension}
),因此您必须方便地进行处理。I have a similar problem and I resolved in this way:
Note that
req.getPathInfo()
will return the complete path (with{siteCode}
and{fileName}.{fileExtension}
) so you will have to process conveniently.为了改进@Daniel Jay Marcaida 的回答
或
To improve upon @Daniel Jay Marcaida answer
or
我是这样做的。您可以看到我如何将 requestURI 转换为文件系统路径(这个 SO 问题是关于什么的)。奖励:以及如何回复文件。
Here is how I did it. You can see how I convert the requestedURI to a filesystem path (what this SO question is about). Bonus: and also how to respond with the file.
是的,
restOfTheUrl
不仅仅返回所需的值,但我们可以通过使用UriTemplate
匹配来获取该值。我已经解决了这个问题,所以这里是问题的有效解决方案:
Yes the
restOfTheUrl
is not returning only required value but we can get the value by usingUriTemplate
matching.I have solved the problem, so here the working solution for the problem:
我已经使用 Tuckey URLRewriteFilter 来处理包含“/”字符的路径元素,因为我认为 Spring 3 MVC 尚不支持它们。
http://www.tuckey.org/
您将此过滤器放入您的应用程序中,并提供 XML配置文件。在该文件中,您提供重写规则,您可以使用这些规则将包含“/”字符的路径元素转换为 Spring MVC 可以使用 @RequestParam 正确处理的请求参数。
WEB-INF/web.xml:
WEB-INF/urlrewrite.xml:
控制器方法:
I have used the Tuckey URLRewriteFilter to handle path elements that contain '/' characters, as I don't think Spring 3 MVC supports them yet.
http://www.tuckey.org/
You put this filter in to your app, and provide an XML config file. In that file you provide rewrite rules, which you can use to translate path elements containing '/' characters into request parameters that Spring MVC can deal with properly using @RequestParam.
WEB-INF/web.xml:
WEB-INF/urlrewrite.xml:
Controller method:
您需要使用内置的
pathMatcher
:You need to use built-in
pathMatcher
:基于 Fabien Kruba 已经很好的答案,我认为如果
**
部分会很好URL 的值可以通过注释作为参数提供给控制器方法,其方式类似于 @RequestParam 和 @PathVariable,而不是总是使用实用程序明确需要 HttpServletRequest 的方法。这是一个如何实现的示例。希望有人觉得它有用。创建注释以及参数解析器:
注册方法参数解析器:
在控制器处理程序方法中使用注释可以轻松访问 URL 的
**
部分:Building upon Fabien Kruba's already excellent answer, I thought it would be nice if the
**
portion of the URL could be given as a parameter to the controller method via an annotation, in a way which was similar to@RequestParam
and@PathVariable
, rather than always using a utility method which explicitly required theHttpServletRequest
. So here's an example of how that might be implemented. Hopefully someone finds it useful.Create the annotation, along with the argument resolver:
Register the method argument resolver:
Use the annotation in your controller handler methods to have easy access to the
**
portion of the URL:这已经在这里有一段时间了,但还是发布了。可能对某人有用。
This has been here quite a while but posting this. Might be useful for someone.
刚刚发现与我的问题相对应的问题。使用 HandlerMapping 常量,我能够为此目的编写一个小实用程序:
Just found that issue corresponding to my problem. Using HandlerMapping constants I was able to wrote a small utility for that purpose:
URL 的不匹配部分公开为名为
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
的请求属性:Non-matched part of the URL is exposed as a request attribute named
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
: