Java org.springframework.web.servlet.DispatcherServlet noHandlerFound 用于基本 spring 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18027619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:52:27  来源:igfitidea点击:

org.springframework.web.servlet.DispatcherServlet noHandlerFound for basic spring example

javaspringspring-mvcspring-3

提问by Sandeep Rao

I'm getting following error when i try to deploy the application in tomcat server. Please help me solve this issue. localhost:8080/EnhancedRoyaltyTool/ is my home page and it is throwing the error.

当我尝试在 tomcat 服务器中部署应用程序时出现以下错误。请帮我解决这个问题。localhost:8080/EnhancedRoyaltyTool/ 是我的主页,它抛出错误。

Log Message:

日志消息:

  Aug 02, 2013 8:33:37 PM org.apache.catalina.core.AprLifecycleListener init
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.7.0_25\jre\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;c:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\VSShell\Common7\IDE\;c:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;c:\Program Files\Microsoft SQL Server0\Tools\Binn\;c:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;C:\Python27;C:\Python27\Scripts;.
Aug 02, 2013 8:33:38 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:EnhancedRoyaltyTool' did not find a matching property.
Aug 02, 2013 8:33:38 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 02, 2013 8:33:38 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 02, 2013 8:33:38 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1007 ms
Aug 02, 2013 8:33:38 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 02, 2013 8:33:38 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.42
Aug 02, 2013 8:33:40 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Aug 02, 2013 8:33:40 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Aug 02, 2013 8:33:40 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
Aug 02, 2013 8:33:40 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Aug 02 20:33:40 CDT 2013]; root of context hierarchy
Aug 02, 2013 8:33:40 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
Aug 02, 2013 8:33:41 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@196f4e87: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,viewResolver,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
Aug 02, 2013 8:33:42 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 1391 ms
Aug 02, 2013 8:33:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.42\webapps\docs
Aug 02, 2013 8:33:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.42\webapps\examples
Aug 02, 2013 8:33:42 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Aug 02, 2013 8:33:42 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Aug 02, 2013 8:33:42 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@5a5f31b0')
Aug 02, 2013 8:33:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.42\webapps\host-manager
Aug 02, 2013 8:33:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.42\webapps\manager
Aug 02, 2013 8:33:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.42\webapps\ROOT
Aug 02, 2013 8:33:42 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 02, 2013 8:33:42 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 02, 2013 8:33:42 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4363 ms
Aug 02, 2013 8:33:43 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/EnhancedRoyaltyTool/] in DispatcherServlet with name 'dispatcher'
My web.xml file 

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">

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

        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>

My dispatcher-servlet.xml

我的调度程序-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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:component-scan base-package="com.saas.royalties.controller" />
    <mvc:annotation-driven />

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

My SearchController.java

我的 SearchController.java

package com.saas.royalties.controller;

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

@Controller
public class SearchController {

     @RequestMapping(value="/")
        public String printHelloWorld(ModelMap model) {
            model.addAttribute("test", "test World!");
            System.out.println("test");
            return "hello";
        }

}

My pom.xml file

我的 pom.xml 文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.saas.royalty</groupId>
    <artifactId>EnhancedRoyaltyTool</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>EnhancedRoyaltyTool Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <org.springframework.version>3.1.2.RELEASE</org.springframework.version>
    </properties>

    <dependencies>
        <!-- Spring MVC depends on spring-core, spring-beans, spring-context, spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>EnhancedRoyaltyTool</finalName>
    </build>
</project>

My folder structure ![Folder structure][1]

我的文件夹结构![文件夹结构][1]

采纳答案by Sandeep Rao

Adding <mvc:default-servlet-handler/>in my dispatcher-servlet has fixed the issue

添加<mvc:default-servlet-handler/>在我的调度程序 servlet 已解决该问题

回答by MGorgon

It's problem with URI. How you invokes this servlet?

URI 有问题。你如何调用这个servlet?

回答by chrylis -cautiouslyoptimistic-

You told the servlet container in your servlet-mappingthat you want the Spring Dispatcher attached to /, and you told Spring that the printHelloWorldmethod should be used to handle requests for /relative to the servlet base (which is just /). But then you're trying to load /EnhancedRoyaltyTool/, and you haven't told the servlet container or Spring to attach anything there. You can change the RequestMappingto "/EnhancedRoyaltyTool/", move the entire Spring servlet to "/EnhancedRoyaltyTool/*"in the servlet-mapping, or just try visiting http://localhost:8080/.

您告诉您中的 servlet 容器servlet-mapping您希望将 Spring Dispatcher 附加到/,并且您告诉 Spring 该printHelloWorld方法应该用于处理/相对于 servlet 基础(即/)的请求。但是随后您正在尝试加载/EnhancedRoyaltyTool/,并且您还没有告诉 servlet 容器或 Spring 在那里附加任何内容。您可以将 更改RequestMapping"/EnhancedRoyaltyTool/",将整个 Spring servlet 移动到"/EnhancedRoyaltyTool/*"servlet-mapping,或者只是尝试访问http://localhost:8080/.

回答by user6555043

Firstly, check Dispatcher Servlet. If you also check it then, simple way: you need not add External jars by Build path. Go to the project workspace by folder and directly copy downloaded jars and paste into:

首先,检查 Dispatcher Servlet。如果你也检查它,简单的方法:你不需要通过构建路径添加外部罐子。按文件夹进入项目工作区,直接将下载的jar包复制粘贴到:

WebContent>WEB-INF>lib>(here directly paste your jar files.)

-> stop your application server (like tomcat);
-> go to your IDE -> Select Project -> Clean Project builds 

After that, you go to: run your project. I hope it works!

之后,你去:运行你的项目。我希望它有效!