Spring-MVC RequestMapping URITemplate中的可选路径变量 [英] Optional path variables in Spring-MVC RequestMapping URITemplate

查看:660
本文介绍了Spring-MVC RequestMapping URITemplate中的可选路径变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下映射:

@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 ,并可以正常工作.

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

我想要的是一种将foo和bar之间的所有内容映射到单个路径参数的方法,如果没有中间值,则将其映射为null(如上一个URI示例):

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) {}

因为我希望像{middle:.*}这样简单的东西(仅 映射到/foo/a/bar或{middle:(.*/)),所以漂亮地卡在了正则表达式上*},似乎什么也没有映射.

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.

在应用正则表达式模式之前,AntPathStringMatcher是否在"/"上标记化? (使模式跨越/不可能)还是有解决方案?

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

仅供参考,这是在3.1M2春天

FYI this is in Spring 3.1M2

这似乎类似于 @RequestMapping控制器和动态URL ,但我没有在那里查看解决方案.

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

推荐答案

在我的项目中,我在springframework中使用了内部变量:

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);
}

请参见 HandlerMapping.html# URI_TEMPLATE_VARIABLES_ATTRIBUTE

这篇关于Spring-MVC RequestMapping URITemplate中的可选路径变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆