@RequestMapping正则表达式

时间:2015-03-20 06:46:32

标签: spring spring-mvc

我试图为spring @RequestMapping注释创建值属性,以便像这样映射网址

/educationDistrict/308/action/resetAddressesForYear/1

和这个

/educationDistrict/308/action/resetAddressesForYear

我有这个

@RequestMapping(value = "/{repository}/{id}/action/{methodName:[A-z]*}{v:.*}", method = RequestMethod.POST)

但第一个网址不匹配。

由于春天的仇恨,我无法使用多重价值 https://github.com/spring-projects/spring-hateoas/issues/186

春天4.1.5

1 个答案:

答案 0 :(得分:5)

/**中的网址映射末尾添加@RequestMapping。您可以按如下方式检索网址的最后部分:

@RequestMapping(value = "/{repository}/{id}/action/{methodName:[A-z]*}{v:.*}/**", method = RequestMethod.GET)
public ModelAndView welcome(@PathVariable("methodName") String name, HttpServletRequest request) {

    String mvcPath = (String) request.getAttribute(
            HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    int index = StringUtils.lastIndexOf(mvcPath, "/");

    System.out.println("Method name - " + name);        
    System.out.println("Rest of the URL - " + mvcPath.substring(index+1));

    ModelAndView model = new ModelAndView();
    model.setViewName("index");
    model.addObject("name", mvcPath);

    return model;
}

注意:我使用StringUtils Apache Commons查找/的最后一个索引。