简体   繁体   English

Spring MVC Web应用程序中的Hello World应用程序中的HTTP 404错误代码

[英]HTTP 404 error code in spring MVC web app for hello world app

I am trying to implement a simple Spring MVC web app.I build a project in eclipse And during deployment over tomcat i am getting this error. 我正在尝试实现一个简单的Spring MVC Web应用程序。我在Eclipse中构建了一个项目,而在通过Tomcat进行部署期间,我遇到了此错误。

Error Code: 错误代码: 在此处输入图片说明

Folder Structure 资料夹结构 在此处输入图片说明 Web.xml 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>

root-context.xml root-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 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;
        }






}

Console Log: 控制台日志:

   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

Deployment directory structure 部署目录结构 在此处输入图片说明

I ran project as Prject_> Run As>Run on Server 我以Prject_> Run As> Run on Server运行项目

When you deploy by default, if you have only one application deployed to tomcat, the context root will be '/'. 默认情况下部署时,如果仅将一个应用程序部署到tomcat,则上下文根将为'/'。 Unless you have specifically said that it is /desi then you should be using http://localhost:8080/ 除非您特别声明它是/desi否则您应该使用http://localhost:8080/

In Spring configuration file you have used 在您使用的Spring配置文件中

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

is it the package name where the controllers reside? 控制器所在的软件包名称吗? If no, change it to the actual package. 如果否,请将其更改为实际的软件包。

Next, the controller class should have @Controller annotation, without it Spring can't use other annotations needed to map the requested URL http://localhost:8080/desi/ . 接下来,控制器类应该具有@Controller批注,没有它,Spring不能使用其他必需的批注来映射请求的URL http://localhost:8080/desi/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM