【问题标题】:HTTP 404 error code in spring MVC web app for hello world apphello world 应用程序的 Spring MVC Web 应用程序中的 HTTP 404 错误代码
【发布时间】:2016-07-14 02:16:42
【问题描述】:

我正在尝试实现一个简单的 Spring MVC Web 应用程序。我在 Eclipse 中构建了一个项目 在通过 tomcat 部署期间,我遇到了这个错误。

错误代码:

文件夹结构 Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">

  <display-name>Theka Desi Khana</display-name>
  <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
     <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

根上下文.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


</beans>

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <context:component-scan base-package="com.theka.desi.Controllers" />



    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->

     <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean> 


    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />    




</beans:beans>

家庭控制器

   package com.theka.desi.Controllers;


import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/")
public class HomeController {
    @Autowired
    private ServletContext servletContext;
    private final Logger logger = Logger.getLogger(HomeController.class);

    public ServletContext getServletContext() {
        return servletContext;
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }



    @RequestMapping("/")
        public String welcome() {
            return "redirect:/home";
        }

        @RequestMapping("/home")
        public ModelAndView home() {
            ModelAndView view = new ModelAndView("index");

            return view;
        }






}

控制台日志:

   Mar 26, 2016 6:09:56 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:desi' did not find a matching property.
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.65
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Oct 9 2015 08:36:58 UTC
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.65.0
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 8.1
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.3
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jdk1.7.0_79\jre
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.7.0_79-b15
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\Users\nand\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         G:\Softy\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\nand\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=G:\Softy\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\nand\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=G:\Softy\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65\endorsed
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Mar 26, 2016 6:09:56 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.7.0_79\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 (x86)\Git\cmd;C:\Program Files (x86)\Git\bin;C:\Program Files (x86)\Common Files\lenovo\easyplussdk\bin;C:\Program Files (x86)\Skype\Phone\;.
Mar 26, 2016 6:09:56 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 26, 2016 6:09:56 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 26, 2016 6:09:56 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1234 ms
Mar 26, 2016 6:09:56 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 26, 2016 6:09:56 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.65
Mar 26, 2016 6:09:59 PM org.apache.catalina.startup.TldConfig execute
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.
Mar 26, 2016 6:09:59 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Mar 26, 2016 6:09:59 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
Mar 26, 2016 6:09:59 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'appServlet'
Mar 26, 2016 6:10:00 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 26, 2016 6:10:00 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 26, 2016 6:10:00 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4357 ms
Mar 26, 2016 6:10:50 PM org.apache.catalina.loader.WebappClassLoaderBase modified
SEVERE:     Resource '/WEB-INF/classes/com/theka/desi/Controllers/HomeController.class' is missing
Mar 26, 2016 6:10:50 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/desi] has started
Mar 26, 2016 6:10:50 PM org.apache.catalina.core.ApplicationContext log
INFO: Destroying Spring FrameworkServlet 'appServlet'
Mar 26, 2016 6:10:50 PM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Mar 26, 2016 6:10:52 PM org.apache.catalina.startup.TldConfig execute
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.
Mar 26, 2016 6:10:52 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Mar 26, 2016 6:10:52 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
Mar 26, 2016 6:10:53 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'appServlet'
Mar 26, 2016 6:10:53 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/desi] is completed

部署目录结构

我将项目运行为 项目_>运行方式>在服务器上运行

【问题讨论】:

  • catalina.out 的任何日志
  • @andy 我已经添加了日志。没有日志相关的映射。
  • 日志未显示应用已部署。
  • 我同意@RomanC。您确定您的应用已配置为部署在 Tomcat 服务器中吗?
  • 这些不是日志中的 desi,也许你需要尝试将你的项目发布到 tomcat

标签: java xml spring spring-mvc


【解决方案1】:

在你使用过的Spring配置文件中

<context:component-scan base-package="com.theka.desi.Controllers" />

是控制器所在的包名吗?如果不是,请更改为实际的包。

接下来,控制器类应该有@Controller注解,没有它Spring不能使用其他注解来映射请求的URLhttp://localhost:8080/desi/

【讨论】:

  • 包文件路径是正确的,并且在我的控制器类中也提到了@controller。我已经更新了有问题的类。请看一下并提出一些解决方案
  • 什么是 Spring 版本?
  • 4.0.3.RELEASE
【解决方案2】:

默认部署时,如果只有一个应用部署到tomcat,上下文根为'/'。除非你明确说是/desi,否则你应该使用http://localhost:8080/

【讨论】:

  • 很抱歉应该说“由 eclipse 部署”而不是“在 tomcat 中部署”。如果将它添加到 Tomcat,那么除非 tomcat webapp 中的目录名为“ROOT”,否则它将使用应用程序文件夹的名称(因此,如果您使用 myapp,它将期望 localhost:8080\myapp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 2015-07-09
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 2018-01-29
相关资源
最近更新 更多