用于hello world app的spring MVC web app中的HTTP 404错误代码

时间:2016-03-26 11:51:20

标签: java xml spring spring-mvc

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

错误代码: enter image description here

文件夹结构 enter image description here 的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>

根context.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>

的HomeController

   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

部署目录结构    enter image description here

我把项目作为  Prject_&GT;运行方式&gt;在服务器上运行

2 个答案:

答案 0 :(得分:0)

默认情况下部署时,如果只有一个应用程序部署到tomcat,则上下文根将为&#39; /&#39;。除非您明确说明它是/desi,否则您应该使用http://localhost:8080/

答案 1 :(得分:0)

在Spring配置文件中,您已使用

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

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

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