Spring boot WAR 部署到 Tomcat 并且缺少静态资源的上下文 [英] Spring boot WAR deployed to Tomcat and missing context for static resources
问题描述
我在将 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
使用 JSP/JSTL:
<img src="${pageContext.request.contextPath}/app/images/springboot.png"/>
或使用 Javascript:
function getContextPath() {return window.location.pathname.substring(0, window.location.pathname.indexOf("/",2));}...var img = new Image();img.src = getContextPath() + "/app/images/springboot.png";document.getElementById('div').appendChild(img);
I'm having a problem when deploying a Spring Boot application as a WAR file to a standalone Tomcat 7 server. It builds and deploys fine but when the index.html page tries to load other static resources they are missing the context in the url so fail to load (404).
e.g. http://localhost:8080/app/images/springboot.png
should be: http://localhost:8080/spring-boot-war-context-issue/app/images/springboot.png
It works fine when using the embedded Tomcat
Similar issues:
Seems to be a similar issue to: Spring-Boot war external Tomcat context path
However the suggestions in that question did not seem to solve my issue. I wasn't sure about the Tomcat xml file.
Steps followed:
I created a simple sample application and followed the steps in the Spring Boot docs.
The sample code can be seen in this github repo along with steps to reproduce the problem: https://github.com/jgraham0325/spring-boot-war-context-issue
Things I've tried so far:
- Set contextPath in application.properties but this only applies to embedded tomcat
- Tried using a fresh install of Tomcat 7
- Tried creating a config file in tomcat to force context: apache-tomcat-7.0.72\conf\Catalina\localhost\spring-boot-war-context-issue.xml
Contents of spring-boot-war-context-issue.xml:
<Context
docBase="spring-boot-war-context-issue"
path="spring-boot-war-context-issue"
reloadable="true"
/>
Any advice would be much appreciated!
Thanks
Update 23/10/2016:
Alex's answer below about using relative URLs without the slash at the start was perfect solution!
Isn't it simply caused by the way you defined your url in index.html
(the url does not include the context root
):
<img src="/app/images/springboot.png" />
Use relative uri
You should be able to use a relative uri (without the leading forward slash):
<img src="app/images/springboot.png" />
get the context root
How do you get the contextPath from JavaScript, the right way?
How to use relative paths without including the context root name?
With JSP/JSTL:
<img src="${pageContext.request.contextPath}/app/images/springboot.png" />
Or with Javascript:
function getContextPath() {
return window.location.pathname.substring(0, window.location.pathname.indexOf("/",2));
}
...
var img = new Image();
img.src = getContextPath() + "/app/images/springboot.png";
document.getElementById('div').appendChild(img);
这篇关于Spring boot WAR 部署到 Tomcat 并且缺少静态资源的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!