Search code examples
javathymeleaf

Thymeleaf fragments don't render


At this point I've checked the syntaxis of the statements - they look correct. Changed the structure of the program - no effect. Even different dependencies don't work. After one and half days it's time to look for help. This is my index.html

<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">  
<head th:replace="fragments/layout :: nameOfFrag">
<meta charset="UTF-8">
<title>from index.html</title>
</head>
<body>
    <form action="printName" method="get">
        <label> Enter your name</label>
        <input type="text" name="name"  id="name">
    </form>
    <div th:include="fragments/layout :: body"></div>
</body>
</html>

This is the layout:

<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">  
<head th:fragment="nameOfFrag">
<meta charset="UTF-8">
<title>this is from layout</title>
</head>
<body>
    <div th:fragment="body">
    <p>This is the body from layout</p>
    </div>
</body>
</html>

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>learning_spring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>learning_spring</name>
    <description>simple_web_dataBase_app</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        
    <dependency>  
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.3.3.RELEASE</version>
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Here's the structure

When I inspect the html in browser, everything seems fine, yet nothing from layout is visible inside index.html


Solution

  • I managed to fix the issue. It seems like the "static" folder does what is say - serves a "static" content, meaning it cannot resolve any thymeleaf templates. Moving index.html into fragments folder resolves the issue. One more thing - in order to access the index file, one has to "call"(return) it via controller method.