无法在tomcat中加载静态文件
Posted
技术标签:
【中文标题】无法在tomcat中加载静态文件【英文标题】:Cannot load static file in tomcat 【发布时间】:2020-12-21 04:55:59 【问题描述】:首先,我通过添加以下插件在springboot中构建vuejs项目:
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>$frontend-maven-plugin.version</version>
<configuration>
<workingDirectory>src/main/frontend</workingDirectory>
</configuration>
<executions>
<execution>
<id>install node and yarn</id>
<goals>
<goal>install-node-and-yarn</goal>
</goals>
<configuration>
<nodeVersion>$nodejs.version</nodeVersion>
<yarnVersion>v1.22.4</yarnVersion>
</configuration>
</execution>
<execution>
<id>yarn install</id>
<goals>
<goal>yarn</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>yarn build</id>
<goals>
<goal>yarn</goal>
</goals>
<configuration>
<arguments>build</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-frontend</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/classes/resources/</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>src/main/frontend/dist</directory>
<includes>
<include>static/</include>
<include>index.html</include>
<include>favicon.ico</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
我已经成功构建并在构建到 jar 文件时运行它。 但是现在我想用tomcat运行它,所以我将构建格式更改为war文件,但是在运行时它无法加载静态文件。
我认为这是由于上下文路径:
在tomcat中运行时:http://localhost:8080/report_system-1.0/ 运行jar文件时:http://localhost:8080但我不知道如何解决这个问题。
【问题讨论】:
【参考方案1】:在为不同环境构建应用程序时,您需要设置publicPath
。类似的东西
module.exports =
publicPath: process.env.NODE_ENV === 'production'
? '/report_system-1.0/'
: '/'
【讨论】:
以上是关于无法在tomcat中加载静态文件的主要内容,如果未能解决你的问题,请参考以下文章