【问题标题】:No mapping found for HTTP request with URI [/ChickenTest/index] in DispatcherServlet with name 'dispatcherServlet' [duplicate]在名称为“dispatcherServlet”的 DispatcherServlet 中找不到具有 URI [/ChickenTest/index] 的 HTTP 请求的映射 [重复]
【发布时间】:2013-11-07 19:52:18
【问题描述】:

当我把它放在我的 url "http:// localhost:8080 /ChickenTest/index.jsp" 上时,浏览器会显示这条消息 “请求的资源 (/ChickenTest/index.jsp) 不可用。” 并且 Eclipse 控制台显示这条消息 " 在名为 'dispatcherServlet' 的 DispatcherServlet 中找不到带有 URI [/ChickenTest/index] 的 HTTP 请求的映射"

控制器:

package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import service.EggService;

@Controller
public class GeneralController {

    @Autowired
    EggService eggService;

    //jsp loaders
    @RequestMapping(value="/index")
    ModelAndView index(){
        ModelAndView mav = new ModelAndView();

        return mav;
    }

    //jsp loaders
    @RequestMapping(value="/Egg/indexEgg")
    ModelAndView indexEgg(){
        ModelAndView mav = new ModelAndView();

        return mav;
    }

    @RequestMapping(value="/Chicken/indexChicken")
    ModelAndView indexChicken(){
        ModelAndView mav = new ModelAndView();

        return mav;
    }

    @RequestMapping(value="/Farm/indexFarm")
    ModelAndView indexFarm(){
        ModelAndView mav = new ModelAndView();

        return mav;
    }
}

mvc-config.xml:

<?xml version="1.0" encoding="UTF-8"?>

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

    <!-- Uncomment and your base-package here:
         <context:component-scan
            base-package="org.springframework.samples.web"/>  -->


    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
    </bean>

</beans>

web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>ChickenTest</display-name>

   <!--
        - Location of the XML file that defines the root application context.
        - Applied by ContextLoaderListener.
    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!--
        - Servlet that dispatches request to registered handlers (Controller implementations).
    -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

【问题讨论】:

  • @RequestMapping(value="/Chicken/indexChicken") 您将 Chicken 设置为映射,但在您使用 ChickenTest 的 url 中
  • 非常感谢,我可以清楚地看到我的错误。
  • 没问题,让我知道它是否有效,然后我会发布一个答案,以便您将其标记为已接受
  • 是的,这是正确的答案:)
  • 出于这个原因,我发现记录 Spring 记录器语句非常有帮助。在应用程序启动时,Spring 记录 RequestMappings(我认为日志会根据您正在实现的 HandlerMapping 略有不同)。然后,您可以将异常中列出的 URI 与应用程序启动时打印的 RequestMappings 进行比较(字面意思是控制台中的 Ctrl+F)。如果找不到,可能是某个地方打错了。

标签: java jsp spring-mvc


【解决方案1】:

在 mvc-config.xml 中,取消注释代码:

<!-- Uncomment and your base-package here:
         <context:component-scan
            base-package="org.springframework.samples.web"/>  -->

添加你的基础包,我想它的控制器什么的。 我认为这是问题所在。

【讨论】:

    【解决方案2】:

    您正在使用路径 ChickenTest 访问 url,但您已将其映射到 Controller 中为 Chicken @RequestMapping(value="/Chicken/indexChicken")

    RequestMapping 替换为ChickenTest 或使用Chicken 访问网址。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-25
      • 2016-12-22
      • 2015-02-18
      • 2013-11-19
      • 1970-01-01
      • 2013-09-12
      • 2016-04-01
      • 2021-01-19
      相关资源
      最近更新 更多