我在将 Spring Boot 应用程序作为 WAR 文件部署到独立的 Tomcat 7 服务器时遇到问题。它构建和部署得很好,但是当 index.html 页面尝试加载其他静态资源时,它们缺少 url 中的上下文,因此无法加载 (404)。
e.g. http://localhost:8080/app/images/springboot.png
应该:http://localhost:8080/spring-boot-war-context-issue/app/images/springboot.png
图片显示问题 https://i.stack.imgur.com/IcJfu.png
使用嵌入式Tomcat时效果很好
类似问题:
似乎是一个类似的问题:Spring-Boot 战争外部 Tomcat 上下文路径 https://stackoverflow.com/questions/29617277/spring-boot-war-external-tomcat-context-path
然而,该问题中的建议似乎并没有解决我的问题。我不确定 Tomcat xml 文件。
步骤如下:
我创建了一个简单的示例应用程序,并按照 Spring Boot 文档中的步骤进行操作。
可以在此 github 存储库中查看示例代码以及重现问题的步骤:https://github.com/jgraham0325/spring-boot-war-context-issue https://github.com/jgraham0325/spring-boot-war-context-issue
到目前为止我尝试过的事情:
- 在 application.properties 中设置 contextPath 但这仅适用于嵌入式 tomcat
- 尝试使用全新安装的 Tomcat 7
- 尝试在 tomcat 中创建一个配置文件来强制上下文:
apache-tomcat-7.0.72\conf\Catalina\localhost\spring-boot-war-context-issue.xml
spring-boot-war-context-issue.xml 的内容:
<Context
docBase="spring-boot-war-context-issue"
path="spring-boot-war-context-issue"
reloadable="true"
/>
任何建议将不胜感激!
Thanks
2016 年 10 月 23 日更新:
下面 Alex 关于使用开头不带斜杠的相对 URL 的回答是完美的解决方案!