问题
I can get a URI template in the form of "/a/b" or "/a/{b}" to work. But when I try "/a/b/{c}", I get a HTTP 404 and a message in the log in the form of "No mapping found for HTTP request with URI [/myapp/a/b/c]..." But I see these message in the log also which leads me to believe that the mappings are correct...?
INFO: Mapped URL path [/a] onto handler 'AController'
Nov 16, 2010 12:18:39 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/a/*] onto handler 'AController'
Nov 16, 2010 12:18:39 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
I noticed all the examples in the spring-mvc docs show URI templates in the form of "/a/{b}" or /a/{b}/c/{d}". So is "/a/b/{c}" not possible? Is there anything I need to configure in web.xml to make this happen? Or can some (mis)configuration prevent that pattern from being mapped? Currently my dispatcher servlet is mapped to:
<url-pattern>/</url-pattern>
The controller looks like this:
@Controller
public class AController {
@RequestMapping(value = "/a/b/{c}", method = RequestMethod.GET)
public ModelAndView show() {
ModelAndView modelAndView = new ModelAndView("A");
return modelAndView;
}
}
Accessing http://localhost:8080/myapp/a/b/c returns a 404 and I see this in the log:
Nov 16, 2010 12:19:06 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myapp/a/b/c] in DispatcherServlet with name 'dispatcher'
Any ideas on how I get my URI pattern to get mapped correctly?
回答1:
It looks like this happens only when ControllerClassNameHandlerMapping is used as a handler mapping.
Annotated controllers are usually used with DefaultAnnotationHandlerMapping, and in that case everything works fine.
EDIT:
Actually, it looks like a legitimate behaviour of ControllerClassNameHandlerMapping. It maps aController to /a/* rather than /a/**, therefore only one level of path hierarchy is accepted. And again, if you need full flexibility, use DefaultAnnotationHandlerMapping.
回答2:
can you try the below,
@RequestMapping(value = "/a/b/{c}", method = RequestMethod.GET)
public ModelAndView show(@PathParam("c") String c) {
ModelAndView modelAndView = new ModelAndView("A");
return modelAndView;
}
来源:https://stackoverflow.com/questions/4196577/spring-mvc-how-to-map-uri-templates-in-the-form-of-a-b-c