Spring Boot
Thymeleaf
Static Methods
Java
Web Development

Spring Boot 2.4.2 and Thymeleaf 3.0.12 - access static methods

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Thymeleaf templates backed by Spring, static methods are usually accessed through Spring Expression Language using the T(...) type operator. That means you do not normally expose a special bean just to call a static utility method. The important question is not whether it is possible, but whether calling static code directly in the template is the cleanest design.

The Basic Syntax

With the Spring-integrated Thymeleaf dialect, the usual form is:

html
<span th:text="${T(com.example.util.TextUtils).formatName(user.name)}"></span>

T(...) tells the expression engine to refer to a Java type. After that, you can call a static method on that class.

A minimal utility class:

java
1package com.example.util;
2
3public final class TextUtils {
4    private TextUtils() {}
5
6    public static String shout(String value) {
7        return value == null ? "" : value.toUpperCase();
8    }
9}

And the Thymeleaf template usage:

html
<p th:text="${T(com.example.util.TextUtils).shout(message)}"></p>

That is the direct answer for static-method access.

When This Works Well

Static calls in templates are reasonable when the method is:

  • simple
  • pure
  • presentation-oriented
  • unlikely to need dependency injection

Examples include:

  • formatting helper methods
  • string normalization helpers
  • constant-based presentation logic

They are less attractive for anything that reaches into services, repositories, localization state, or environment-dependent logic.

A Better Alternative for Complex Logic

If the method is not really a lightweight utility, expose a bean or precompute the value in the controller instead.

A controller example:

java
1@GetMapping("/profile")
2public String profile(Model model) {
3    String displayName = TextUtils.shout("alice");
4    model.addAttribute("displayName", displayName);
5    return "profile";
6}

Then the template stays simple:

html
<p th:text="${displayName}"></p>

This usually improves testability and keeps templates from turning into mini-programs.

Static Fields and Constants

The same T(...) mechanism also works for static fields.

java
1package com.example.util;
2
3public final class Constants {
4    public static final String APP_NAME = "DemoApp";
5}

Template:

html
<span th:text="${T(com.example.util.Constants).APP_NAME}"></span>

So the feature is not limited to methods.

Be Careful with Readability

A small number of static calls in a template is fine. A template full of nested T(...) expressions becomes hard to read fast.

That is usually the design boundary:

  • one or two formatting helpers in the template: acceptable
  • lots of logic and branching through static utilities: probably a smell

Thymeleaf is strongest when templates stay close to presentation and controller or service code handles most computation.

Typical Spring Boot Setup

You do not normally need special Spring Boot configuration just to enable this. If Thymeleaf is working with Spring expressions already, T(...) should be part of the normal expression language behavior.

A minimal controller and template are enough:

java
1@Controller
2public class HomeController {
3    @GetMapping("/")
4    public String home(Model model) {
5        model.addAttribute("message", "hello world");
6        return "home";
7    }
8}

Template:

html
1<!DOCTYPE html>
2<html xmlns:th="http://www.thymeleaf.org">
3<body>
4  <p th:text="${T(com.example.util.TextUtils).shout(message)}"></p>
5</body>
6</html>

Common Pitfalls

A common mistake is trying to expose a static utility as a Spring bean just so the template can call it. That is often unnecessary when T(...) is sufficient.

Another mistake is putting complex business logic into static methods and then calling them from the view. That makes templates harder to reason about.

People also sometimes get the class name wrong in the expression. T(...) needs the fully qualified class name unless the type is otherwise resolvable.

Finally, if the method needs injected dependencies, it is probably not a good candidate for static template access in the first place.

Summary

  • In Thymeleaf with Spring, static methods are typically accessed with ${T(full.qualified.ClassName).method(...)}
  • The same mechanism works for static fields too
  • This is suitable for small, pure presentation helpers
  • For heavier logic, compute the value in the controller or expose a normal bean instead
  • No special Spring Boot feature is usually required beyond normal Thymeleaf and Spring expression support
  • Use static calls sparingly so templates stay readable and presentation-focused

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.