It's quite similar to this question, but I just couldn't figure out how to match the url pattern.
web.xml:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/activate/*</url-pattern>
</servlet-mapping>
My controller:
@RequestMapping(value = {"activate/{key}"}, method = RequestMethod.GET)
public ModelAndView activate(@PathVariable(value = "key") String key) {
...
}
When I try to access localhost:9999/myApp/activate/123456789
, I get the following error:
No mapping found for HTTP request with URI [/myApp/activate/123456789] in DispatcherServlet with name 'dispatcher'
I also tried <url-pattern>/*</url-pattern>
, same thing happens.
However, by changing <url-pattern>/activate/*</url-pattern>
to
<url-pattern>/**</url-pattern>
no error appears, but i still get 404.
So, how do I map this url pattern?
You need to put slash on @RequestMapping
, like:
@RequestMapping(value = {"/activate/{key}"}, method = RequestMethod.GET)
public ModelAndView activate(@PathVariable(value = "key") String key) {
...
}
Anyway, if you wat to get access to following context:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/activate/</url-pattern>
</servlet-mapping>
You cant try this:
@RequestMapping(value = {"/{key}"}, method = RequestMethod.GET)
public ModelAndView activate(@PathVariable(value = "key") String key) {
...
}
[Edited]
Like Leonel said, you should have this configuration to use with full URL (@RequestMapping(value = {"/activate/{key}"}
):
<url-pattern>/</url-pattern>
Change the url-pattern element to the following. A single slash, no asterisk:
<url-pattern>/</url-pattern>
What happens is, first the servlet container matches the requested URL into the pattern, and then it calls Spring's DispatcherServlet, which dispatches to the correct controller.
When you have the slash + asterisk, /*
, the snippet of the URI /activate/123456789
is matched, which leaves the empty string for the DispatcherServlet to find the controller.
When you have the single slash, /
, only the slash is matched by the servlet container, which leaves the string /activate/123456789
to be matched by the DispatcherServlet; with this string, the DispatcherServlet is able to find the controller.
All of the request mapping I've seen has used a leading slash. Have you tried this:
@RequestMapping("/activate/{key}")