让tomcat自动部署上下文

时间:2016-01-31 11:11:25

标签: java tomcat servlets deployment

我有一个名为' projectx'的项目。我的目标是执行mvn clean install并让 Tomcat 使用 / target 文件夹自动部署webapp。

我将 projectx.xml 添加到 / tomcat / conf / Catalina / localhost /

以下内容
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:\Users\Username\workspace\projectx-webapp\target" path="/projectx-webapp"/>

我可以在服务器日志中看到成功处理文件 projectx.xml 。 webapp已成功部署。网站网址是 localhost:8080 / projectx-webapp / 。但是当我转到网址时,找不到网络应用。

当我在 tomcat \ webapps 中手动部署war文件时,当我转到指定的url时,webapp正常工作。 我是否错过了 projectx.xml 中的配置?

我的 web.xml

<web-app id="projectx-webapp" 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">

 <display-name>Project Web Application</display-name>

 <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
 </servlet-mapping>

</web-app>

我的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>projectx</groupId>
    <artifactId>projectx-webapp</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>projectx-webapp Maven Webapp</name>
    <properties>
        <!-- Generic properties -->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Spring -->
        <spring.version>4.2.4.RELEASE</spring.version>
    </properties>
    <dependencies>
       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Below declared dependencies are included for the servers who may complain about servlet/jstl missing dependency -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>projectx-webapp</finalName>
    </build>
</project>

1 个答案:

答案 0 :(得分:1)

正如我们在聊天中验证的那样,您的应用程序运行良好,问题是应用程序使用的上下文是/projectx而不是您期望的/projectx-webapp

之所以发生这种情况,是因为当您将文件apache-tomcat-8.0.30\conf\Catalina\localhost\projectx.xml更改为apache-tomcat-8.0.30\conf\Catalina\localhost\projectx-webapp.xml时,指定了已创建文件/projectx-webapp的部署目录,该应用程序已使用您预期的上下文进行部署<div class="form"> <div class="thumbnail"><img src="images/schedule_icon.png"/></div> <form action="lecturerLogin" class="register-form"> <input type="text" name="lecturerID" placeholder="Lecturer ID"/> <input type="password" name="lecturerPassword" placeholder="Password"/> <button>Login</button> <p class="message">Forgot password?<a href="#">Recover here</a></p> </form> <form action="adminLogin" class="login-form"> <input type="text" name="adminname" placeholder="Admin Name"/> <input type="password" name="adminpassword" placeholder="Password"/> <button>Login</button> <p class="message">Not an admin? <a href="#">Login here</a></p> </form> </div>