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...
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.
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:
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.
Thymeleaf Standard Layout System offers page fragment inclusion that is similar to JSP includes, with some important improvements over them.
Thank you Smajl, adding
<dependency>
<groupId> nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
solved the issue also for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With