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

What will be my arguments to the userDetails method? And how do I differentiate /userDetails and /userDetails/edit/9 within the method?

geekosaur
  • 59,309
  • 11
  • 123
  • 114
Aravind Vel
  • 277
  • 1
  • 5
  • 18
  • Duplicate of [this question](http://stackoverflow.com/questions/2745471/spring-web-mvc-use-same-request-mapping-for-request-parameter-and-path-variable) – nobeh Apr 09 '12 at 11:39

1 Answers1

17

Ideally we can get pathvariable by using annotation @PathVariable in method argument but here you have used array of url {"/userDetails", "/userDetails/edit/{id}"} so this will give error while supply request like localhost:8080/domain_name/userDetails , in this case no id will be supplied to @PathVariable.

So you can get the difference (which request is comming through) by using argument HttpServletRequest request in method and use this request object as below -

String uri = request.getRequestURI();

Code is like this -

   @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
 }
kundan bora
  • 3,821
  • 2
  • 20
  • 29
  • I use method=RequestMethod.GET and method=RequestMethod.POST separately. In that case, I won't be passing HttpServletRequest as an argument. Is there any other way around? – Aravind Vel Apr 09 '12 at 11:50
  • 6
    why don't you separate these two requestmapping into two methods? and if you have any common functionality to implement you can put that common code in separate private method and call this method accordingly. – kundan bora Apr 09 '12 at 12:06