Thymeleaf's 3 fragment insert not working

Viewed 3020

I'd like to use Thymeleaf as a template engine in my Java web application. I'd also like to build front-end using Bootstrap 4. I'm using Intellij IDEA 2017.2.5 Ultimate, Spring Boot 1.5.7, Tomcat 8.5.13, Java 8, Thymeleaf 3.0.7 and Gradle. I created a simple controller:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {
    @RequestMapping("/")
    public String homeScreen()
    {
        return "index";
    }
}

and two HTML files: base and index respectively:

<!DOCTYPE html>
<html lang="pl_PL" xmlns:th="http://www.thymeleaf.org">
    <head th:fragment="head">
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
        <title>Title</title>
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css"
              integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous"/>
    </head>
    <body>

        <header th:fragment="header">
            <div class="btn-group btn-group-lg" role="group">
                <button type="button" class="btn btn-secondary">Home</button>
                <button type="button" class="btn btn-secondary">Invoices</button>
                <button type="button" class="btn btn-secondary">Customers</button>
                <button type="button" class="btn btn-secondary">Products</button>
                <button type="button" class="btn btn-secondary">Warehouse</button>
            </div>
        </header>

        <div th:fragment="bootstrap-js-jquery-links">
            <!-- Optional JavaScript -->
            <!-- jQuery first, then Popper.js, then Bootstrap JS -->
            <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
                    integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"
                    integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"
                    integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
        </div>
    </body>
</html>

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head th:insert="base :: head"></head>
    <body>
        <header th:insert="base :: header"></header>
        <div th:insert="base :: bootstrap-js-jquery-links"></div>
    </body>
</html>

When I run this application everything builds and deploys correctly, but I can't see anything at my index page, like th:insert wasn't working. When I change return "index" to return "base" in the controller this is what I'm getting (and what I want to get with index as return string):

enter image description here

According to the Thymeleaf's documentation I feel that I wrote things correctly, so where am I making a mistake? My build.gradle:

buildscript {
    ext {
        springBootVersion = '1.5.7.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

group = 'com.invoices-web'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa'){
        exclude group: 'org.hibernate', name:'hibernate-core'
    }
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.postgresql:postgresql')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.11.Final'
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'
    compile group: 'org.thymeleaf', name: 'thymeleaf-spring4', version: '3.0.7.RELEASE'
    compile group: 'com.ibm.icu', name: 'icu4j', version: '59.1'
    compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.7'
    compile group: 'com.google.guava', name: 'guava', version: '23.0'

}

My project's structure:
enter image description here

1 Answers

You can try this :

index.html :

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head th:replace="base :: head"></head>
    <body>
        <header th:replace="base :: header"></header>
        <div th:replace="base :: bootstrap-js-jquery-links"></div>
    </body>
</html>
Related