When using thymeleaf-layout-dialect in Spring Boot applications compiled to GraalVM native images, builds fail due to Groovy compatibility issues. A working alternative is Thymeleaf's built-in flexible layouts using fragment expressions. The approach involves defining a layout template with th:fragment parameters (title, content, pageScripts) and referencing it from page templates using th:replace with fragment expressions like ~{::title/text()} and ~{::#content}. Full code examples for layout template, page templates, and Spring MVC controller are provided, along with Maven/Gradle commands to build and run the native image.
Table of contents
Create a Spring Boot ApplicationCreate Layout TemplateCreate a Page TemplateCreate Another Page TemplateCreate controller handler methodsRun the ApplicationConclusionSort: