这与这个问题非常相似,但是我只是不知道如何匹配url模式。
web.xml:
<servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/activate/*</url-pattern> </servlet-mapping>
我的控制器:
@RequestMapping(value = {"activate/{key}"}, method = RequestMethod.GET) public ModelAndView activate(@PathVariable(value = "key") String key) { ... }
当我尝试访问时localhost:9999/myApp/activate/123456789,出现以下错误:
localhost:9999/myApp/activate/123456789
No mapping found for HTTP request with URI [/myApp/activate/123456789] in DispatcherServlet with name 'dispatcher'
我也试过了<url-pattern>/*</url-pattern>,同样的事情发生了。
<url-pattern>/*</url-pattern>
但是,通过更改<url-pattern>/activate/*</url-pattern>为 <url-pattern>/**</url- pattern>没有错误出现,但我仍然得到404。那么,如何映射此url模式?
<url-pattern>/activate/*</url-pattern>
<url-pattern>/**</url- pattern>
您需要在上加上斜线@RequestMapping,例如:
@RequestMapping
@RequestMapping(value = {"/activate/{key}"}, method = RequestMethod.GET) public ModelAndView activate(@PathVariable(value = "key") String key) { ... }
无论如何,如果您想访问以下上下文:
<servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/activate/</url-pattern> </servlet-mapping>
您不能尝试这样:
@RequestMapping(value = {"/{key}"}, method = RequestMethod.GET) public ModelAndView activate(@PathVariable(value = "key") String key) { ... }
[编辑]
就像Leonel所说的那样,您应该使用完整的URL(@RequestMapping(value ={"/activate/{key}"})进行以下配置:
@RequestMapping(value ={"/activate/{key}"}
<url-pattern>/</url-pattern>