java - Spring Boot资源静态文件夹

标签 java spring-mvc spring-boot

不久,我的应用程序的结构如下,问题是 Spring Boot 不递归扫描静态文件夹。我是说 如果我将 bootstrap.min.css 放入 static/css/bootstrap.min.css没关系,没问题,但如果我放入静态文件夹 static/bootstrap/bootstrap.min.css 子包中的另一个文件夹中,就像屏幕截图中一样,我无法加载这些资源。我该如何处理这个问题?提前致谢。 enter image description here

最佳答案

您的网址缺少前导斜杠。

http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls

There are different types of URLs:

  • Absolute URLs: http://www.thymeleaf.org
  • Relative URLs, which can be:
    • Page-relative: user/login.html
    • Context-relative: /itemdetails?id=3 (context name in server will be added automatically)
    • Server-relative: ~/billing/processInvoice (allows calling URLs in another context (= application) in the same server.
    • Protocol-relative URLs: //code.jquery.com/jquery-2.0.3.min.js

屏幕截图中的 URL bootstrap/css/bootstrap.min.css 是相对于页面的。

您需要的是一个上下文相关的 URL。当您的文件位于 src/main/resources/static/css/bootstrap.min.css 中时,请使用:

th:href="@{/css/bootstrap.min.css}"

当它位于src/main/resources/static/bootstrap/bootstrap.min.css中时,使用:

th:href="@{/bootstrap/bootstrap.min.css}"

此外,我建议您使用Bootstrap WebJar,而不是手动下载bootstrap.min.css。然后使用以下href:

th:href="@{/webjars/bootstrap/3.3.6/css/bootstrap.min.css}"

关于java - Spring Boot资源静态文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41881232/

相关文章:

java - 通过反射调用支持 bean 方法 - JSF

java - 为什么 catch 语句在这里被忽略了?

java - 运行 GWT Map 时出错 - Maps key 是否丢失或无效?

spring - <task :annotation-driven> in spring 4. 3 的注释是什么

java - 上传到 Azure 时无法访问 RESTful 端点

java - Intellij spring boot PropertiesLauncher

java - 无法让 AppEngine 在 Eclipse 中工作

spring - 如何使用 Spring MVC 使用 REST URL?

spring-mvc - Sass (LibSass) 与 Spring MVC 和 JAVA

spring-boot - Apache 点燃: Possible too long JVM pause: 714 milliseconds