【问题标题】:Spring MVC - Mapping Controller to URLs using AnnotationsSpring MVC - 使用注释将控制器映射到 URL
【发布时间】:2011-08-09 17:06:33
【问题描述】:

我在配置 Spring 控制器以映射到特定 URL 时遇到问题,我将其归结为一个相当简单的场景,我认为它应该可以工作:

我正在使用注释配置我的 Controller 类,它看起来如下:

@Controller
@RequestMapping(value = "/question/**")
public class QuestionController 
{

    /**
     * Method to build users questions list
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //Display Questions List
    }
}

Controller 没有进一步的配置,我的 webmvc 配置中只是有<mvc:annotation-driven/> 配置和<context:component-scan>.. 配置,所以会自动检测到控制器。

现在,当我导航到 /question/list 时,应用程序无法找到资源并且我收到 ResourceNotFound 错误。但是,如果我导航到 /question/question/list,那么应用程序会正确加载我期望的页面。

任何想法为什么这是指向使用/question/question/list 的方法?


在此之后我尝试将配置添加到我的 webmvc 配置中以强制所有 RequestMappings 使用参数 alwaysUseFullPath=true,我这样做如下:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="3">
        <property name="alwaysUseFullPath" value="true" />
    </bean>

这一次,当我导航到 /question/list 时,它仍然无法加载正确的页面,但日志显示 Spring 至少识别了正确的 Controller,但只是找不到方法(之前它甚至没有根据 URL 找到 Controller):

2011-08-09 18:02:27,885 [http-8080-3] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Matching patterns for request [/question/list] are [/question/**/list/, /question/**/list, /question/**/, /question/**]
2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapping [/question/list] to handler 'com.tmm.enterprise.microblog.controller.QuestionController@143c423'
2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/microblog/question/list] is: -1
2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'microblog' processing GET request for [/microblog/question/list]
2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving exception from handler [com.tmm.enterprise.microblog.controller.QuestionController@143c423]: org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException: No matching handler method found for servlet request: path '/list', method 'GET', parameters map[[empty]]
2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving to view 'resourceNotFound' for exception of type [org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException], based on exception mapping [.NoSuchRequestHandlingMethodException]
2011-08-09 18:02:27,887 [http-8080-3] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Exposing Exception as model attribute 'exception'

在我看来,在使用注释将控制器连接到 URL 时,我试图实现相对简单的事情,但它无法正常工作 - 有没有人遇到过这个问题或看到任何明显的错误我的部分?


更新

我的调查取得了一些进展。

在我的 web.xml 中,我将 servlet 映射定义如下:

<servlet-mapping>
        <servlet-name>microblog</servlet-name>
        <url-pattern>/question/*</url-pattern>
    </servlet-mapping>

如果我删除这个 servlet 映射,(我仍然有一个 servlet 映射,将所有 .html 映射到同一个 servlet)并将我正在使用的 URL 更改为 /question/list.html 然后它可以工作(我也更改了方法级别将问题控制器中我的list() 方法上的@RequestMapping 注释映射到/list.html)。

总结:

  1. 我有 servlet 映射 /question 到 web 上下文

  2. 我有另一个/questionQuestionController 的映射

  3. 我的/list列表方法有一个方法级别映射

现在我不希望我的 URL 在这些情况下以 .html 结尾 - 有谁知道我可以如何解决这个问题?似乎 servlet 映射可能会从 URL 中删除匹配的 /question 字符串(因此 /question/list 不起作用,但 /question/question/list 起作用)

【问题讨论】:

  • 为没有 html 的 url 设置 /
  • 如果我使用它,它会将我的所有图像、css 资源等重定向到控制器,因此无法找到它们(我所有的图像都在 /images/.. 等) - 所以当我加载该页面没有图像或 CSS 样式。有什么想法可以将 URL 映射到控制器而不需要复制 URL 中的路径?
  • 将您的文件移动到上面的文件夹中,而不是在 WEB-INF 文件夹中。
  • 抱歉,我错了 - 我的图像、css 等已经在 WEB-INF 之上的目录级别。任何想法为什么当我添加 / 并导航到页面时没有样式/图像出现?
  • 哦,使用

标签: java spring web-applications spring-mvc


【解决方案1】:

设置

<url-pattern>/</url-pattern> 

对于没有 html 的 url 并使用

<mvc:resources mapping="/resources/**" location="/resources/" />

用于 .css、.jpg 等资源。

【讨论】:

    【解决方案2】:

    您的控制器映射中不需要 /**。

    映射到@RequestMapping(value = "/question") 会将您带到此控制器。

    /list 将附加到/question

    当您添加 /** 就像您拥有它一样时,您是在告诉它查找问题的基本路径,然后是任何内容,然后将 /list 添加到末尾。

    希望对您有所帮助。

    【讨论】:

    • 谢谢,我已经这样做了,但无奈之下,我尝试添加“/**”,以反映它在旧 XML 配置中的配置方式。在这两种情况下,我仍然得到相同的结果。
    • 我还更新了原始问题,并进一步更新了我对 web.xml servlet-mapping 所做的一些发现
    【解决方案3】:

    我也遇到这个问题,但是不开发只是调用 Rest API 然后报错:“org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException: No matching handler method found for servlet request.....”

    我通过在 Rest 客户端工具的请求标头中添加“Accept: application/json”来解决此问题。希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-01-16
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 2016-07-04
      相关资源
      最近更新 更多