Thymeleaf + Spring Boot: placing html in subfolders

Viewed 4817

By default, Thymeleaf needs all the html files to be present in scr/main/java/resources/templates. In order not to create a mess in templates folder I need to create different subloders there. The problem is that html files placed there never get resolved. Example is below.

Structure:

project structure

IndexController:

@Controller
public class IndexController {
    @GetMapping(value = "/")
    public String index() {
        return "index";
    }
}

default template:

<!DOCTYPE html>
<html lang="en"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <meta charset="UTF-8"/>
    <title>Default template</title>
</head>
<body>

<section id="site-content" layout:fragment="content"></section>

</body>
</html>

index:

<!DOCTYPE html>
<html lang="en"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="default">
<head>
    <meta charset="UTF-8"/>
    <title>Index</title>
</head>
<body>

<div layout:fragment="content">
    <div>
        This is index page
    </div>

    <a href="subfolder/page.html">Page</a>
</div>

</body>
</html>

page in the subfolder:

<!DOCTYPE html>
<html lang="en"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="default">
<head>
    <meta charset="UTF-8">
    <title>Page</title>
</head>
<body>

<div layout:fragment="content">
    This is a page in the subfolder
</div>

</body>
</html>

When I open index page and click the link to the page.html, I get this:

error

What I did wrong?

1 Answers
Related