HTTP状态404 - / spring-mvc / login

我试图测试Spring MVC并编写以下代码:

web.xml:http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd“version =”3.0“>

<display-name>To do List</display-name>

    <welcome-file-list>
            <welcome-file>login.do</welcome-file>
    </welcome-file-list>


     <servlet>
         <servlet-name>dispatcher</servlet-name>
         <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
         </servlet-class>
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/todo-servlet.xml</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>

     <servlet-mapping>
         <servlet-name>dispatcher</servlet-name>
         <url-pattern>/spring-mvc/*</url-pattern>
     </servlet-mapping>

</web-app>

待办事项-servlet.xml中:

<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-4.0.xsd
        http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-4.0.xsd">

        <context:component-scan base-package="com.ezmsip" />

        <mvc:annotation-driven />

    </beans>

LoginController.java:

package com.ezmsip.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    @RequestMapping(value="/login")
    @ResponseBody
    public String sayHello() {

         return "Hola Mundo";
    }
}

HTTP状态404错误:

HTTP Status 404 - /spring-mvc/login


type Status report

message /spring-mvc/login

description The requested resource is not available.

Apache Tomcat / 8.0.39

Tomcat控制台日志:

nov 29, 2016 10:07:47 AM org.apache.tomcat.util.digester.SetPropertiesRule    begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting    property 'source' to 'org.eclipse.jst.jee.server:login' did not find a matching     property.
nov 29, 2016 10:07:47 AM org.apache.tomcat.util.digester.SetPropertiesRule    begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting    property 'source' to 'org.eclipse.jst.jee.server:first-web-app' did not find a     matching property.
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener    log
INFO: Server version:        Apache Tomcat/8.0.39
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener    log
INFO: Server built:          Nov 9 2016 08:48:39 UTC
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener   log
INFO: Server number:         8.0.39.0
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 7
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.1
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre1.8.0_111
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_111-b14
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:           C:\Users\agaragor\workspaceNeon\RBWG\.metadata\.plugins\org.eclipse.wst.server.c    ore\tmp1
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Users\agaragor\Apache-Tomcat-V8\apache-    tomcat-8.0.39
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -    Dcatalina.base=C:\Users\agaragor\workspaceNeon\RBWG\.metadata\.plugins\org.eclip    se.wst.server.core\tmp1
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Users\agaragor\Apache-    Tomcat-V8\apache-tomcat-8.0.39
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -   Dwtp.deploy=C:\Users\agaragor\workspaceNeon\RBWG\.metadata\.plugins\org.eclipse.    wst.server.core\tmp1\wtpwebapps
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Users\agaragor\Apache-    Tomcat-V8\apache-tomcat-8.0.39\endorsed
nov 29, 2016 10:07:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
nov 29, 2016 10:07:47 AM 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_111\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windo    ws;C:/Program Files/Java/jre1.8.0_111/bin/server;C:/Program     Files/Java/jre1.8.0_111/bin;C:/Program     Files/Java/jre1.8.0_111/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\app\aga        ragor\product\11.2.0\dbhome_1\bin;C:\windows\system32;C:\windows;C:\windows\Syst    em32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files     (x86)\Skype\Phone\;C:\Program Files\Java\jdk1.8.0_65\bin;C:\Program Files     (x86)\PuTTY\;C:\Users\agaragor\Desktop;;.
nov 29, 2016 10:07:48 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
nov 29, 2016 10:07:48 AM org.apache.tomcat.util.net.NioSelectorPool    getSharedSelector
INFO: Using a shared selector for servlet write/read
nov 29, 2016 10:07:48 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
nov 29, 2016 10:07:48 AM org.apache.tomcat.util.net.NioSelectorPool    getSharedSelector
INFO: Using a shared selector for servlet write/read
nov 29, 2016 10:07:48 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1538 ms
nov 29, 2016 10:07:48 AM org.apache.catalina.core.StandardService     startInternal
INFO: Starting service Catalina
nov 29, 2016 10:07:48 AM org.apache.catalina.core.StandardEngine     startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.39
nov 29, 2016 10:07:56 AM org.apache.jasper.servlet.TldScanner scanJars
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.
nov 29, 2016 10:07:58 AM org.apache.jasper.servlet.TldScanner scanJars
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.
nov 29, 2016 10:07:58 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
nov 29, 2016 10:07:59 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
nov 29, 2016 10:07:59 AM org.springframework.web.servlet.DispatcherServlet     initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
nov 29, 2016 10:07:59 AM    org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet':     startup date [Tue Nov 29 10:07:59 BRST 2016]; root of context hierarchy
nov 29, 2016 10:07:59 AM     org.springframework.beans.factory.xml.XmlBeanDefinitionReader     loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-   INF/todo-servlet.xml]
nov 29, 2016 10:08:02 AM     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMappi    ng register
INFO: Mapped "{[/login]}" onto public java.lang.String     com.ezmsip.controller.LoginController.sayHello()
nov 29, 2016 10:08:02 AM     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapt    er initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace     'dispatcher-servlet': startup date [Tue Nov 29 10:07:59 BRST 2016]; root of     context hierarchy
nov 29, 2016 10:08:02 AM   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapt    er initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace        'dispatcher-servlet': startup date [Tue Nov 29 10:07:59 BRST 2016]; root of     context hierarchy
nov 29, 2016 10:08:02 AM org.springframework.web.servlet.DispatcherServlet    initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 3226 ms
nov 29, 2016 10:08:02 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
nov 29, 2016 10:08:02 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
nov 29, 2016 10:08:02 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 14164 ms

回答(2)

3 years ago

更改 web.xml 如下:

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

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

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/your-mapping/*</url-pattern>
</servlet-mapping>

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

3 years ago

我的问题是我的Eclipse Neon环境设置为使用JRE而不是JDK运行 . 我必须编辑Eclipse的init文件并设置指向JDK路径的-vm语句 .