在名称为___的DispatcherServlet中找不到具有URL ___的HTTP请求的映射

时间:2018-04-06 20:44:40

标签: java spring spring-mvc

我一直在寻找解决此错误的方法,但无法找到答案。 保持这个错误:

  

2018年4月6日下午1:40:36 org.springframework.web.servlet.DispatcherServlet noHandlerFound

     

警告:在DispatcherServlet中找不到带有URI [/ spring-tutorial /]的HTTP请求的映射,其名称为' offers'

我在JDK 1.8,tomcat 8.0上运行,并在最新的eclipse IDE上运行。

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>spring-tutorial</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <description></description>
        <display-name>offers</display-name>
        <servlet-name>offers</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>

    </servlet>
    <servlet-mapping>
        <servlet-name>offers</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

我的offer-servlet.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:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc 
                            http://www.springframework.org/schema/mvc/spring-mvc-3.2.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-3.2.xsd">
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan 
            base-package="com.emenpy.spring.web.controllers">
    </context:component-scan>
    <bean id="jspViewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

我的OffersController.java

package com.emenpy.spring.web.controllers;

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

@Controller
public class OffersController {

    @RequestMapping("/")
    public String showHome() {
        return "home";
    }

}

我的home.jsp

<html>
    <body>
        <h2>Hello World!</h2>
    </body>
</html>

这是我的目录树: enter image description here

1 个答案:

答案 0 :(得分:1)

你的jsp应该被称为“home.jsp”而不是“index.jsp”

您的扫描包名称是

<context:component-scan 
        base-package="com.emenpy.spring.web.controllers">
</context:component-scan>

当它应该是你的目录树显示

<context:component-scan 
        base-package="com.cuong.spring.web.controllers">
</context:component-scan>

因为扫描没有找到你的控制器,映射没有完成