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.
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:
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:
And the Thymeleaf template usage:
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:
Then the template stays simple:
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.
Template:
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:
Template:
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
- Spring Boot 2.5.0 generates plain.jar file. Can I remove it?
- Spring Boot 2.6.0 / Spring fox 3 - Failed to start bean 'documentationPluginsBootstrapper
- Spring Boot 2.6 regression How can I fix Keycloak circular dependency in adapter?
- Spring Boot 2 - Actuator Metrics Endpoint not working
- Spring Boot 2 - Change Jar Name
- Spring Boot 2 - Unsatisfied dependency on Feign client when autowired for service
- Spring boot 2 Converting Duration java 8 application.properties
- Spring Boot 2 health actuator default mapping

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.