我使用以下代码遇到此错误:
警告:找不到带有URI [/ SpringMVC /]的HTTP请求的映射 具有名称' spring-dispatcher'
的DispatcherServlet
HelloController.java
:
package com.xxx.hellocontroller;
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 arg0,
HttpServletResponse arg1) throws Exception {
ModelAndView modelandview = new ModelAndView("HelloPage");
modelandview.addObject("welcomeMessage", "Welcome to Spring MVC");
return modelandview;
}
}
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>SpringMVC</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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xxx.hellocontroller.HelloController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
HelloPage.jsp
:
<html>
<body>
<h1>First Spring MVC Application Demo</h1>
<h2>${welcomeMessage}</h2>
</body>
</html>
我错过了什么?
Console log
:
usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help | start | stop }
Aug 24, 2015 1:24:47 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SpringMVC' did not find a matching property.
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/7.0.57
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Nov 3 2014 08:39:16 UTC
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 7.0.57.0
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 7
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 6.1
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JAVA_HOME: C:\Program Files\Java\jdk1.8.0_25\jre
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_25-b18
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: C:\Users\g.v\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: C:\Users\g.v\Documents\MyDocs\tools\apache-tomcat-7.0.57
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\g.v\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Users\g.v\Documents\MyDocs\tools\apache-tomcat-7.0.57
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\g.v\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Users\g.v\Documents\MyDocs\tools\apache-tomcat-7.0.57\endorsed
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Aug 24, 2015 1:24:47 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\jdk1.8.0_25\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files (x86)/Java/jre1.8.0_51/bin/client;C:/Program Files (x86)/Java/jre1.8.0_51/bin;C:/Program Files (x86)/Java/jre1.8.0_51/lib/i386;c:\programdata\oracle\java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\system32\wbem;C:\Windows\system32\windowspowershell\v1.0\;c:\program files (x86)\java\jre7\bin;c:\program files\tortoisesvn\bin;c:\program files (x86)\ibm\gsk8\lib;C:\Program Files (x86)\Brackets\command;C:\Users\g.v\Downloads\eclipse;;.
Aug 24, 2015 1:24:47 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 24, 2015 1:24:47 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 24, 2015 1:24:47 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 833 ms
Aug 24, 2015 1:24:47 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 24, 2015 1:24:47 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.57
Aug 24, 2015 1:24:48 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [394] milliseconds.
Aug 24, 2015 1:24:50 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://www.springframework.org/tags is already defined
Aug 24, 2015 1:24:50 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://www.springframework.org/tags/form is already defined
Aug 24, 2015 1:24:50 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Aug 24, 2015 1:24:50 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 24, 2015 1:24:50 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 24, 2015 1:24:50 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2544 ms
Aug 24, 2015 1:24:51 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'spring-dispatcher'
Aug 24, 2015 1:24:51 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'spring-dispatcher': initialization started
Aug 24, 2015 1:24:51 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'spring-dispatcher-servlet': startup date [Mon Aug 24 13:24:51 ADT 2015]; root of context hierarchy
Aug 24, 2015 1:24:51 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]
Aug 24, 2015 1:24:51 PM org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping registerHandler
INFO: Mapped URL path [/welcome.html] onto handler '/welcome.html'
Aug 24, 2015 1:24:52 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'spring-dispatcher': initialization completed in 547 ms
Aug 24, 2015 1:24:52 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringMVC/] in DispatcherServlet with name 'spring-dispatcher'
答案 0 :(得分:0)
您只接受来自绝对路径'/'的请求。在servlet映射中包含它。它可能会解决您的问题
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>