Miha Jamsek
Miha Jamsek

Reputation: 1273

Thymeleaf's layout:fragment not working after packaged in jar

So, I am writing a Spring boot app with Thymeleaf as my template engine. When I run my project through Spring Tool Suite, everything works as it should.

However, when i run mvn clean package and run jar that is generated, my layout:fragment stops working, saying it cannot resolve it:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "Layout", template might not exist or might no t be accessible by any of the configured Template Resolvers (index)

My layout.html:

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

    <head>
        <title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE" >my Site</title>
        <meta charset="UTF-8"/>
    </head>
    <body onload="init()">
        <header>
            <nav class="navbar navbar-inverse">
                <!-- Navigation bar -->
            </nav>
        </header>
        <section layout:fragment="vsebina" style="padding: 25px;"></section>
    </body>
</html>

and my index.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
        xmlns:th="http://www.thymeleaf.org" 
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
        layout:decorate="~{layout}"
        layout:decorator="Layout">

    <head>
        <title>Index</title>
    </head>
    <body>
        <section layout:fragment="vsebina">
            ...
            Content of my index page
            ...
        </section>
    </body>
</html>

and my 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mypackage</groupId>
    <artifactId>mySite</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>mySite</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sourceforge.nekohtml/nekohtml -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.21</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
    </dependencies>

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


</project>

It seems odd that in one case it works and in another it doesn't. Or am I doing something wrong?

Upvotes: 0

Views: 2057

Answers (1)

Miha Jamsek
Miha Jamsek

Reputation: 1273

Apparently the problem was in html tag:

<html xmlns="http://www.w3.org/1999/xhtml" 
        xmlns:th="http://www.thymeleaf.org" 
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
        layout:decorate="~{layout}"
        layout:decorator="Layout">

This was needed to change into this:

<html xmlns="http://www.w3.org/1999/xhtml" 
        xmlns:th="http://www.thymeleaf.org" 
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
        layout:decorate="~{layout}"
        layout:decorator="layout">

layout:decorator="Layout" to layout:decorator="layout"

In Spring Tool Suite the capital L didn't caused problems, but when the app was packaged into jar, it started to cause problems.

Upvotes: 1

Related Questions