无法在Tomcat中加载静态文件。

huangapple go评论79阅读模式
英文:

Cannot load static file in tomcat

问题

首先,我通过添加以下插件在Spring Boot中构建了Vue.js项目:

<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

但我不知道如何修复这个问题。

英文:

Firstly, I build the vuejs project in springboot by adding the following plugin:

          &lt;plugin&gt;
&lt;groupId&gt;com.github.eirslett&lt;/groupId&gt;
&lt;artifactId&gt;frontend-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;${frontend-maven-plugin.version}&lt;/version&gt;
&lt;configuration&gt;
&lt;workingDirectory&gt;src/main/frontend&lt;/workingDirectory&gt;
&lt;/configuration&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;id&gt;install node and yarn&lt;/id&gt;
&lt;goals&gt;
&lt;goal&gt;install-node-and-yarn&lt;/goal&gt;
&lt;/goals&gt;
&lt;configuration&gt;
&lt;nodeVersion&gt;${nodejs.version}&lt;/nodeVersion&gt;
&lt;yarnVersion&gt;v1.22.4&lt;/yarnVersion&gt;
&lt;/configuration&gt;
&lt;/execution&gt;
&lt;execution&gt;
&lt;id&gt;yarn install&lt;/id&gt;
&lt;goals&gt;
&lt;goal&gt;yarn&lt;/goal&gt;
&lt;/goals&gt;
&lt;phase&gt;generate-resources&lt;/phase&gt;
&lt;configuration&gt;
&lt;arguments&gt;install&lt;/arguments&gt;
&lt;/configuration&gt;
&lt;/execution&gt;
&lt;execution&gt;
&lt;id&gt;yarn build&lt;/id&gt;
&lt;goals&gt;
&lt;goal&gt;yarn&lt;/goal&gt;
&lt;/goals&gt;
&lt;configuration&gt;
&lt;arguments&gt;build&lt;/arguments&gt;
&lt;/configuration&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;id&gt;copy-frontend&lt;/id&gt;
&lt;phase&gt;generate-resources&lt;/phase&gt;
&lt;goals&gt;
&lt;goal&gt;copy-resources&lt;/goal&gt;
&lt;/goals&gt;
&lt;configuration&gt;
&lt;outputDirectory&gt;target/classes/resources/&lt;/outputDirectory&gt;
&lt;overwrite&gt;true&lt;/overwrite&gt;
&lt;resources&gt;
&lt;resource&gt;
&lt;directory&gt;src/main/frontend/dist&lt;/directory&gt;
&lt;includes&gt;
&lt;include&gt;static/&lt;/include&gt;
&lt;include&gt;index.html&lt;/include&gt;
&lt;include&gt;favicon.ico&lt;/include&gt;
&lt;/includes&gt;
&lt;/resource&gt;
&lt;/resources&gt;
&lt;/configuration&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;

I have successfully built and run it when building into a jar file.
But now I want to run it with tomcat so I change the build format to war file but when running it can't load static file.
无法在Tomcat中加载静态文件。

I think it's due to context path:

  • When run in tomcat: http://localhost:8080/report_system-1.0/
  • When run jar file: http://localhost:8080

But I don't know how to fix that.

答案1

得分: 2

你需要在为不同环境构建应用程序时设置 publicPath。类似于以下内容:

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

You need to set publicPath when building the app for different environments. Something like

module.exports = {
publicPath: process.env.NODE_ENV === &#39;production&#39;
? &#39;/report_system-1.0/&#39;
: &#39;/&#39;
}

huangapple
  • 本文由 发表于 2020年9月2日 11:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63698461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定