2

I was wondering if I can customize my url into controller to accept null values

@RequestMapping(
            value = "foo/{y}/{x}",
            method = RequestMethod.GET)
    public ModelAndView doSmth(@PathVariable(value = "x") String x,
                               @PathVariable(value = "y") String y) {
}

Here in my case , X , Y can be empty string . How could I manage smth like that to let this method on the controller to handle this url ?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Echo
  • 2,959
  • 15
  • 52
  • 65
  • what are all combinations of X and Y? Is it possible to split it to many methods - preprocess variables and do logic in some common private method? – Random Jul 12 '11 at 10:27
  • BTW: http://stackoverflow.com/questions/5879666/requestmapping-controlers-and-dynamic-urls – Random Jul 12 '11 at 10:29

2 Answers2

1

If you want to support optional values, then path variables aren't well suited. Request parameters (using @RequestParam) are a better idea, since they easily support optional values using the required attribute.

If you really want to support optional path variables, then you need to overload your mappings, i.e.

@RequestMapping(value = "foo/{y}/{x}")
public ModelAndView doSmth(@PathVariable(value = "x") String x, @PathVariable(value = "y") String y) 

@RequestMapping(value = "foo/{y}")
public ModelAndView doSmth(@PathVariable(value = "y") String y) 

@RequestMapping(value = "foo")
public ModelAndView doSmth() 
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • The thing is this url contains mulible fields that may contain empty string not null . If I follow this methodology , I will need to write many overloads methods . I need to avoid this !! – Echo Jul 12 '11 at 10:29
  • @Echo: Yes, that's why I just said use `@RequestParam` instead. – skaffman Jul 12 '11 at 10:30
  • I am trying to support it using @RequestMapping /foo/${x}/[#if x?has_content]${y} [#else] What I can do to pass empty String [/#if] – Echo Jul 12 '11 at 10:38
  • So is there any advice how to pass empty string !! – Echo Jul 12 '11 at 10:39
  • 2
    @Echo: You're not listening to me - path variables *are not suited* to this kind of logic. – skaffman Jul 12 '11 at 10:47
  • u are right that @RequestMapping can't be suited to handle my case . I was just needed to avoid writing multiple overloaded methods .Also @Whiteship , the one who suggested a solution below, may has an interesting solution . However , I have maintained my code a little bit in order not to call my method with multiple url variables .Thanks @skaffman for ur amazing assistance – Echo Jul 15 '11 at 14:59
1

I don't think, that's a good URL. It can be very confusing to users.

Why don't you try this one?

@RequestMapping("foo/x/{x}/y/{y}")
public ModelAndView doSmth(@PathVariable String x, @PathVariable String y)
Whiteship
  • 1,799
  • 3
  • 16
  • 15
  • Thanks. I didn't try it as I have used another methodology but ur solution is interesting to be tried . – Echo Jul 15 '11 at 14:53