9

This is similar to this question, but I am still confused about my situation. I want to map this ant-style pattern to a controller method:

/results/**

That is, I want any URL like www.hostname.com/MyServlet/results/123/abc/456/def/ to go to this method. I have:

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/results/*</url-pattern>
</servlet-mapping>

and:

@RequestMapping(value="/**", method=RequestMethod.GET)
public ModelAndView handleRequest() {...}

This works to guide the request to my method, but leads me to several questions:

  1. What if I add another servlet mapping, like <url-pattern>/another-mapping/*</url-pattern>??? It will also get mapped to that method! How can I separate the two?
  2. Why does the url-pattern /results/* work, whereas /results/** doesn't? According to ant path styles, ** means to include nested / characters, whereas * stops at the next /. So, it should only successfully map a URL like /results/123, bot NOT /results/123/abc/. Right?
Community
  • 1
  • 1
Tony R
  • 11,224
  • 23
  • 76
  • 101

2 Answers2

6

Perhaps in your servlet mapping you would want to direct all traffic to '/*'. This way, you can distinguish in your controller what method to use with different @RequestMapping's.

<servlet-mapping>
  <servlet-name>MyServlet</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

and

@RequestMapping(value="/results/**", method=RequestMethod.GET)
public ModelAndView handleResults() {...}

@RequestMapping(value="/another-mapping/**", method=RequestMethod.GET)
public ModelAndView handleAnotherMapping() {...}

Hopefully the above will help with number 1. As far as number 2 goes, I do not think that you can use 'ant-style' pattern matchers (specifically **) in your web.xml domain descriptor.

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • Hmm okay, I thought I had read that it uses Ant-style, but I guess I'm mistaken. So /* corresponds to nested /abc/123/....... – Tony R May 10 '11 at 18:57
5

What if I add another servlet mapping, like /another-mapping/*??? It will also get mapped to that method! How can I separate the two?

With your current configuration you cannot. If you want to map DispatcherServlet to multiple URL patterns and distinguish between them, you can declare DefaultAnnotationHandlerMapping with alwaysUseFullPath = "true" and use full path in @RequestMapping.

Alternatively, you can map DispatcherServlet as <url-pattern>/*</url-pattern> and use full path in @RequestMapping without reconfiguring DefaultAnnotationHandlerMapping. Though in this case you'll need to configre exclusions for static content.

Why does the url-pattern /results/* work, whereas /results/** doesn't? According to ant path styles, ** means to include nested / characters, whereas * stops at the next /. So, it should only successfully map a URL like /results/123, bot NOT /results/123/abc/. Right?

URL patterns in web.xml are not ant-style patterns, so that only .../* and *.xxx wildcards are allowed in them.

Community
  • 1
  • 1
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • Okay, so I added to my DefaultAnnotationHandlerMapping bean. What should the @RequestMapping value be? Sorry, I'm new at this... – Tony R May 10 '11 at 19:12
  • @Tony: It should be `/results/**`. – axtavt May 10 '11 at 19:17
  • Okay, I tried that and I'm getting: "No matching handler method found for servlet request: path '/abc/123'" – Tony R May 10 '11 at 19:30
  • Did you also change your web.xml domain descriptor's servlet mapping for MyServlet to '/'? – nicholas.hauschild May 10 '11 at 19:43
  • Okay so I have Dispatcher servlet mapped to "/", an @RequestMapping("/results/**") mapped method in my controller, and mappings on my default servlet for "*.html", "*.js", etc. Everything works except for URL's like "/results/123/abc", which give a 404. Any advice? – Tony R May 12 '11 at 19:28
  • I have static .html pages that are served fine w/o mappings, and they also work without the explicit mapping * .html on my default servlet. However, I have a static .gif file in the same directory that isn't getting served. I also need to be able to use URL's like /results/abc/myImage.bmp, which I want to serve from a controller and not from static content (i.e. I need URL's ending in .bmp to be mapped to a controller IF the URL starts with /results/**, but serve static content otherwise) – Tony R May 12 '11 at 19:39