首页 > 解决方案 > 在 Spring MVC 项目中出现错误“在 DispatcherServlet 中找不到带有 URI [/FirstSpringMvcProj/] 的 HTTP 请求的映射”

问题描述

我是 Spring MVC 的初学者,并尝试执行 youtube 上教程中给出的演示程序。

https://www.youtube.com/watch?v=qXIESogSi_Q&index=6&list=PLBgMUB7xGcO31B2gBmy1igpZn6LK78-CJ

演示代码没有使用注释,并且在运行程序时我收到 HTTP 404 错误。

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>FirstSpringMvcProj</display-name>

<servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>  

</web-app>

spring-dispatcher-servlet.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-2.5.xsd">  

 <bean id = "HandlerMapping" 
 class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<bean name = "/welcome.html" class="com.demo.controller.HelloController"/>

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

HelloController.java

package com.demo.controller;

import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;  

public class HelloController extends AbstractController {  

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse res) throws Exception  {  
    ModelAndView modelAndView = new  ModelAndView("HelloPage");
    modelAndView.addObject("welcomeMessage","Hi, Welcome to my Page");
    return modelAndView;  
}
}  

HelloPage.jsp

<html>
<body>
<h1>Message is: ${welcomeMessage}</h1>>
</body>
</html>

在运行上述代码时,我在控制台中遇到以下错误

Aug 25, 2018 10:31:43 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FirstSpringMvcProj/] in DispatcherServlet with name 'spring-dispatcher'
Aug 25, 2018 10:43:03 PM org.apache.catalina.core.StandardContext reload

完整的服务器日志:

Aug 26, 2018 5:50:28 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:FirstSpringMvcProj' did not find a matching property.
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.90
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Jul 2 2018 17:05:37 UTC
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.90.0
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 10
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre1.8.0_131
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_131-b11
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         F:\Study\Spring_Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         F:\Study\apache-tomcat-7.0.90\apache-tomcat-7.0.90
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=F:\Study\Spring_Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=F:\Study\apache-tomcat-7.0.90\apache-tomcat-7.0.90
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=F:\Study\Spring_Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=F:\Study\apache-tomcat-7.0.90\apache-tomcat-7.0.90\endorsed
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Aug 26, 2018 5:50:28 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_131\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_131/bin/server;C:/Program Files/Java/jre1.8.0_131/bin;C:/Program Files/Java/jre1.8.0_131/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Android;C:\Windows\System32;%SYSTEMROOT%\System32\OpenSSH\;C:\Users\Dell\AppData\Local\Microsoft\WindowsApps;;F:\Study\Eclipse_Mars;;.
Aug 26, 2018 5:50:28 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 26, 2018 5:50:28 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 26, 2018 5:50:28 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1136 ms
Aug 26, 2018 5:50:28 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 26, 2018 5:50:28 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.90
Aug 26, 2018 5:50:28 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
WARNING: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [132] milliseconds.
Aug 26, 2018 5:50:30 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://www.springframework.org/tags is already defined
Aug 26, 2018 5:50:30 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://www.springframework.org/tags/form is already defined
Aug 26, 2018 5:50:30 PM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Aug 26, 2018 5:50:30 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Aug 26, 2018 5:50:30 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 26, 2018 5:50:30 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 26, 2018 5:50:30 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2178 ms
Aug 26, 2018 5:50:33 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'spring-dispatcher'
Aug 26, 2018 5:50:33 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'spring-dispatcher': initialization started
Aug 26, 2018 5:50:33 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'spring-dispatcher-servlet': startup date [Sun Aug 26 17:50:33 IST 2018]; root of context hierarchy
Aug 26, 2018 5:50:33 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]
Aug 26, 2018 5:50:34 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/welcome] onto handler '/welcome'
Aug 26, 2018 5:50:34 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'spring-dispatcher': initialization completed in 607 ms
Aug 26, 2018 5:50:34 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FirstSpringMvcProj/] in DispatcherServlet with name 'spring-dispatcher'
Aug 26, 2018 5:51:17 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FirstSpringMvcProj/] in DispatcherServlet with name 'spring-dispatcher'
Aug 26, 2018 5:51:31 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FirstSpringMvcProj/welcome.html] in DispatcherServlet with name 'spring-dispatcher' 

在浏览器中打开 url http://localhost:8080/FirstSpringMvcProj/welcome.html我收到 HTTP 404 错误。有人可以帮助我了解导致此错误的原因。

我还尝试使用 Anootation 并修改我的 HelloController 和 spring-dispatcher-servlet.xml 文件,如下所示。但是,我仍然得到同样的错误。

HelloController.java

package com.demo.controller;

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

@Controller
public class HelloController {  

@RequestMapping ("/welcome")
protected ModelAndView helloWorld(){  
    ModelAndView modelAndView = new  ModelAndView("HelloPage");
    modelAndView.addObject("welcomeMessage","Hello World !!");
    return modelAndView;  
}
}  

spring-dispatcher-servlet.xml

<beans xmlns = "http://www.springframework.org/schema/beans"
    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-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 = "com.demo.controller"/> 

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

标签: javaspringspring-mvcjspservlets

解决方案


这里的问题可能与 ModelAndView 导入有关

代替 import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.portlet.ModelAndView;

https://stackoverflow.com/a/29096411/1664430


推荐阅读