3

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?

1
  • Try adding a '/' at the beginning of your request mapping and see if that makes a difference. It should work with url-pattern /* and access it with /contextPath/activate/123456789. Also, check your logs for any startup errors that might have caused the web application to not have started properly. Commented Mar 28, 2013 at 14:12

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

2 Comments

OK, it works. But it causes redirect loop in my case. For example, if the activation is successful, I redirect it by new ModelAndView(redirect:./success) and it gets root directory as myApp/activate, so 'success' matches /{key}. But I want my root to be myApp only. Should I use full path to solve this or is there any other way?
Hum, I think that you need to use full path URL with <url-pattern>/</url-pattern> to user 'myApp' as root.
2

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.

2 Comments

You don't just lose URL substrings because the DispatcherServlet matches them. /* will match anything after /, but the * will still be available. It's the leading / in his RequestMapping that is missing.
Although I like it, the explanation above isn't fully correct and I think there are issues with using * in <url-pattern>'s. I only get the stuff working predictably with a single <url-pattern>/</url-pattern> in web.xml, and then all request mappings starting with /... e.g. /activate/{key}, etc. A set of <url-pattern>'s as allowed by the web.xml schema works predictably only if it does contain no path with * and the controller has an exactly matching set of request mapping paths. Anything else I tried yields errors for what shall be matching patterns.
1

All of the request mapping I've seen has used a leading slash. Have you tried this: @RequestMapping("/activate/{key}")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.