🏠ThymeleafThymeleaf Literals and Constants in Spring Boot

Thymeleaf Literals and Constants in Spring Boot

In this post, We will see how to use String literals,numeric and boolean constants in thymeleaf templates with an example in a spring boot application.

Spring Boot can support any primitive data type to be a literal in an expression. To clarify, all number types, Char types, boolean type and ofcourse strings.

Thymeleaf Literals

Text literals in Thymeleaf

Thymeleaf Text literals are equivalent to java String objects. They are a sequence of characters usually represented by wrapping them inside single quotes' as shown below.

    Here is a simple demo that explains <span th:text="'text literals'">This text will be gone</span>.
</div>Code language: HTML, XML (xml)

Subsequently, This results in the following output.

    Here is a simple demo that explains <span>text literals</span>.
</div>Code language: HTML, XML (xml)

The following is a complex implementation using String literal.

    Here is a simple demo that explains <span th:text="${userDto.getMessage('Home')}">This text will be gone</span>.
</div>Code language: HTML, XML (xml)

Here, the text `Home` is a String literal. That is how thymeleaf is able to appropriately call the methods.

Number literals

Number literals represents hmm… numbers. Duh…! There is nothing to explain about this type. Here are some examples that demonstrates number literals.

<div>The document contains <span th:text="7"></span> steps</div>
<div>We are at step <span th:text="2"></span></div>Code language: HTML, XML (xml)

The numbers can even be introduced to arithmetic operations.

<div>There are <span th:text="7-2"></span> steps remaining</div>Code language: HTML, XML (xml)

They can also be used on indexed Collections. And,of course they can be part of complex expressions.

<div>The first text Book name is <span th:text="${bookList[0].name}"></span></div>Code language: HTML, XML (xml)

As you see, We passed array index as `0` which is a numeric constant.

Boolean literals

Boolean literals are either true or false. They are usually used along with th:if where conditional expressions are evaluated. Some examples are,

<div th:if="true"></div>
<div th:if="${user.isActive()} == true"></div>
<div th:text="${userDto.getMessageByActiveStatus(true)}"></div>Code language: HTML, XML (xml)

The null literal

Finally, The null literal represents an object being NULL.

<div th:if="${someVariable} == null"></div>Code language: HTML, XML (xml)

The null literal exists for the sake of completeness. Because you can write templates for an entire web application without using the null literal.

Literal tokens

Thymeleaf has a little restriction towards String literals. This is because a space means two separate variables. Similarly, a comma is usually used for parameters in thymeleaf expressions. that is why you should always wrap large String literals in single quotes.

<div th:text="something"></div> <!--this is valid -->
<div th:text="something else"></div> <!--invalid due to space -->
<div th:text="something,else"></div> <!--invalid due to comma -->
<div th:text="'Hello to you, and you..!'"></div> <!--valid, look at the wrapped quotes-->Code language: HTML, XML (xml)


To conclude, We learned various type of literals in thymeleaf and how to use them. If you want to learn more about thymeleaf concepts take a look at the posts about thymeleaf.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *