tomcat不会启动war应用程序

u2nhd7ah  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(402)

其目的是将一个普通java应用程序转换为一个SpringWeb应用程序,该应用程序将在tomcat服务器上运行(war打包)。约束条件是不能使用spring引导,并且配置必须是xml类型(不是java配置)。应用程序应该提供一个静态页面,比如说“hello world”,但它也应该处理驻留在某个文件夹中的文件(此时不相关)。
pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>file-handler</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-inline</artifactId>
        <version>3.11.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>3.11.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.12.3</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>2.12.3</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.200</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.28</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.8</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.8</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <mainClass>com.example.Main</mainClass>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>

    </plugins>
</build>

tomcat可以服务于静态页面(如果是.jsp格式),而无需编写显式的internalviewresolver;所以在webapp文件夹中有一个index.jsp文件(静态页面的呈现(效果良好)
在src/main/resources中有一个beans.xml文件,其中声明了所有bean,如下所示:

<?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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:log4j.properties, classpath:application.properties"/>

    <bean id="jdbcFileRepository" class="com.example.data.JdbcFileRepository">
        <constructor-arg index="0" value="File_Handler_Table"/>
        <constructor-arg index="1" value="jdbc:h2:/home/user/repo/file-handler"/>
    </bean>

    <bean id = "fileHandler" class = "com.example.service.FileHandler">
        <constructor-arg index="0" ref="jdbcFileRepository"/>
    </bean>
</beans>

main方法应创建filehandler类的示例,并对其调用run(),以处理文件:

public class Main {
    public static void main(String[] args) {
        var applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        var fileHandler = applicationContext.getBean(XmlHandler.class);
        fileHandler.run();
    }
}

现在,如果我们在本地运行这个主方法,一切都正常。。。我们可以观察到这些文件正在被处理,有关这些文件的信息被存储到数据库中。然而,如果我们要创建应用程序的war包并将其部署在tomcat服务器上;将显示静态页面,但永远不会调用filehandler.run()。有人能解释一下这里发生了什么吗?我试图找到类似的问题,但我想以前没有人遇到过这种问题。

i34xakig

i34xakig1#

问题在于没有为应用程序定义DispatcherServlet。将其添加到web.xml后,应用程序在tomcat上运行良好:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:beans.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>

相关问题