DefaultAnnotationHandlerMapping如何工作 [英] How DefaultAnnotationHandlerMapping works

查看:77
本文介绍了DefaultAnnotationHandlerMapping如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对DefaultAnnotationHandlerMapping的工作方式感到困惑.

I am confused about the way the DefaultAnnotationHandlerMapping works.

在我的web.xml中,

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>  

我有这样的控制器,

   @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处理程序映射

This does not work. I get the error no handler mapping found for "/adduser" and 404 for the page "/user/adduser"

但是在.xml文件中,如果我提到

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.

我现在对@ReuqestMapping的工作方式感到困惑.当一个请求像"/user/adduser"那样从那里开始时,它将开始寻找正确的类和正确的方法?

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?

推荐答案

Spring将与HttpServletRequestpathInfo属性匹配.

Spring will match against the pathInfo property of the HttpServletRequest.

如果您的web.xml指定<url-pattern>/user/*</url-pattern>,则pathInfo将是删除了/user前缀的路径,因此@RequestMapping必须为/adduser.

If your web.xml specifies <url-pattern>/user/*</url-pattern>, then the pathInfo will be the path with the /user prefix removed, so the @RequestMapping has to be /adduser.

如果web.xml指定<url-pattern>/user/adduser</url-pattern>,则pathInfo将是完整的/user/adduser路径,因此@RequestMapping必须与此匹配.

If web.xml specifies <url-pattern>/user/adduser</url-pattern>, then the pathInfo will be the full /user/adduser path, so @RequestMapping has to match against that.

这不是由Spring完成的,而是由servlet容器完成的,有时可能会有些混乱.

This isn't done by Spring, but by the servlet container, and it can be a bit confusing at times.

您可以通过在@RequestMapping中使用通配符来缓解这种情况,例如

You can mitigate against this by using wildcards in @RequestMapping, e.g.

@RequestMapping(value="**/adduser", method={RequestMethod.POST})

这篇关于DefaultAnnotationHandlerMapping如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆