spring-mvc:如何以"a/b/{c}"形式映射URI模板? [英] spring-mvc: how to map URI templates in the form of "a/b/{c}"?
问题描述
我可以使用"/a/b"或"/a/{b}"形式的URI模板.但是,当我尝试"/a/b/{c}"时,我在日志中收到了一条HTTP 404和一条消息,消息形式为未找到带有URI [/myapp/a/b/c]的HTTP请求的映射. ..但是我也在日志中看到了这些消息,这使我相信映射是正确的...?
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
我注意到所有 spring-mvc文档中的示例以"/a/{b}"或/a/{b}/c/{d}"的形式显示了URI模板. "/a/b/{c}"是不可能的吗?我是否需要在web.xml中进行配置以使这种情况发生?或者某些(错误)配置会阻止该模式被映射?目前我的调度程序servlet已映射到:
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>
控制器看起来像这样:
@Controller
public class AController {
@RequestMapping(value = "/a/b/{c}", method = RequestMethod.GET)
public ModelAndView show() {
ModelAndView modelAndView = new ModelAndView("A");
return modelAndView;
}
}
访问 http://localhost:8080/myapp/a/b/c 返回404,我在日志中看到了这一点:
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'
关于如何获取URI模式以正确映射的任何想法吗?
Any ideas on how I get my URI pattern to get mapped correctly?
推荐答案
仅当ControllerClassNameHandlerMapping
用作处理程序映射时,才会出现这种情况.
It looks like this happens only when ControllerClassNameHandlerMapping
is used as a handler mapping.
带注释的控制器通常与DefaultAnnotationHandlerMapping
一起使用,在这种情况下,一切正常.
Annotated controllers are usually used with DefaultAnnotationHandlerMapping
, and in that case everything works fine.
实际上,它看起来像ControllerClassNameHandlerMapping
的合法行为.它将aController
映射到/a/*
而不是/a/**
,因此仅接受一级路径层次结构.同样,如果您需要完全的灵活性,请使用DefaultAnnotationHandlerMapping
.
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
.
这篇关于spring-mvc:如何以"a/b/{c}"形式映射URI模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!