Last updated: 2024-05-24

  1. Learn
  2. Frontend
  3. Thymeleaf Bootstrap

Spring Boot with Thymeleaf and Bootstrap

Thymeleaf and Bootstrap are powerful technologies to develop modern web applications. In this article, we will show the steps to add Thymeleaf and Bootstrap to our Spring Boot application.

The complete setup can also be created directly in the Bootify Builder. Just open a project with start project, choose your preferences and select Thymeleaf + Bootstrap as frontend stack. If you don't select a frontend, all the following steps can be followed manually.

Adding Themeleaf to our app

Thymeleaf is the most used template engine for Spring Boot. Since the templates are written in valid HTML code, they can be opened without server-side rendering as well. To setup the engine and all recommended utilities, the following two dependencies are added to our application first.

Adding new dependencies for Thymeleaf

The Spring Boot library spring-boot-starter-thymeleaf will automatically integrate the template engine - in the following controller we can already see how this works. The thymeleaf-layout-dialect is recommended for most applications, in order that several pages can use the same layout. We don't need to provide any version here, as they are part of the dependency management plugin of Spring Boot.

A simple MVC controller

Our simple endpoint just shows a list of todos. For this all current todos are loaded from the service first and added to the model. With "todo/list" the template from src/main/resources/templates/todo/list.html is picked up. Unless we configure it otherwise, the template folder is the default one from which the templates are loaded.

A simple Thymeleaf Template

After adding our list.html template, we are already using Thymeleaf and Bootstrap classes successfully - the only missing layout will be shown in a moment. To enable real hot reloading during development, it is not enough to switch off template caching. How to read the HTML files directly from the file system is described in this article.

Bootstrap integration using WebJars

Bootstrap is currently the most popular CSS framework and can be integrated directly into our Spring Boot app using WebJars. For this we need another dependency first.

We can now include the JavaScript and CSS files for Bootstrap in our layout.html as follows. Our own JS or CSS can be provided additionally in src/main/resources/static/js and src/main/resources/static/css if needed.

Integration of Bootstrap files in their minimized versions

Bootstrap integration using Webpack

Alternatively, Bootstrap can also be integrated using Webpack to define more complex rules for how CSS and JS should be provided to the frontend. If Webpack is already prepared, only one command is needed to add Bootstrap.

However, since the Webpack setup with npm and DevServer requires a lot of explanation, please check the article about the setup of Spring Boot with Webpack. Using Webpack offers many advantages - but also comes with the cost of a higher integration effort.

In the Free plan of Bootify, Spring Boot application including a frontend stack can be initialized - directly in the browser, without registration. Thymeleaf with Bootstrap is available there, either directly via WebJars or via Webpack.

Start Project
No registration required

Further readings

Thymeleaf documentation
Bootstrap