java Spring-MVC RequestMapping URITemplate 中的可选路径变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7841770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Optional path variables in Spring-MVC RequestMapping URITemplate
提问by laochan
I have the following mapping:
我有以下映射:
@RequestMapping(value = "/{first}/**/{last}", method = RequestMethod.GET)
public String test(@PathVariable("first") String first, @PathVariable("last")
String last) {}
Which for the following URIs:
对于以下 URI:
foo/a/b/c/d/e/f/g/h/bar
foo/a/bar
foo/bar
maps footo firstand barto lastand works fine.
将foo映射到第一个,将bar映射到最后一个并且工作正常。
What I would like is something that maps everything between foo and bar into a single path param, or null if there is no middle (as in the last URI example):
我想要的是将 foo 和 bar 之间的所有内容映射到单个路径参数中,或者如果没有中间值则为 null(如上一个 URI 示例):
@RequestMapping(value = "/{first}/{middle:[some regex here?]}/{last}",
method = RequestMethod.GET)
public String test(@PathVariable("first") String first, @PathVariable("middle")
String middle, @PathVariable("last") String last) {}
Pretty stuck on the regex since I was hoping that something simple like {middle:.*}, which onlymaps to /foo/a/bar, or {middle:(.*/)*}, which seems to map to nothing.
非常坚持正则表达式,因为我希望像 {middle:.*} 这样简单的东西,它只映射到 /foo/a/bar,或者 {middle:(.*/)*},它似乎什么都不映射。
Does the AntPathStringMatcher tokenize upon "/" prior to applying regex patterns? (making patterns that cross a / impossible) or is there a solution?
在应用正则表达式模式之前,AntPathStringMatcher 是否对“/”进行标记?(制作跨越/不可能的模式)还是有解决方案?
FYI this is in Spring 3.1M2
仅供参考,这是在 Spring 3.1M2
This seem similar to @RequestMapping controllers and dynamic URLsbut I didn't see a solution there.
这似乎类似于@RequestMapping 控制器和动态 URL,但我在那里没有看到解决方案。
回答by imxylz
In my project, I use inner variable in the springframework:
在我的项目中,我在 springframework 中使用了内部变量:
@RequestMapping(value = { "/trip/", // /trip/
"/trip/{tab:doa|poa}/",// /trip/doa/,/trip/poa/
"/trip/page{page:\d+}/",// /trip/page1/
"/trip/{tab:doa|poa}/page{page:\d+}/",// /trip/doa/page1/,/trip/poa/page1/
"/trip/{tab:trip|doa|poa}-place-{location}/",// /trip/trip-place-beijing/,/trip/doa-place-shanghai/,/trip/poa-place-newyork/,
"/trip/{tab:trip|doa|poa}-place-{location}/page{page:\d+}/"// /trip/trip-place-beijing/page1/
}, method = RequestMethod.GET)
public String tripPark(Model model, HttpServletRequest request) throws Exception {
int page = 1;
String location = "";
String tab = "trip";
//
Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
if (pathVariables != null) {
if (pathVariables.containsKey("page")) {
page = NumberUtils.toInt("" + pathVariables.get("page"), page);
}
if (pathVariables.containsKey("tab")) {
tab = "" + pathVariables.get("tab");
}
if (pathVariables.containsKey("location")) {
location = "" + pathVariables.get("location");
}
}
page = Math.max(1, Math.min(50, page));
final int pagesize = "poa".equals(tab) ? 40 : 30;
return _processTripPark(location, tab, pagesize, page, model, request);
}
回答by peabody
Can't be done as far as I know. Just as you stated, the regular expression is being applied to the path element after splitting up the path at each slash, so the regular expression can never match a '/'.
据我所知做不到。正如您所说,在每个斜杠处拆分路径后,正则表达式将应用于路径元素,因此正则表达式永远无法匹配“/”。
You could manually inspect the url and parse it yourself from the request object.
您可以手动检查 url 并从请求对象中自己解析它。
回答by dlaidlaw
It could be done by writing a custom path matcher and configuring Spring to use it. For example, such a solution is documented here: http://java.dzone.com/articles/spring-3-webmvc-optional-path
可以通过编写自定义路径匹配器并配置 Spring 来使用它来完成。例如,这里记录了这样的解决方案:http: //java.dzone.com/articles/spring-3-webmvc-optional-path
The link provides a custom path matcher and shows how to configure spring to use it. That should solve your need if you don't mind writing a custom component.
该链接提供了一个自定义路径匹配器,并展示了如何配置 spring 以使用它。如果您不介意编写自定义组件,那应该可以解决您的需求。
Also, this is a duplicate of With Spring 3.0, can I make an optional path variable?
另外,这是与 Spring 3.0的副本,我可以创建一个可选的路径变量吗?
回答by Secondo
try to use this
尝试使用这个
@RequestMapping(value = {"some mapped address","some mapped address with path variable","some mapped address with another path variable"})
array list of available url for specific method
特定方法的可用 url 的数组列表
But be careful on creating list of url when you are using @PathVariable in your method signature it cant be null.
但是当您在方法签名中使用 @PathVariable 时,创建 url 列表时要小心,它不能为空。
hope this help
希望这有帮助