Literals and Operators
Thymeleaf expressions support literals and operators so you can do more than just display variables. You can compare, combine, and check values right in your templates.
String Literals
Wrap text in single quotes to make it a string literal. This is how you add static text in expressions.
<p th:text="'Hello World'">Hello World</p>
<p th:text="'Welcome, ' + ${name} + '!'">Welcome!</p>
Remember: single quotes, not double quotes. Double quotes don't work for string literals in Thymeleaf.
Number Literals
Numbers work just like you'd expect. Use them in comparisons and calculations.
<p th:if="${age >= 18}">You are an adult.</p>
<p th:text="${price * 1.1}">Price with tax</p>
<p th:text="${100 / 4}">25</p>
You can do basic arithmetic right in the template. But keep complex calculations in the controller — templates should stay simple.
Boolean and Ternary Operators
Check conditions and pick values with boolean logic and the ternary operator.
<p th:if="${isLoggedIn}">Welcome back!</p>
<p th:if="${!isLoggedIn}">Please sign in.</p>
<p th:text="${isActive ? 'Active' : 'Inactive'}">Status</p>
The ternary operator condition ? trueValue : falseValue is perfect for showing one of two values based on a condition.
Empty Checks
The isEmpty operator checks if a string, collection, or array is empty. Super useful for conditional display.
<p th:if="${!name.isEmpty()}" th:text="${name}">Name</p>
<p th:if="${#lists.isEmpty(errors)}">No errors found.</p>
<p th:if="${messages.size() > 0}">You have new messages.</p>
You can also use the utility object #lists, #maps, and #sets for collection operations. They give you more options than the standard Java methods.