【发布时间】:2017-03-05 08:34:20
【问题描述】:
在将 Spring Boot 应用程序作为 WAR 文件部署到独立的 Tomcat 7 服务器时遇到问题。它可以正常构建和部署,但是当 index.html 页面尝试加载其他静态资源时,它们会丢失 url 中的上下文,因此无法加载 (404)。
例如http://localhost:8080/app/images/springboot.png
应该是:http://localhost:8080/spring-boot-war-context-issue/app/images/springboot.png
使用嵌入式Tomcat时效果很好
类似问题:
似乎与以下问题类似: Spring-Boot war external Tomcat context path
但是,该问题中的建议似乎并未解决我的问题。我不确定 Tomcat xml 文件。
后续步骤:
我创建了一个简单的示例应用程序并按照 Spring Boot 文档中的步骤进行操作。
可以在此 github 存储库中查看示例代码以及重现问题的步骤: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"
/>
任何建议将不胜感激!
谢谢
2016 年 10 月 23 日更新:
Alex 关于在开头使用不带斜线的相对 URL 的回答是完美的解决方案!
【问题讨论】:
-
websphere 8.5 服务器仍然存在这个问题
标签: java spring tomcat spring-boot