Spring-MVC RequestMapping URITemplate 中的可选路径变量

发布于 2024-12-10 19:22:02 字数 1084 浏览 3 评论 0原文

我有以下映射:

@RequestMapping(value = "/{first}/**/{last}", method = RequestMethod.GET)
public String test(@PathVariable("first") String first,  @PathVariable("last")  
  String last) {}

对于以下 URI:

foo/a/b/c/d/e/f/g/h/bar
foo/a/bar
foo/bar

foo 映射到 first 并将 bar 映射到 last 并且有效美好的。

我想要的是将 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) {}

非常卡在正则表达式上,因为我希望像 {middle 这样简单的东西:.*},映射到 /foo/a/bar,或 {middle:(.*/)*},它似乎没有映射到任何内容。

在应用正则表达式模式之前,AntPathStringMatcher 是否对“/”进行标记? (使交叉/不可能的模式)或者有解决方案吗?

仅供参考,这是在 Spring 3.1M2 中,

这看起来与 @RequestMapping 控制器和动态 URL 类似,但是我在那里没有看到解决方案。

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:

foo/a/b/c/d/e/f/g/h/bar
foo/a/bar
foo/bar

maps foo to first and bar to last and works fine.

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):

@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 only maps to /foo/a/bar, or {middle:(.*/)*}, which seems to map to nothing.

Does the AntPathStringMatcher tokenize upon "/" prior to applying regex patterns? (making patterns that cross a / impossible) or is there a solution?

FYI this is in Spring 3.1M2

This seem similar to @RequestMapping controllers and dynamic URLs but I didn't see a solution there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

深居我梦 2024-12-17 19:22:02

在我的项目中,我在 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);
}

请参阅 HandlerMapping.html#URI_TEMPLATE_VARIABLES_ATTRIBUTE

In my project, I use inner variable in the 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);
}

See HandlerMapping.html#URI_TEMPLATE_VARIABLES_ATTRIBUTE

许久 2024-12-17 19:22:02

据我所知是做不到的。正如您所说,在每个斜杠处分割路径后,正则表达式将应用于路径元素,因此正则表达式永远不能匹配“/”。

您可以手动检查 url 并从请求对象中自行解析它。

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.

最好是你 2024-12-17 19:22:02

这可以通过编写自定义路径匹配器并配置 Spring 使用它来完成。例如,这样的解决方案记录在此处: http://java.lang. dzone.com/articles/spring-3-webmvc-optional-path

该链接提供了一个自定义路径匹配器,并展示了如何配置 spring 来使用它。如果您不介意编写自定义组件,这应该可以解决您的需求。

另外,这是 With Spring 3.0 的副本,我可以创建一个可选的路径变量吗?

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

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.

Also, this is a duplicate of With Spring 3.0, can I make an optional path variable?

迎风吟唱 2024-12-17 19:22:02

尝试使用

@RequestMapping(value = {"some mapped address","some mapped address with path variable","some mapped address with another path variable"})

特定方法的可用 url 的数组列表,

但是当您在方法签名中使用 @PathVariable 时,创建 url 列表时要小心,它不能为 null。

希望这有帮助

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

But be careful on creating list of url when you are using @PathVariable in your method signature it cant be null.

hope this help

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