Spring MVC中有多个@PathVariable

时间:2012-07-05 19:14:24

标签: java spring spring-mvc annotations path-variables

不幸的是找不到答案,所以希望有人可以提供帮助。

在Spring MVC 3.1.0中,这是我的方法:

@RequestMapping(value = "/{app}/conf/{fnm}", method=RequestMethod.GET)
public ResponseEntity<?> getConf(@PathVariable String app, @PathVariable String fnm) {
    log.debug("AppName:" + app);
    log.debug("fName:" + fnm);
            ...
            return ...
    }

我在网上看过一些例子,看来理论上有多个@PathVariables没问题。

然而,当我这样做时,“app”和“fnm”都包含相同的值(这是分配给“app”的任何值)。

真的很感激有人可能对我出错的地方有所了解吗?

谢谢!

1 个答案:

答案 0 :(得分:29)

@RequestMapping(value = "/{app}/conf/{fnm}", method=RequestMethod.GET)
public ResponseEntity<?> getConf(@PathVariable("app") String app, @PathVariable("fnm") String fnm) {
   log.debug("AppName:" + app);
   log.debug("fName:" + fnm);
           ...
           return ...
  }

基本上,路径变量需要在方法参数中用括号指定。 这有帮助吗?