静态内容无法从在JBoss服务器上部署的Spring Boot WAR加载。

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

Static content not loading from spring boot war in jboss server

问题

我已将静态内容,如CSS、JS和图像,放置在资源文件夹下的静态文件夹中,而将所有的HTML文件放置在模板文件夹中。然而,当我构建WAR文件并在JBoss上部署时,HTML被加载了,但静态内容没有被加载,在内置的Tomcat服务器中却能正常工作。

访问WAR文件页面的URL:IP:8080/warname/login
当我在Chrome的控制台中检查时,我得到JS、CSS的URL,如 IP:8080/js/jsfile.js 或者 IP:8080/css/abc.css

这是我的项目结构:
项目结构图片

这是我在HTML文件中如何使用CSS和JS的方式

英文:

i have placed the static content such as css, js , images in static folder under resources and all the html files in templates folder. however when i build a war and deploy it on jboss. the html is loaded but static content is not loaded and the same works fine in inbuild tomcat server.

url to access the war pages : IP:8080/warname/login
when i inspect in console of chrome i get url of js, css as IP:8080/js/jsfile.js or IP:8080/css/abc.css.

here is my project structure
image of project structure

Here is how i am using css,js in html files

答案1

得分: 0

你正在引用 src 或 href 中的静态内容,例如 /js,但是在部署为 WAR 文件时,这样做是行不通的,因为在应用服务器中会添加上下文根(在你的情况中是 warname)。

你需要使用 Thymeleaf 语法,像这样:

<script type="text/javascript" th:src="@{/js/searchOP.js}"></script>

请查阅文档获取更多信息:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

英文:

You are referencing the static content in src or href with for example /js but this does not work when deploying as WAR file because in the app server there will be a context root added. (warname in your case)

You have to use Thymleaf syntax like

&lt;script type=&quot;text/javascript&quot; th:src=&quot;@{/js/searchOP.js}&quot;&gt;&lt;/script&gt;

Please checkout the documentation for more information: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

huangapple
  • 本文由 发表于 2020年5月5日 20:25:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61613093.html
匿名

发表评论

匿名网友

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

确定