java - HTTP 状态 404 -/spring-mvc/login

标签 java spring tomcat

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

网络.xml: http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd” 版本="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>

tod​​o-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>

登录 Controller .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

最佳答案

如下更改您的 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>

关于java - HTTP 状态 404 -/spring-mvc/login,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40865887/

相关文章:

java - Feign Client : Post a Map<String, Object> in Request Body => feign.FeignException: status 400 reading MAp

java - java中unsigned long的等价物是什么

java - 更改 Tomcat Set-Cookie header

spring - 未找到 HTTP 请求的映射

eclipse - 硬关闭 Eclipse 后无法启动服务器(tomcat 服务器已经启动)

tomcat - 如何使用liferay服务器配置热插拔代理

java - 如何让 Spring 使用多个上下文 Autowiring 集成测试类?

java - 回滚 @Transactional 注释的方法

json - 如何使用 Spring Boot 2 CacheManager 在 Redis 中存储非类型化 JSON

java - BasicDataSource 找不到 postgresql 或 postgis jdbc 驱动程序