首页 > 解决方案 > Thymeleaf 布局装饰器不起作用

问题描述

嗨,伙计们,我正在学习百里香,我不明白为什么它不起作用我读了很多例子,我做对了,但它不起作用。有人可以告诉我我在这里缺少什么吗?

这是我的布局:

<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

<link href="webjars/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<th:block layout:fragment="styles">

<nav class="navbar navbar-light bg-light">
    <span class="navbar-brand mb-0 h1">Decorator Sample</span>
</nav>

<div class="container">
    <div class="content">
        <div layout:fragment="page_content"></div>
    </div>
</div>

<script src="webjars/jquery/3.0.0/jquery.min.js"></script>
<script src="webjars/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<th:block layout:fragment="scripts">

这是我的索引:

<html xmlns:th="http://www.thymeleaf.org" 
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" 
  layout:decorate="~{_layout}">
<body>
    <div layout:fragment="page_content">

    </div>
</body>

标签: spring-mvcthymeleaf

解决方案


我们需要将 Thymeleaf 入门包添加到您的 Spring Boot pom 中:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

但是,从 Spring Boot 2 开始,这已经不够了。布局方言不是启动器的一部分,我们需要自己添加它:

<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>    
</dependency>

代码示例还使用 Bootstrap,因此还需要添加 webjar:

<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>

推荐阅读