有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法在tomcat中加载静态文件

首先,我通过添加以下插件在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文件,但运行时无法加载静态文件。 enter image description here

我认为这是由于上下文路径:

  • 在tomcat中运行时:http://localhost:8080/report_system-1.0/
  • 运行jar文件时:http://localhost:8080

但我不知道怎么解决这个问题


共 (1) 个答案

  1. # 1 楼答案

    在为不同的环境构建应用程序时,需要设置^{}。差不多

    module.exports = {
      publicPath: process.env.NODE_ENV === 'production'
        ? '/report_system-1.0/'
        : '/'
    }