Displaying Data with Spring Boot and Thymeleaf Templates

Spring Boot with Thymeleaf for Data Display

1. Project Dependencies Setup

Add the following dependencies to your build.gradle file:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath('org.springframework.boot:spring-boot-gradle-plugin:2.5.4')
    }
}

group "com.example"
version "1.0-SNAPSHOT"
apply plugin: "java"
apply plugin: "org.springframework.boot"
apply plugin: "io.spring.dependency-management"

sourceCompatibility = 1.8
repositories {
    mavenCentral()
}

dependencies {
    compile(
        "org.springframework.boot:spring-boot-starter-web",
        "org.springframework.boot:spring-boot-starter-test",
        "org.springframework.boot:spring-boot-starter-cache",
        "org.springframework.boot:spring-boot-devtools",
        "org.springframework.boot:spring-boot-starter-thymeleaf",
        "net.sourceforge.nekohtml:nekohtml"
    )
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

2. Application Configuration

Configure Thymeleaf and datasource in application.yml:

spring:
  thymeleaf:
    mode: LEGACYHTML5
    cache: false
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/your_database
    username: your_username
    password: your_password

3. Data Model Definition

Create a class to hold your data:

package com.example.model;

public class UserData {
    private String identifier;
    private String title;
    private String description;

    public String getIdentifier() {
        return identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

4. Controller Implementation

Create a controller to handle requests and prepare data for the view:

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/display")
public class DisplayController {

    @GetMapping("/show-data")
    public String showData(Model model) {
        model.addAttribute("counter", 42);
        
        UserData userData = new UserData();
        userData.setIdentifier("xyz-123");
        userData.setTitle("Sample Data");
        userData.setDescription("This is a demonstration of data display using Thymeleaf");
        
        model.addAttribute("user", userData);
        return "data-display";
    }
}

5. Main Application Class

Create the main application entry point:

package com.example;

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

@SpringBootApplication
public class DataDisplayApplication {

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

6. HTML Template

Create a Thymeleaf template to display the data:


<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Data Display</title>
</head>
<body>
    <h1>Counter Value: <span th:text="${counter}"></span></h1>
    
    <div class="data-container">
        <h2 th:text="${user.title}"></h2>
        <p>ID: <span th:text="${user.identifier}"></span></p>
        <p>Description: <span th:text="${user.description}"></span></p>
    </div>
</body>
</html>

7. Running the Application

Start your Spring Boot application and navigate to:

http://localhost:8080/display/show-data

You should see the data displayed in your browser according to the template configuration.

Tags: Spring Boot thymeleaf Model Data Display web development

Posted on Tue, 08 Sep 2026 16:03:30 +0000 by benyboi