Search code examples
javaspring-bootthymeleaf

Why doesn't inserting a fragment in index.html


The app is based on Spring-Boot + thymeleaf. The file starts first index.html, which is located in the static folder. All other templates are located in the templates folder. I was thinking of doing index.html the main page of the site. But I'm trying to insert a fragment of the header there using thymeleaf, and nothing works, i.e. this fragment is not on this page.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Home page</title>
</head>
<body>
    <div th:insert="navbar :: f-navbar"></div>
    <p>Go: <a href="/setting">Настройки</a></p> 
</body>
</html>

And if I go to the setting page, where exactly the same fragment is displayed perfectly. What am I doing wrong? Or is it not allowed to do this in this file? Then why do you need it?


Solution

  • The static folder is served by Spring Boot as is. There is no Thymeleaf processing going on there. Move your index.html inside the templates folder as well.

    If that is not enough to make it work, you can add a simple RootController:

    public class RootController {
      @GetMapping("/")
      public String homePage() {
        return "index"
      }
    }