Layout with thymeleaf on Spring Boot

When designing a website with Spring Boot, I want to show a simple layout structure.

Dependencies that we should add while creating our project;

- Web
- Thymeleaf

Layout structure we will construct is shown below.

Create layout structure with thymeleaf on Spring Boot

└── templates
├── fragments
│ ├── footer.html
│ ├── header.html
│ └── main_layout.html
└── index.html

Now create our fragments respectively. The html tags we need to describe

xmlns:layout="" xmlns:th=""
<header th:fragment="header" xmlns:th="">
<!-- Static navbar -->
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<a class="navbar-brand" href="#">Project name</a>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About</a></li>

<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="./">Default <span class="sr-only">(current)</span></a></li>
<li><a href="../navbar-static-top/">Static top</a></li>
<li><a href="../navbar-fixed-top/">Fixed top</a></li>
</div><!--/.nav-collapse -->
</div><!--/.container-fluid -->
<div th:fragment="footer" class="footer" xmlns:th="">
<div class="container">
<span class="text-muted">Place sticky footer content here. &copy; 2018 @omeryazir</span>

Place our fragment on this page. We can use this page as the main html page

<!DOCTYPE html>
<html xmlns:layout="" xmlns:th="">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href=""
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href=""
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src=""
<!--header.html that's here -->
<div th:replace="fragments/header :: header"></div>

<div layout:fragment="content"></div>
<!--footer.html that's here -->
<div th:replace="fragments/footer :: footer"></div>
<html xmlns:layout="" layout:decorate="~{fragments/main_layout}">
<title>Spring Boot Thymeleaf + Spring Security</title>
<div layout:fragment="content" class="container">

<div class="jumbotron">
<h1>Navbar example</h1>
<p>This example is a quick exercise to illustrate how the default, static navbar and fixed to top navbar work.
It includes the responsive CSS and HTML, so it also adapts to your viewport and device.</p>
<a class="btn btn-lg btn-primary" href="../../components/#navbar" role="button">View navbar docs &raquo;</a>

Create IndexController to display your page.

package com.layout_example.layoutexample.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

public class IndexController {

public String showIndex(){
return "index";

We created a layout structure and index.html we placed it on our page. But we need to add the following dependency to the pom.xml file to run it.


not forget! maven projects need to be imported