首页 > 解决方案 > 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 它没有。有人可以提出一个可能的原因吗???

标签: springspring-bootthymeleaf

解决方案


我正在使用一个.java文件来映射BootstrapJquery

@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>

希望对你有所帮助


推荐阅读