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

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

Cannot load static file in tomcat

问题

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

  1. <plugin>
  2. <groupId>com.github.eirslett</groupId>
  3. <artifactId>frontend-maven-plugin</artifactId>
  4. <version>${frontend-maven-plugin.version}</version>
  5. <configuration>
  6. <workingDirectory>src/main/frontend</workingDirectory>
  7. </configuration>
  8. <executions>
  9. <execution>
  10. <id>install node and yarn</id>
  11. <goals>
  12. <goal>install-node-and-yarn</goal>
  13. </goals>
  14. <configuration>
  15. <nodeVersion>${nodejs.version}</nodeVersion>
  16. <yarnVersion>v1.22.4</yarnVersion>
  17. </configuration>
  18. </execution>
  19. <execution>
  20. <id>yarn install</id>
  21. <goals>
  22. <goal>yarn</goal>
  23. </goals>
  24. <phase>generate-resources</phase>
  25. <configuration>
  26. <arguments>install</arguments>
  27. </configuration>
  28. </execution>
  29. <execution>
  30. <id>yarn build</id>
  31. <goals>
  32. <goal>yarn</goal>
  33. </goals>
  34. <configuration>
  35. <arguments>build</arguments>
  36. </configuration>
  37. </execution>
  38. </executions>
  39. </plugin>
  40. <plugin>
  41. <artifactId>maven-resources-plugin</artifactId>
  42. <executions>
  43. <execution>
  44. <id>copy-frontend</id>
  45. <phase>generate-resources</phase>
  46. <goals>
  47. <goal>copy-resources</goal>
  48. </goals>
  49. <configuration>
  50. <outputDirectory>target/classes/resources/</outputDirectory>
  51. <overwrite>true</overwrite>
  52. <resources>
  53. <resource>
  54. <directory>src/main/frontend/dist</directory>
  55. <includes>
  56. <include>static/</include>
  57. <include>index.html</include>
  58. <include>favicon.ico</include>
  59. </includes>
  60. </resource>
  61. </resources>
  62. </configuration>
  63. </execution>
  64. </executions>
  65. </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:

  1. &lt;plugin&gt;
  2. &lt;groupId&gt;com.github.eirslett&lt;/groupId&gt;
  3. &lt;artifactId&gt;frontend-maven-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;${frontend-maven-plugin.version}&lt;/version&gt;
  5. &lt;configuration&gt;
  6. &lt;workingDirectory&gt;src/main/frontend&lt;/workingDirectory&gt;
  7. &lt;/configuration&gt;
  8. &lt;executions&gt;
  9. &lt;execution&gt;
  10. &lt;id&gt;install node and yarn&lt;/id&gt;
  11. &lt;goals&gt;
  12. &lt;goal&gt;install-node-and-yarn&lt;/goal&gt;
  13. &lt;/goals&gt;
  14. &lt;configuration&gt;
  15. &lt;nodeVersion&gt;${nodejs.version}&lt;/nodeVersion&gt;
  16. &lt;yarnVersion&gt;v1.22.4&lt;/yarnVersion&gt;
  17. &lt;/configuration&gt;
  18. &lt;/execution&gt;
  19. &lt;execution&gt;
  20. &lt;id&gt;yarn install&lt;/id&gt;
  21. &lt;goals&gt;
  22. &lt;goal&gt;yarn&lt;/goal&gt;
  23. &lt;/goals&gt;
  24. &lt;phase&gt;generate-resources&lt;/phase&gt;
  25. &lt;configuration&gt;
  26. &lt;arguments&gt;install&lt;/arguments&gt;
  27. &lt;/configuration&gt;
  28. &lt;/execution&gt;
  29. &lt;execution&gt;
  30. &lt;id&gt;yarn build&lt;/id&gt;
  31. &lt;goals&gt;
  32. &lt;goal&gt;yarn&lt;/goal&gt;
  33. &lt;/goals&gt;
  34. &lt;configuration&gt;
  35. &lt;arguments&gt;build&lt;/arguments&gt;
  36. &lt;/configuration&gt;
  37. &lt;/execution&gt;
  38. &lt;/executions&gt;
  39. &lt;/plugin&gt;
  40. &lt;plugin&gt;
  41. &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
  42. &lt;executions&gt;
  43. &lt;execution&gt;
  44. &lt;id&gt;copy-frontend&lt;/id&gt;
  45. &lt;phase&gt;generate-resources&lt;/phase&gt;
  46. &lt;goals&gt;
  47. &lt;goal&gt;copy-resources&lt;/goal&gt;
  48. &lt;/goals&gt;
  49. &lt;configuration&gt;
  50. &lt;outputDirectory&gt;target/classes/resources/&lt;/outputDirectory&gt;
  51. &lt;overwrite&gt;true&lt;/overwrite&gt;
  52. &lt;resources&gt;
  53. &lt;resource&gt;
  54. &lt;directory&gt;src/main/frontend/dist&lt;/directory&gt;
  55. &lt;includes&gt;
  56. &lt;include&gt;static/&lt;/include&gt;
  57. &lt;include&gt;index.html&lt;/include&gt;
  58. &lt;include&gt;favicon.ico&lt;/include&gt;
  59. &lt;/includes&gt;
  60. &lt;/resource&gt;
  61. &lt;/resources&gt;
  62. &lt;/configuration&gt;
  63. &lt;/execution&gt;
  64. &lt;/executions&gt;
  65. &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。类似于以下内容:

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

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

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

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:

确定