Spring MVC错误:org.springframework.web.servlet.DispatcherServlet noHandlerFound

时间:2017-07-30 14:50:10

标签: spring jsp spring-mvc

我创建了一个简单的Spring MVC项目来显示" Hello World"它工作正常。现在,我想添加一些css属性。我在WebContent中创建了一个css文件夹,并在其中添加了一个css文件,并在我的调度程序servlet中添加了以下行:

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

现在我在控制台上遇到以下错误:

org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringTest/] in DispatcherServlet with name 'spring'

这是我的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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringGradle</display-name>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/spring-servlet.xml</param-value>         
       </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>        
    </servlet-mapping>  
</web-app>

这是我的Dispatcher servlet:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    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.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.pawan.controller" />
    <mvc:resources mapping="/css/**" location="/css/" />

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

</beans>

和JSP文件:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<spring:url value="/css/myStyleCss.css" var="myStyleCss" />
<link href="${myStyleCss}" rel="stylesheet" />

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
 <h2 class="test">Welcome</h2><br>
<h3>${message}</h3>
</body>
</html>

控制器类:

package com.pawan.controller;

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

@Controller
public class LoginController {

    @RequestMapping("/")
    public ModelAndView login() {

        ModelAndView model = new ModelAndView("home");
        model.addObject("message", "Hello !!! First Spring Program");
        return model;
    }
}

请帮助我清理我的概念。

1 个答案:

答案 0 :(得分:0)

已解决:我的调度程序servlet中缺少以下行:

<mvc:annotation-driven />