【问题标题】:org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [duplicate]org.springframework.web.servlet.PageNotFound noHandlerFound 警告:没有为带有 URI 的 HTTP 请求找到映射 [重复]
【发布时间】:2015-05-05 12:02:28
【问题描述】:

我是 Spring MVC 框架的新手。我正在尝试在 Eclipse Juno 和 Tomcat(V 7.0.61)中使用 Spring 4 设置示例代码库。我尝试了不同论坛中所述的不同选项,但没有一个有效。非常感谢任何解决此问题的帮助!

下面是代码:

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_3_0.xsd" id="WebApp_ID" version="3.0">
   <display-name>Spring MVC Application</display-name>

   <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

</web-app>

HelloWeb-servlet.xml

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

    <context:component-scan base-package="com.example.HelloController" />
<!--    <mvc:default-servlet-handler /> -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

HelloController.java

package com.example;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/hello")
public class HelloController{

   @RequestMapping(method = RequestMethod.GET )
   public String printHello(ModelMap model) {


      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }
}

服务器日志:

May 5, 2015 5:26:32 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:HelloWeb' did not find a matching property.
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.61
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Mar 27 2015 12:03:56 UTC
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.61.0
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 7
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.1
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          x86
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files (x86)\Java\jdk1.6.0_31\jre
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.6.0_31-b05
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Sun Microsystems Inc.
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\U175827\Workspaces\jBpm\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\U175827\apache-tomcat-7.0.61
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\U175827\Workspaces\jBpm\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\U175827\apache-tomcat-7.0.61
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\U175827\Workspaces\jBpm\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\U175827\apache-tomcat-7.0.61\endorsed
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Xbootclasspath:C:\Program
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: Files
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: (x86)\Java\jdk1.6.0_31\jre\lib\resources.jar;C:\Program
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: Files
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: (x86)\Java\jdk1.6.0_31\jre\lib\rt.jar;C:\Program
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: Files
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: (x86)\Java\jdk1.6.0_31\jre\lib\jsse.jar;C:\Program
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: Files
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: (x86)\Java\jdk1.6.0_31\jre\lib\jce.jar;C:\Program
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: Files
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: (x86)\Java\jdk1.6.0_31\jre\lib\charsets.jar;C:\U175827\jboss-5.1.0.GA-jdk6\jboss-5.1.0.GA\common\lib\servlet-api.jar
May 5, 2015 5:26:32 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 (x86)\Java\jdk1.6.0_31\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/U175827/eclipse/jre/bin/client;C:/U175827/eclipse/jre/bin;C:/U175827/eclipse/jre/lib/i386;C:\Program Files (x86)\Java\jdk1.6.0_31\bin;C:\Program Files (x86)\Java\jdk1.6.0_31\jre\bin;C:\Oracle\product\11.2.0\client_1\bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft Application Virtualization Client;C:\Program Files\Citrix\ICAService\;C:\Program Files\Citrix\System32\;C:\Program Files (x86)\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files (x86)\Microsoft SQL Server\90\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Common Files\Citrix\System32\;C:\U175827\unix-utils;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\U175827\eclipse;;.
May 5, 2015 5:26:32 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8082"]
May 5, 2015 5:26:32 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8010"]
May 5, 2015 5:26:32 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1809 ms
May 5, 2015 5:26:32 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
May 5, 2015 5:26:32 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.61
May 5, 2015 5:26:33 PM org.apache.tomcat.websocket.server.WsSci onStartup
INFO: JSR 356 WebSocket (Java WebSocket 1.1) support is not available when running on Java 6. To suppress this message, run Tomcat on Java 7, remove the WebSocket JARs from $CATALINA_HOME/lib or add the WebSocket JARs to the tomcat.util.scan.DefaultJarScanner.jarsToSkip property in $CATALINA_BASE/conf/catalina.properties. Note that the deprecated Tomcat 7 WebSocket API will be available. 
May 5, 2015 5:26:33 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [329] milliseconds.
May 5, 2015 5:26:36 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
May 5, 2015 5:26:36 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'HelloWeb'
May 5, 2015 5:26:36 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'HelloWeb': initialization started
May 5, 2015 5:26:36 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'HelloWeb-servlet': startup date [Tue May 05 17:26:36 IST 2015]; root of context hierarchy
May 5, 2015 5:26:36 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/HelloWeb-servlet.xml]
May 5, 2015 5:26:38 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'HelloWeb': initialization completed in 1750 ms
May 5, 2015 5:26:38 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8082"]
May 5, 2015 5:26:38 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8010"]
May 5, 2015 5:26:38 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 5487 ms
May 5, 2015 5:26:45 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/HelloWeb/hello] in DispatcherServlet with name 'HelloWeb'

我正在使用的网址:

http://localhost:8082/HelloWeb/hello

【问题讨论】:

  • 你做了 hello.jsp 吗?

标签: java eclipse spring spring-mvc web-applications


【解决方案1】:

您需要将此添加到您的web.xml

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>

<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>

【讨论】:

    【解决方案2】:

    您的配置中缺少 annotation-driven 元素

     <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"    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
       http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
       <!-- Enables the Spring MVC @Controller programming model -->
        <mvc:annotation-driven />
    
        <context:component-scan base-package="com.example" />
    <!--    <mvc:default-servlet-handler /> -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
    </beans> 
    

    还请注意,您需要将扫描的值设置为包,而不是类,因此请将您的设置从 &lt;context:component-scan base-package="com.example.HelloController" /&gt; 修改为 &lt;context:component-scan base-package="com.example" /&gt;

    【讨论】:

    • 我用了上面那个还是不行!
    • 注意到另一个问题,已在答案中编辑
    【解决方案3】:

    我在不同的系统中进行了相同的设置并且它工作正常,因此我认为 Eclipse/Tomcat 存在一些问题。

    【讨论】:

      【解决方案4】:

      如果 hello.jsp 放在 jsp 文件夹中,请将 @RequestMapping("/hello") 更改为 @RequestMapping("/")

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多