在我的 java 11、基于 tomcat 和 maven 的 servlet 应用程序中,我无法访问客户端的 Web 资源。比如css文件、图片无法加载,xhtml文件也无法加载。
在服务器端,它们是可以访问的。
我尝试在 Maven 插件的配置中手动添加这些资源。
<?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.pr</groupId>
<artifactId>project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<flyway.user>sa</flyway.user>
<flyway.password>password</flyway.password>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp/WEB-INF/templates</directory>
<targetPath>WEB-INF</targetPath>
<includes>
<include>**/*.css</include>
<include>**/*.png</include>
<include>**/*.xhtml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>7.15.0</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
<version>10.11.0</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>8.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.5</version>
</dependency>
</dependencies>
</project>
但是没有效果。
我的 Dockerfile:
FROM maven:3-eclipse-temurin-11 AS build
WORKDIR /deploy
COPY . .
RUN mvn package -DskipTests
FROM tomcat:8.5
COPY --from=build /deploy/target/amazonlite-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/amazonlite.war
EXPOSE 8080
ENTRYPOINT ["catalina.sh", "run"]
资源在我的 docker 容器的 /usr/local/tomcat/webapps/project 目录中可用。
经过几个小时的工作,我找到了解决方案。
在我的 web.xml 中,我确实需要指定我希望通过我的网络服务器访问的资源(css/等)。
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/css/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/jpg/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/html/*</url-pattern>
</servlet-mapping>
在 webapp-dir 中,我确实需要应用这些目录结构,我在 web.xml 中指定为可访问资源。