如何调试“找不到HTTP请求的映射”?

时间:2011-07-17 12:16:43

标签: java spring servlets

我正在尝试使用Spring 3.0映射Controller而没有成功。我有以下错误:

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

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

2011-07-17 20:10:28,402 [main] INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - 映射的URL路径[/ cp / index]到处理程序'index' 2011-07-17 20:10:28,402 [main] INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - 映射的URL路径[/cp/index.*]到处理程序'index' 2011-07-17 20:10:28,402 [main] INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - 映射的URL路径[/ cp / index /]到处理程序'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中定义了类索引

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 
    }

}

我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

我不确定因为我无法测试,但尝试改变这个:

<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中省略。

答案 1 :(得分:0)

我设法让它与以下更改一起使用:

@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>