警告:在DispatcherServlet中找不到带有URI [/Spring3MVC/hello.html]的HTTP请求的映射,名称为“spring”

时间:2015-03-17 06:46:01

标签: java spring spring-mvc

为什么会这样。

"WARNING: No mapping found for HTTP request with URI [/Spring3MVC/hello.html] 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>Spring3MVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>

spring.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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 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="net.viralpatel.spring3.controller"/>

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

index.jsp是

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <a href="hello.html">Say Hello</a>
</body>
</html>

hello.jsp是

<html>
<body>
    <h1>Message : ${message}</h1>   
</body>
</html>

HelloWorldController.java是

package net.viralpatel.spring3.controller;

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

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView helloWorld() {

        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message);
    }
}

为什么上面的警告正在进行而没有击中控制器。

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

4 个答案:

答案 0 :(得分:0)

尝试添加<mvc:annotation-driven />

答案 1 :(得分:0)

我认为问题应该是ModelAndView应该是 ModelAndView("hello.jsp","message",message)如果不是,则它与viewResolver的模式.jsp不匹配。

答案 2 :(得分:0)

您需要<mvc:annotation-driven />。在<context:component-scan base-package="net.viralpatel.spring3.controller"/&gt;之后添加 。这将注册Spring默认HandlerMapping和HandlerAdapter,它是向控制器发送请求所必需的。

答案 3 :(得分:0)

<a href="hello.html">Say Hello</a>

这将查找映射/hello.html,并且这不会映射到控制器中的任何位置,这就是您收到此错误的原因。

请注意,所有请求都通过dispatcherServlet,它将从已注册的请求映射中查找每个URL或操作。如果未映射或注册url或资源映射,则会出现此错误。

其次,您的viewResolver会在每个pafe解析后附加.jsp。因此,当您要求弹出的实际页面为hello.html.jsp