【问题标题】:How do I debug a "No Mapping found for HTTP Request"?如何调试“未找到 HTTP 请求的映射”?
【发布时间】:2011-07-17 12:16:42
【问题描述】:

我正在尝试使用 Spring 3.0 映射控制器但没有成功。我有以下错误:

2011-07-17 20:01:16,536 [http-8080-exec-5] 警告 org.springframework.web.servlet.PageNotFound - 未找到带有 URI [/sherd/cp/index] 的 HTTP 请求的映射名称为“cp”的 DispatcherServlet

如何调试此错误?我已将日志设置为 INFO 并看到:

2011-07-17 20:10:28,402 [main] INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - 将 URL 路径 [/cp/index] 映射到处理程序“索引” 2011-07-17 20:10:28,402 [main] INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - 映射到处理程序“索引”的 URL 路径 [/cp/index.*] 2011-07-17 20:10:28,402 [main] INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - 将 URL 路径 [/cp/index/] 映射到处理程序“索引”

但是,我在尝试加载页面时收到上面显示的 WARN。

WEB-INF/web.xml的相关部分如下:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/cp-beans.xml</param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<servlet>
    <servlet-name>cp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>cp</servlet-name>
    <url-pattern>/cp/*</url-pattern>
</servlet-mapping>

我的 WEB-INF/cp-servlet.xml 包含以下内容:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.obliquid.sherd.web.cp"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
</beans>

而WEB-INF/cp-beans.xml暂时基本是空的:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>

我在包org.obliquid.sherd.web.cp中定义了类Index

package org.obliquid.sherd.web.cp;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Spring MVC controller for /cp/index
 * 
 * @author stivlo
 */
@Controller
@RequestMapping("/cp/index")
public class Index {

    @RequestMapping
    public String show() {
        return "cp/index"; //view 
    }

}

我错过了什么吗?

【问题讨论】:

    标签: java spring servlets


    【解决方案1】:

    我不确定,因为我无法测试,但请尝试更改此设置:

    <servlet-mapping>
        <servlet-name>cp</servlet-name>
        <url-pattern>/cp/*</url-pattern>
    </servlet-mapping>
    

    到这里:

    <servlet-mapping>
        <servlet-name>cp</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    RequestMapping 注解有/cp,所以应该在servlet-mapping 中省略。

    【讨论】:

    • 嗨 Sangdol,谢谢你的建议。我有前缀的原因是我还有其他 servlet 映射,我想慢慢迁移。无论如何,为了测试,我尝试了“/*” url-pattern 和“/”,但我仍然使用相同的 WARN。
    【解决方案2】:

    我已经设法使它与以下更改一起工作:

    @Controller
    public class Index {
    
        @RequestMapping("/index")
        public String show() {
            return "index"; //view 
        }
    
    }
    

    请注意,我已经删除了 @RequestMapping 和返回的逻辑视图名称中的 /cp 前缀。此外,在 cp-servlet.xml 我添加了以下内容:

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/cp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多