spring-boot Spring boot + Hibernate + Web UI (Thymeleaf) Thymeleaf Resources and Spring Controller


In order to expose Thymeleaf templates we need to define controllers.


public class MessageController {
    private MessageRepository messageRepository;
    public ModelAndView index() {
        Iterable<Message> messages = messageRepository.findAll();
        return new ModelAndView("index", "index", messages);

This simple controller injects MessageRepository and pass all messages to a template file named index.html, residing in src/main/resources/templates, and finally expose it on /index.

In the same way, we can place other templates in the templates folder (default by spring to src/main/resources/templates), pass a model to them and serve them to the client.

Other static resources should be placed in one of the following folders, exposed by default in spring boot:


Thymeleaf index.html example:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head th:fragment="head (title)">
        <title th:text="${title}">Index</title>
        <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" href="../../css/bootstrap.min.css" />
        <nav class="navbar navbar-default navbar-fixed-top">
          <div class="container-fluid">
            <div class="navbar-header">
              <a class="navbar-brand" href="#">Thymeleaf</a>
        <div class="container">
            <ul class="nav">
                <li><a th:href="@{/}" href="messages.html"> Messages </a></li>
  • bootstrap.min.css is in src/main/resources/static/css folder. you can use the syntax @{} to get other static resources using relative path.