具有pathvariable的多个值的RequestMapping - Spring 3.0

时间:2012-04-09 11:05:41

标签: java spring spring-mvc spring-3

@RequestMapping(value = {"/userDetails", "/userDetails/edit/{id}"}, method = RequestMethod.GET)
public String userDetails(Map Model,****) {
//what goes here? 
}

我对userDetails方法的论点是什么?如何在方法中区分/userDetails/userDetails/edit/9

1 个答案:

答案 0 :(得分:15)

理想情况下,我们可以通过在方法参数中使用注释@PathVariable来获取路径变量,但是在这里您使用了url {"/userDetails", "/userDetails/edit/{id}"}数组,因此这会在localhost:8080/domain_name/userDetails等供应请求时出错,在这种情况下没有id将提供给@PathVariable。

所以你可以通过在方法中使用参数HttpServletRequest请求来获得差异(哪个请求正在通过)并使用此请求对象,如下所示 -

String uri = request.getRequestURI();

代码就是这样 -

   @RequestMapping(value = {"/userDetails", "/userDetails/edit/{id}"}, method=RequestMethod.GET)
   public String userDetails(Map Model,HttpServletRequest request) {
   String uri = request.getRequestURI();  
  //put the condition based on uri
 }