【问题标题】:Thymeleaf local @{} urls not workingThymeleaf 本地 @{} 网址不起作用
【发布时间】:2018-07-13 17:11:54
【问题描述】:

我在 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 的引用时,它不会。有人可以提出一个可能的原因吗???

【问题讨论】:

  • 你能发布你的控制器代码来获取这个模板和你的pom.xml吗?
  • 嘿,谢谢你指出,结果不知何故,请求映射中的 url 被删除了..现在一切都解决了......我的错。

标签: spring spring-boot thymeleaf


【解决方案1】:

我正在使用一个.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>

希望对你有所帮助

【讨论】:

    猜你喜欢
    • 2013-02-27
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 2015-03-26
    • 1970-01-01
    相关资源
    最近更新 更多