th:text is a Thymeleaf attribute that replaces the body of an HTML element with the result of an expression, and it HTML-escapes that result. It is the standard way to print model values, messages and computed text in a server-rendered template without opening an XSS hole.
The text inside the tag in your source file is only a prototype value for designers. When Thymeleaf processes the template, it is replaced.
Given a controller that adds username to the model, the template can print it like this:
If username is Asha, the output is:
If you open the template file directly in a browser, you see placeholder name. This "natural template" behaviour is a core design goal of Thymeleaf.
th:text escapes <, >, & and quotes, so a value containing markup is shown as text rather than executed.
With comment set to <b>hi</b>:
Use th:utext only for content you have already sanitised, such as HTML produced by a trusted rich-text pipeline. Printing user input with th:utext is a cross-site scripting vulnerability.
| Syntax | Meaning | Example |
|---|---|---|
${...} | Variable expression (model or Spring EL) | th:text="${user.name}" |
*{...} | Selection on the object chosen with th:object | th:text="*{email}" |
#{...} | Message from a resource bundle | th:text="#{welcome.title}" |
@{...} | URL (rarely printed as text) | th:text="@{/home}" |
'...' | Literal | th:text="'Total'" |
Messages let you internationalise a page. With messages.properties containing welcome.title=Welcome, {0}!, you pass an argument like this:
The pipe syntax |...| is literal substitution, and it is easier to read than repeated plus signs.
The Elvis operator ?: supplies a default for null values, ?. avoids a null pointer on a missing intermediate object, and the #numbers and #temporals utility objects format values. #temporals works with java.time types and needs the thymeleaf-extras-java8time support, which Spring Boot includes.
For text inside an element, Thymeleaf also supports inlining with double brackets, which escape like th:text:
Use [(...)] for unescaped output, equal to th:utext. Inlining is shorter inside long paragraphs, but th:text is clearer when one element has a single value, and it keeps the template valid when opened statically.
th:text is often used with th:each and th:if:
th:text and child elements inside the same tag. th:text replaces the whole body, including children.th:text on a self-closing element such as <input>. Use th:value or th:field instead.th:utext for user input.th:text to work inside a <script> block. Use JavaScript inlining with th:inline="javascript" or [[...]] in the script, which escapes values for JavaScript.#request or #session objects in Thymeleaf 3.1 and later. Direct access to them was removed from expressions for security reasons, so pass the values through the model.th:text escapes HTML special characters, while th:utext writes the value as raw HTML. Prefer th:text unless the content is trusted and already sanitised.
A null value renders as an empty string. Use ?: to show a default, or use th:if to skip the element entirely.
Use a message expression such as th:text="#{key}". Spring Boot reads messages.properties from the classpath by default, with locale variants like messages_fr.properties.
Both escape the output. th:text replaces an element body, whereas [[...]] prints inline inside existing text.
th:text evaluates an expression and writes the escaped result as the element body. It supports variables, messages, literals, defaults and formatting, and it protects against XSS by default. Reach for th:utext only with trusted HTML, and use inlining when a sentence mixes text and values.