Spring Boot + Bootstrap + Thymeleaf Tree Table

July 16, 2020 No comments Spring Boot Bootstrap Thymeleaf Tree-Table

Spring Boot + Bootstrap + Thymeleaf Tree Table

1. Introduction

In this article, we are going to present the Thymeleaf Tree Table component. Tree table elements are frequently used in web applications for example: to present folder or employees' hierarchical structure. This tutorial will describe how to create this kind of component and fill them with data generated in the back-end.

If you are looking for more useful articles about Thymeleaf, please check the following links:
Thymeleaf Articles
Create DataTable component in Thymeleaf template
Handle table pagination in Thymeleaf

2. Dependencies

2.1. Maven dependencies

To present how the Thymeleaf Tree Table component works with Thymeleaf we used a simple Spring Boot application created as a Maven project using the following dependencies:

2.2. Front-End libraries
  • bootstrap - a UI framework for creating responsive designes,
  • jquery-treetable - library for creating tree table components.

Project Maven pom.xml file has the following structure:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">



    <!-- Inherit defaults from Spring Boot -->

    <!-- Add typical dependencies for a web application -->





    <!-- Package as an executable jar -->


3. Web Controller, Tree Node, and Main Application class

In the model layer, we have a single object Node that will be used to create a hierarchical structure. This structure will be flat for the sake of simplicity.

package com.frontbackend.thymeleaf.bootstrap.model;

import lombok.Getter;
import lombok.Setter;

public class Node {

    private String nodeId; // node id
    private String pid; // parent id
    private String text;
    private String href;

    public Node(String nodeId, String pId, String text, String href) {
        this.nodeId = nodeId;
        this.pid = pId;
        this.text = text;
        this.href = href;

Node class contains a unique identifier in the fields: nodeId. To create tree structure we have pid field that points to the parent element.

NodeConroller will be responsible for returning sample tree table data for GET /nodes requests:

package com.frontbackend.thymeleaf.bootstrap.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.frontbackend.thymeleaf.bootstrap.model.Node;

public class NodesController {

    public List<Node> nodes() {
        return getSampleNodeList();

    private List<Node> getSampleNodeList() {
        List<Node> nodes = new ArrayList<>();
        nodes.add(new Node("Root", "0", "Root", "http://frontbackend.com/"));
        nodes.add(new Node("Child1", "Root", "Child1", "http://frontbackend.com/child1"));
        nodes.add(new Node("Child2", "Root", "Child2", "http://frontbackend.com/child2"));
        nodes.add(new Node("Child3", "Root", "Child3", "http://frontbackend.com/child3"));
        nodes.add(new Node("Child3.1", "Child3", "Child3.1", "http://frontbackend.com/child3/child1"));
        nodes.add(new Node("Child3.2", "Child3", "Child3.2", "http://frontbackend.com/child3/child2"));
        nodes.add(new Node("Child4", "Root", "Child4", "http://frontbackend.com/child4"));
        return nodes;

The Application is the Java class with the main method that starts the Spring Boot application server:

package com.frontbackend.thymeleaf.bootstrap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

4. Templates

In the presentation layer, we have only one Thymeleaf template - index.html - the main view.

The index.html template has the following structure:

<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8"/>
    <title>Spring Boot Thymeleaf Application - Bootstrap Tree Table</title>

    <link th:rel="stylesheet" th:href="@{assets/jquery-treetable/jquery.treetable.css}"/>
    <link th:rel="stylesheet" th:href="@{assets/jquery-treetable/jquery.treetable.theme.default.css}"/>
    <link th:rel="stylesheet" th:href="@{webjars/bootstrap/4.0.0-2/css/bootstrap.min.css} "/>

<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top">
    <div class="container">
        <a class="navbar-brand" href="/">Thymeleaf - Bootstrap Tree Table</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive"
                aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item active">
                    <a class="nav-link" href="#">Home
                        <span class="sr-only">(current)</span>
                <li class="nav-item">
                    <a class="nav-link" href="#">About</a>
                <li class="nav-item">
                    <a class="nav-link" href="#">Services</a>
                <li class="nav-item">
                    <a class="nav-link" href="#">Contact</a>

<div class="container">

    <div class="row">
        <div class="col mt-5">

            <table id="treeTable" class="table">
                    <th>First Name</th>



<script th:src="@{/webjars/jquery/jquery.min.js}"></script>
<script th:src="@{/webjars/popper.js/umd/popper.min.js}"></script>
<script th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"></script>
<script th:src="@{/assets/jquery-treetable/jquery.treetable.js}"></script>

<script type="text/javascript">
    $(document).ready(function () {
            "type": 'get',
            "url": '/nodes',
            "dataType": "json",
            "success": function (data) {
                $.each(data, function (idx, obj) {
                    $("#treeTable").append("<tr data-tt-id=\"" + obj.nodeId + "\" data-tt-parent-id=\"" + obj.pid + "\"><td>" + obj.text + "</td><td>" + obj.href + "</td><td><button id=\"" + obj.nodeId + "\">edit</button><button id=\"" + obj.nodeId + "\">delete</button></td></tr>");
                    expandable: true,
                    initialState: "expanded",
                    clickableNodeNames: true,
                    indent: 30


5. The output

The running application is available under http://locahost:8080 URL and presents the following functionality:

Thymeleaf bootstrap tree table

6. Conclusion

In this article, we presented how to use Tree Table components in Thymeleaf templates.

As usual, the code used in this article is available under our GitHub repository.

{{ message }}

{{ 'Comments are closed.' | trans }}