gpt4 book ai didi

html - Thymeleaf 加载资源失败

转载 作者:搜寻专家 更新时间:2023-10-31 22:59:37 27 4
gpt4 key购买 nike

您好,我正在尝试使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com