Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf stopped resolving layout templates after migrating to Thymeleaf 3

I have a simple Spring Boot application in version 1.5.7 and I am trying to migrate it to version 2.0.0. I am almost done but there is one last piece missing and that is Thymeleaf.

Everything was working fine in the old version but after the migration, Spring Boot stopped resolving any templates (both pages and emails).

I have all templates src/main/resources/templates. Additionaly, I have layout called default in src/main/resources/templates/layout which looks like this:

<!DOCTYPE html>
<html>
<head th:replace="fragments/header :: head"></head>
<body>

    <div id="page">
        <nav class="navigation" th:replace="fragments/navigation"></nav>

        <div class="container">
            <div layout:fragment="container"></div>
        </div>
    </div>
</body>
</html>

My pages link to this layout like this:

<!DOCTYPE html>
<html lang="en"
  xmlns:th="http://www.thymeleaf.org"
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
  layout:decorate="layout/default">

<body>

    <div layout:fragment="container">
...
</body>
</html>

The navigation fragment in localted in src/main/resources/templeats/fragments

In Spring Boot 2, the login page is rendered but the layout is not applied (so the navigation bar and styles are missing). Did something change in version 2.0.0? I have not found any solution to this in officials docs or migration guide.

EDIT: As suggested in the answer, it is caused by migrating to Thymeleaf 3. I updated my question with some changes reflecting the migration guide but the code is still not injecting the layout.

I have tried layout:data-layout-decorate=~{layout/default} and layout:data-layout-decorate=~{layout/default.html} as well

I made it work with manually adding the thymeleaf dialect dependency:

compile('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect')

But I thought that this should be a transitive dependency already present in Spring and I should not have to add it manually...

like image 365
Smajl Avatar asked Mar 12 '18 09:03

Smajl


People also ask

Where can I find the thymeleaf project?

The project can be found on GitHub. In this article, we described many ways of achieving the same: layouts. You can build layouts using Thymeleaf Standard Layout System that is based on include-style approach.

Are my existing thymeleaf templates compatible with thyme leaf 3?

First, we have good news: Your existing Thymeleaf templates are almost 100% compatible with Thymeleaf 3 so you will only have to do a few modifications in your configuration. Let’s have a quick look at each of the important new concepts and features this new version brings:

Why can’t I see the fragments in a thymeleaf template?

When a Thymeleaf template is used as a static prototype, we cannot see the fragments we are including using the th:insert/th:replace host tags. We can only see the fragments aside, opening their own template documents.

What is thymeleaf standard layout system?

Thymeleaf Standard Layout System offers page fragment inclusion that is similar to JSP includes, with some important improvements over them.


1 Answers

Thank you Smajl, adding

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

solved the issue also for me.

like image 178
Jochen Marsaille Avatar answered Oct 13 '22 02:10

Jochen Marsaille