为什么我得到&#34; HTTP状态404 - Servlet <servlet name =“”>不可用&#34;

时间:2015-06-30 17:38:47

标签: spring spring-mvc tomcat servlets http-status-code-404

我正在经历this Spring web MVC tutorial,但我被困在第1.12节,我应该看到&#34;你好&#34;我的控制器处理的页面,但取而代之的是&#34; HTTP状态404 - Servlet不可用&#34;。我查看了其他具有相同错误的stackoverflow问题,但没有一个有帮助。

以下是相关文件:

/网络/ WEB-INF /的的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

    <!--
         The "Front Controller" (http://en.wikipedia.org/wiki/Front_Controller_pattern).
         that dispatches requests to registered handlers (Controller implementations).
      -->
    <servlet>
        <servlet-name>filth</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>filth</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

/网络/ WEB-INF /的污物-servlet.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- the application context definition for the filth DispatcherServlet -->

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

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

/网络/视图/的的hello.jsp

<html>
    <head><title>Hello :: Spring Application</title></head>
    <body>
        <h1>Hello - Spring Application</h1>
        <p>Greetings.</p>
     </body>
</html>

/ SRC /主/ JAVA / COM /污物/控制器/的 HelloController.java

package com.filth.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Controller for the "Hello" page
 */
@Controller
public class HelloController {

    private static final Log LOGGER = LogFactory.getLog(HelloController.class);

    @RequestMapping(value="/hello.html", method = RequestMethod.GET)
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        LOGGER.info("Returning hello view");

        return new ModelAndView("hello");
    }

}

/build.properties中

# Ant properties for building FiLTH

appserver.home=${env.WORKSPACE}/tomcat6
appserver.lib=${appserver.home}/lib
deploy.path=${appserver.home}/webapps

tomcat.manager.url=http://localhost:8080/manager
tomcat.manager.username=tomcat
tomcat.manager.password=s3cret

/build.xml

<?xml version="1.0"?>

<project name="filth" xmlns:ivy="antlib:org.apache.ivy.ant" basedir="." default="usage">

    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <pathelement path="${build.lib.dir}/ant-contrib.jar"/>
        </classpath>
    </taskdef>

    <taskdef name="ivy-configure" classname="org.apache.ivy.ant.IvyConfigure"/>
    <taskdef name="ivy-resolve" classname="org.apache.ivy.ant.IvyResolve"/>
    <taskdef name="ivy-retrieve" classname="org.apache.ivy.ant.IvyRetrieve"/>
    <taskdef name="ivy-deliver" classname="org.apache.ivy.ant.IvyDeliver"/> 
    <taskdef name="ivy-publish" classname="org.apache.ivy.ant.IvyPublish"/>

    <target name="ivy-resolve">
        <ivy:configure />

        <ivy:resolve file="${ivy.dep.file}" conf="${ivy.configurations}" />

        <ivy:retrieve pattern="${ivy.retrieve.pattern}" conf="${ivy.configurations}" />
        <echo message="ivy.dep.file: ${ivy.dep.file}"/>
        <echo message="ivy.configurations: ${ivy.configurations}"/>
        <echo message="ivy.retrieve.pattern: ${ivy.retrieve.pattern}"/>
    </target>

    <property environment="env"/>

    <property file="build.properties"/>
    <property file="build.passwords.properties" />

    <property name="src.dir" value="src/main/java"/>
    <property name="test.dir" value="src/test"/>
    <property name="web.dir" value="web/"/>
    <property name="build.dir" value="target"/>
    <property name="name" value="filth"/>
    <property name="resources.dir" value="src/main/resources"/>

    <path id="master-classpath">
        <fileset dir="lib">
            <include name="**/*.jar"/>
        </fileset>
        <!-- We need the servlet API classes: -->
        <!--  * for Tomcat 5/6 use servlet-api.jar -->
        <!--  * for other app servers - check the docs -->
        <fileset dir="${appserver.lib}">
            <include name="servlet*.jar"/>
        </fileset>
        <pathelement path="${resources.dir}"/>
        <pathelement path="${build.dir}"/>
    </path>

    <target name="usage">
        <echo message=""/>
        <echo message="${name} build file"/>
        <echo message="-----------------------------------"/>
        <echo message=""/>
        <echo message="Available targets are:"/>
        <echo message=""/>
        <echo message="build     --> Build the application"/>
        <echo message="deploy    --> Deploy application as directory"/>
        <echo message="deploywar --> Deploy application as a WAR file"/>
        <echo message="install   --> Install application in Tomcat"/>
        <echo message="reload    --> Reload application in Tomcat"/>
        <echo message="start     --> Start Tomcat application"/>
        <echo message="stop      --> Stop Tomcat application"/>
        <echo message="list      --> List Tomcat applications"/>
        <echo message=""/>
    </target>

    <target name="build" description="Compile main source tree java files">
        <mkdir dir="${build.dir}"/>
        <javac destdir="${build.dir}" source="1.6" target="1.6" debug="true"
               deprecation="false" optimize="false" failonerror="true">
            <src path="${src.dir}"/>
            <classpath refid="master-classpath"/>
        </javac>
    </target>

    <target name="deploy" depends="build" description="Deploy application">
        <copy todir="${deploy.path}/${name}" preservelastmodified="true">
            <fileset dir="${web.dir}">
                <include name="**/*.*"/>
            </fileset>
        </copy>
    </target>

    <target name="deploywar" depends="build" description="Deploy application as a WAR file">
        <war destfile="${name}.war" webxml="${web.dir}/WEB-INF/web.xml">
            <fileset dir="${web.dir}">
                <include name="**/*.*"/>
            </fileset>
        </war>
        <copy todir="${deploy.path}" preservelastmodified="true">
            <fileset dir=".">
                <include name="*.war"/>
            </fileset>
        </copy>
    </target>

    <target name="buildtests" description="Compile test tree java files">
        <mkdir dir="${build.dir}"/>
        <javac destdir="${build.dir}" source="1.6" target="1.6" debug="true"
            deprecation="false" optimize="false" failonerror="true">
            <src path="${test.dir}"/>
            <classpath refid="master-classpath"/>
        </javac>
    </target>

    <target name="tests" depends="build, buildtests" description="Run tests">
        <junit printsummary="on"
            fork="false"
            haltonfailure="false"
            failureproperty="tests.failed"
            showoutput="true">
            <classpath refid="master-classpath"/>
            <formatter type="brief" usefile="false"/>

            <batchtest>
                <fileset dir="${build.dir}">
                    <include name="**/*Test.*"/>
                </fileset>
            </batchtest>
        </junit>

        <fail if="tests.failed">
            tests.failed=${tests.failed}
            ***********************************************************
            ***********************************************************
            ****  One or more tests failed!  Check the output ...  ****
            ***********************************************************
            ***********************************************************
        </fail>
    </target>

<!-- ============================================================== -->
<!-- Tomcat tasks - remove these if you don't have Tomcat installed -->
<!-- ============================================================== -->

    <path id="catalina-ant-classpath">
        <!-- We need the Catalina jars for Tomcat -->
        <!--  * for other app servers - check the docs -->
        <fileset dir="${appserver.lib}">
           <include name="catalina-ant.jar"/>
           <include name="tomcat-coyote.jar"/>
           <include name="tomcat-util.jar"/>
        </fileset>
        <fileset dir="${appserver.home}/bin">
           <include name="tomcat-juli.jar"/>
        </fileset>
    </path>

    <taskdef name="install" classname="org.apache.catalina.ant.DeployTask">
        <classpath refid="catalina-ant-classpath"/>
    </taskdef>
    <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
        <classpath refid="catalina-ant-classpath"/>
    </taskdef>
    <taskdef name="list" classname="org.apache.catalina.ant.ListTask">
        <classpath refid="catalina-ant-classpath"/>
    </taskdef>
    <taskdef name="start" classname="org.apache.catalina.ant.StartTask">
        <classpath refid="catalina-ant-classpath"/>
    </taskdef>
    <taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
        <classpath refid="catalina-ant-classpath"/>
    </taskdef>

    <target name="install" description="Install application in Tomcat">
        <install url="${tomcat.manager.url}"
                 username="${tomcat.manager.username}"
                 password="${tomcat.manager.password}"
                 path="/${name}"
                 war="${name}"/>
    </target>

    <target name="reload" description="Reload application in Tomcat">
        <reload url="${tomcat.manager.url}"
                 username="${tomcat.manager.username}"
                 password="${tomcat.manager.password}"
                 path="/${name}"/>
    </target>

    <target name="start" description="Start Tomcat application">
        <start url="${tomcat.manager.url}"
                 username="${tomcat.manager.username}"
                 password="${tomcat.manager.password}"
                 path="/${name}"/>
    </target>

    <target name="stop" description="Stop Tomcat application">
        <stop url="${tomcat.manager.url}"
                 username="${tomcat.manager.username}"
                 password="${tomcat.manager.password}"
                 path="/${name}"/>
    </target>

    <target name="list" description="List Tomcat applications">
        <list url="${tomcat.manager.url}"
                 username="${tomcat.manager.username}"
                 password="${tomcat.manager.password}"/>
    </target>

<!-- End Tomcat tasks -->

</project>

/ivy.xml

<ivy-module version="2.0">
    <info organisation="org.apache" module="hello-ivy"/>
    <dependencies>
        <dependency org="org.springframework" name="spring-context" rev="4.1.6.RELEASE"/>
        <dependency org="org.springframework" name="spring-webmvc" rev="4.1.6.RELEASE"/>
        <dependency org="commons-logging" name="commons-logging" rev="1.2"/>
        <dependency org="javax.servlet" name="servlet-api" rev="2.5"/>
        <dependency org="junit" name="junit" rev="4.12"/>
    </dependencies>
</ivy-module>

导航到http://localhost:8080/filth/hello.html时出错。 catalina.out没有给出任何错误,我确实看到HelloController.java中的日志条目,但非常不一致 - 不知道为什么会这样。

请注意,http://localhost:8080/filth/index.jsphttp://localhost:8080/filth/view/hello.jsp工作 - 只是通过控制器无法按预期工作。

有人有什么想法吗? 如果您需要更多上下文/信息,请与我们联系。 谢谢!

1 个答案:

答案 0 :(得分:0)

想出来。我没有在部署中包含必要的jar依赖项 - 在WEB-INF中添加了一个lib /目录,并且常春藤在那里放置了必要的运行时依赖(&#34; deploy&#34; ant target从那里获取它) )。我之前没有看到这个错误,因为我没有查看正确的日志文件 - 最后看到日志输出转到[tomcat] / logs / localhost ... log。