spring - Thymeleaf 本地 @{} 网址不起作用
问题描述
我在 springboot 中使用 thymeleaf,我用本地 url 提到的所有链接都不起作用。我什至检查了浏览器上的网络选项卡,所有 css 和 js 资源都正确加载,但不知何故没有效果。这是一些代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="common-header">
<title>Fullstack app skeleton</title>
<!-- Bootstrap core CSS -->
<link th:href="@{/webjars/bootstrap/3.3.6/css/bootstrap.min.css}"
rel="stylesheet"></link>
<!-- Custom styles for this template -->
<link type="text/css" th:href="@{/css/styles.css}" rel="stylesheet">
</link>
</head>
<div th:fragment="before-body-scripts">
<script th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script>
<script th:src="@{/webjars/bootstrap/3.3.6/js/bootstrap.min.js}">
</script>
<script th:src="@{/js/fullstackapp.js}"></script>
</div>
</html>
另外,我检查了 pom.xml 并且所有依赖项都已正确加载。在上面的代码中,当我提到绝对 URL(https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css)时,所有引导功能都可以工作,但是当我使用对本地 jar 它没有。有人可以提出一个可能的原因吗???
解决方案
我正在使用一个.java
文件来映射Bootstrap
和Jquery
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/bootstrap/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/bootstrap/4.0.0/");
registry.addResourceHandler("/resources/jquery/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/jquery/3.0.0/");
}
}
这是我的html
代码:
<link th:href="@{/resources/bootstrap/css/bootstrap.min.css}" rel="stylesheet"/>
<script th:src="@{/resources/jquery/jquery.min.js}"></script>
<script th:src="@{/resources/bootstrap/js/bootstrap.js}"></script>
希望对你有所帮助
推荐阅读
- mime-types - 使用 Forge DataManagement API 在 BIM360 中创建新项目版本时出现 VER_MISMATCH_MIMETYPE 错误
- artificial-intelligence - 如何在类似国际象棋的游戏中实现极小极大算法?
- swiftui - SwiftUI:拖放和拐角半径
- togglz - 有没有办法从外部微服务读取功能切换的值?
- visual-studio-code - 如何阅读 Visual Studio 代码悬停在工具提示上?
- php - laravel 8 laravel-dompdf 0.9.0 找不到图像或类型未知
- java - Unity Ads 未显示,因为我正在获取 Unity Ads 由于缺少权限而无法获取当前网络类型
- git - 在推送到远程之前忘记创建新分支
- dictionary - 在 Julia 中找不到使用 sympy 生成的 dict 的密钥
- python - 上传 CSV 文件时,BigQuery 会将小数四舍五入为零