Thymeleaf
Spring Boot
Application Version
Web Development
Java Framework

display application version in title using thymeleaf and springboot

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Displaying the application version in a Thymeleaf page is useful for support, QA, and deployment verification. In Spring Boot, the cleanest way to do it is to expose build metadata as a bean and pass that value into the model rather than hardcoding version strings in templates.

The Most Reliable Spring Boot Approach

Spring Boot can generate build metadata and expose it through the BuildProperties bean. That is usually better than manually maintaining a version string in a controller because the value comes from the actual build.

If you use Maven, enable the build-info goal in the Spring Boot plugin:

xml
1<build>
2    <plugins>
3        <plugin>
4            <groupId>org.springframework.boot</groupId>
5            <artifactId>spring-boot-maven-plugin</artifactId>
6            <executions>
7                <execution>
8                    <goals>
9                        <goal>build-info</goal>
10                    </goals>
11                </execution>
12            </executions>
13        </plugin>
14    </plugins>
15</build>

During the build, Spring Boot generates META-INF/build-info.properties. At runtime, Boot can turn that file into a BuildProperties bean.

Injecting the Version into a Controller

Once the build metadata exists, inject BuildProperties and add the version to the model.

java
1import org.springframework.boot.info.BuildProperties;
2import org.springframework.stereotype.Controller;
3import org.springframework.ui.Model;
4import org.springframework.web.bind.annotation.GetMapping;
5
6@Controller
7public class HomeController {
8
9    private final BuildProperties buildProperties;
10
11    public HomeController(BuildProperties buildProperties) {
12        this.buildProperties = buildProperties;
13    }
14
15    @GetMapping("/")
16    public String home(Model model) {
17        model.addAttribute("appVersion", buildProperties.getVersion());
18        return "home";
19    }
20}

Then use the value in Thymeleaf:

html
1<!DOCTYPE html>
2<html xmlns:th="http://www.thymeleaf.org">
3<head>
4    <title th:text="'My App v' + ${appVersion}">My App</title>
5</head>
6<body>
7    <h1>Dashboard</h1>
8</body>
9</html>

That updates the page title on every build without extra manual steps.

Making the Version Available to Every Template

If several pages need the version, do not repeat the same model.addAttribute call in every controller method. A @ControllerAdvice class can expose a shared model attribute globally.

java
1import org.springframework.boot.info.BuildProperties;
2import org.springframework.web.bind.annotation.ControllerAdvice;
3import org.springframework.web.bind.annotation.ModelAttribute;
4
5@ControllerAdvice
6public class GlobalModelAttributes {
7
8    private final BuildProperties buildProperties;
9
10    public GlobalModelAttributes(BuildProperties buildProperties) {
11        this.buildProperties = buildProperties;
12    }
13
14    @ModelAttribute("appVersion")
15    public String appVersion() {
16        return buildProperties.getVersion();
17    }
18}

Now every Thymeleaf template can use ${appVersion} without controller duplication.

Simpler Alternative: Read a Property

If you do not want build metadata, another option is to define the version in application.properties and inject it with @Value.

properties
app.version=@project.version@

With Maven resource filtering enabled, @project.version@ is replaced during the build. Then inject it:

java
1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.stereotype.Controller;
3import org.springframework.ui.Model;
4import org.springframework.web.bind.annotation.GetMapping;
5
6@Controller
7public class HomeController {
8
9    @Value("${app.version}")
10    private String appVersion;
11
12    @GetMapping("/")
13    public String home(Model model) {
14        model.addAttribute("appVersion", appVersion);
15        return "home";
16    }
17}

This works, but BuildProperties is more explicit and more aligned with Spring Boot's build metadata support.

Showing the Version Outside the Title

Once the version is in the model, you can reuse it anywhere in the page:

html
<footer>
    <small th:text="'Version ' + ${appVersion}">Version</small>
</footer>

This is especially useful for QA builds or internal tools where support staff need to confirm the deployed artifact quickly.

Common Pitfalls

The first pitfall is hardcoding the version in the Thymeleaf template. That guarantees drift because someone eventually forgets to update it during a release.

Another pitfall is assuming Spring Boot creates BuildProperties automatically without build configuration. The bean appears only when the metadata file is generated.

A third pitfall is exposing the version in only one controller when several templates need it. A global model attribute is cleaner in that situation.

Finally, think about whether the exact version should be visible publicly. For internal tools it is usually helpful. For internet-facing applications, some teams prefer a less specific release label for security or product reasons.

Summary

  • The clean Spring Boot solution is to generate build metadata and inject BuildProperties
  • Add the version to the Thymeleaf model and render it in the title with th:text
  • Use @ControllerAdvice when multiple templates need the same version value
  • A filtered application.properties value is a workable alternative
  • Avoid hardcoded template versions because they quickly drift from the real build

Course illustration
Course illustration

All Rights Reserved.