html - Thymeleaf 加载资源失败

标签 html thymeleaf

您好,我正在尝试使用 thymeleaf 呈现我的 html 页面,但它无法加载 .js 文件。我正在使用 spring-boot,我的文件结构是 src/main/resources/templates 这包含我所有的 html 文件。我的 css 文件位于 src/main/resources/static/css/。我的 .js 文件位于 src/main/resources/js/

现在我尝试渲染我的 guest.html 文件,但它无法加载资源,因为我尝试检查 chrome 上的元素并显示以下错误消息:

http://localhost:8080/js/bootstrap-formhelpers.min.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/js/bootstrap.min.js Failed to load resource: the server responded with a status of 404 (Not Found)

以下是我的 guest.html 文件中无法定位资源的问题:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" th:src="@{https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js}" ></script>
<script src="../js/bootstrap.min.js" th:src="@{js/bootstrap.min.js}"></script>
<script src="../js/bootstrap-formhelpers.min.js" th:src="@{js/bootstrap-formhelpers.min.js}"></script>

最佳答案

默认情况下,Spring Boot 不提供来自 src/main/resources 的静态内容。它提供来自以下classpath 位置的内容:

  • classpath:/META-INF/resources
  • classpath:/static
  • classpath:/resources
  • classpath:/public

因此,提供 src/main/resources/static/css,但不提供 src/main/resources/js/

js 目录移动到 src/main/resources/static 目录以提供它。另一种选择是添加资源处理程序:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/js/**")
            .addResourceLocations("classpath:/js/");
}

您还可以覆盖 spring.resources.staticLocations 属性。

有关使用 Spring Boot 提供静态内容的更多信息,请参阅 Spring Boot Documentation: Static Content .

关于html - Thymeleaf 加载资源失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33908861/

相关文章:

java - 在 Spring-boot 中提交表单后,所需的请求部分 'file' 不存在

java - 无法在特定端点使用 thymeleaf 访问静态资源

java - Thymeleaf 应用程序在生产中陷入僵局,卡在 Thymeleaf 代码中

java - 如何截断 thymeleaf 中的十进制数字?

javascript - 从 JavaScript 对象中随机选择值

javascript - 如何修复canvasjs中的气泡大小(标记大小)?

javascript - 如何改变小图片相对于大图片的位置?

jquery - 从一个 XML XSLT 到 HTML 制作两列

php - 是否有更优雅的方法来检查表单默认值?

spring - thymeleaf : How to edit a field?