我已經使用ant build.xml
文件成功地將我的小型web應用程序的war文件部署到我的本地tomcat 7.0.42服務器。當我輸入網址到我的應用程序localhost:8080/springapp/index.jsp
時,出現404錯誤。我不明白爲什麼當部署war文件時出現錯誤。在tomcat上部署的war文件給出了404錯誤
我知道我的部署成功了,因爲我可以看到在tomcat管理器應用程序localhost:8080/manager
中列出的/springapp
。我一直在關注本教程,http://docs.spring.io/docs/Spring-MVC-step-by-step/part1.html,幾乎完全改變了一些東西,因爲我使用的是tomcat 7,它使用的是tomcat 6.我目前正在步驟1.4。 任何幫助將不勝感激,我一直堅持在這個問題上超過一天的谷歌搜索和故障排除。
這裏是我的build.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project name="springapp" basedir="." default="usage">
<property file="build.properties"/>
<property name="src.dir" value="src"/>
<property name="web.dir" value="war"/>
<property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
<property name="name" value="springapp"/>
<path id="master-classpath">
<fileset dir="${web.dir}/WEB-INF/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="${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.5" target="1.5" 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>
<path id="catalina-ant-classpath">
<fileset dir="${appserver.home}/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>
</project>
這裏是我的web.xml文件:
<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" >
<welcome-file-list>
<welcome>
index.jsp
</welcome>
</welcome-file-list>
</web-app>
這裏是我的properties.build文件:
appserver.home=/Users/joe/projects/tomcat
appserver.lib=${appserver.home}/lib
deploy.path=${appserver.home}/webapps
tomcat.manager.url=http://localhost:8080/manager/text
tomcat.manager.username=someone
tomcat.manager.password=something
這裏我的tomcat-users.xml文件:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="someone" password="something"
roles="manager-gui,manager-script,manager-jmx,manager-status"/>
</tomcat-users>
該應用程序已部署,但啓動了嗎? tomcat日誌說什麼? –
你的意思是應用程序啓動了什麼?我用sh startup.sh通過終端啓動了tomcat。然後我使用ant build來部署它。關於檢查日誌,我應該檢查哪一個以及我應該尋找什麼?有localhost,本地主機訪問,catalina,catalina.out和管理員日誌。我檢查了他們所有,並沒有錯誤消息,如果這就是我正在尋找。 – Joe91
看看ant文件中的用法指示。你執行什麼目標?你有沒有執行'start'目標,正如用法所示,它是用來啓動應用程序的? –