DefaultAnnotationHandlerMapping 的工作原理
我对 DefaultAnnotationHandlerMapping 的工作方式感到困惑。
在我的 web.xml 中我有
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/somePath/someWork</url-pattern>
<url-pattern>/users</url-pattern>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
这样的控制器,
@RequestMapping(value="/user/adduser", method={RequestMethod.POST})
public void addAdmin(@ModelAttribute("myData") myData data) {
System.out.println("We reached adduser controller");
}
在 jsp 文件中我有
<form:form id="adduser" method="post" action="/user/adduser" commandName="myData">
这不起作用。我得到的错误是“/adduser”没有找到处理程序映射,“/user/adduser”页面有 404,
但是在 .xml 文件中,如果我提到
<url-pattern>/user/adduser</url-pattern>
它有效,或者如果我使控制器像这样,
@RequestMapping(value="/adduser", method={RequestMethod.POST})
也有效。提交页面时,它会到达正确的控制器。
我现在对 @ReuqestMapping 的工作方式感到困惑。当像“/user/adduser”这样的请求出现时,它将从哪里开始寻找正确的类和正确的方法?
I am confused about the way the DefaultAnnotationHandlerMapping works.
In my web.xml I have
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/somePath/someWork</url-pattern>
<url-pattern>/users</url-pattern>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
I have the controller like this,
@RequestMapping(value="/user/adduser", method={RequestMethod.POST})
public void addAdmin(@ModelAttribute("myData") myData data) {
System.out.println("We reached adduser controller");
}
And in the jsp file i have
<form:form id="adduser" method="post" action="/user/adduser" commandName="myData">
This does not work. I get the error no handler mapping found for "/adduser" and 404 for the page "/user/adduser"
But in the .xml file if i mention
<url-pattern>/user/adduser</url-pattern>
it works, or if i make the controller like,
@RequestMapping(value="/adduser", method={RequestMethod.POST})
also works. When submitting the page it reaches the correct controller.
I am now confused the way the @ReuqestMapping works. When a request comes like "/user/adduser" from where it will start looking for the right class and the right method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Spring 将与
HttpServletRequest
的pathInfo
属性进行匹配。如果您的
web.xml
指定/user/*
,则pathInfo
将是删除了/user
前缀的路径,因此@RequestMapping
必须是/adduser
。如果
web.xml
指定/user/adduser
,则pathInfo
将是完整的/user/adduser
路径,因此@RequestMapping
必须与其匹配。这不是由 Spring 完成的,而是由 servlet 容器完成的,有时可能会有点混乱。
您可以通过在
@RequestMapping
中使用通配符来缓解这种情况,例如Spring will match against the
pathInfo
property of theHttpServletRequest
.If your
web.xml
specifies<url-pattern>/user/*</url-pattern>
, then thepathInfo
will be the path with the/user
prefix removed, so the@RequestMapping
has to be/adduser
.If
web.xml
specifies<url-pattern>/user/adduser</url-pattern>
, then thepathInfo
will be the full/user/adduser
path, so@RequestMapping
has to match against that.This isn't done by Spring, but by the servlet container, and it can be a bit confusing at times.
You can mitigate against this by using wildcards in
@RequestMapping
, e.g.