Thymeleaf If Not Null or Empty

Handling null or empty values is a common requirement in web development, especially when dealing with dynamic content. Thymeleaf is a popular Java-based templating engine that offers straightforward mechanisms to handle such conditions gracefully. In this post, we'll explore how to use Thymeleaf to check if a variable is null or empty before rendering it in your HTML templates.

Introduction to Thymeleaf

Thymeleaf is a Java-based templating engine designed to process and generate HTML, XML, JavaScript, and CSS. It integrates seamlessly with Spring Boot and is often used to build dynamic web pages. Thymeleaf templates are natural HTML files. This means they can be viewed and edited as regular HTML in a browser, making development more intuitive.

Checking for Null or Empty Values in Thymeleaf

When working with Thymeleaf, it's essential to ensure that variables are not null or empty before displaying them. This helps to avoid errors and ensures that the user interface displays information correctly.

Using Thymeleaf's th:if Attribute

Thymeleaf provides the th:if attribute to conditionally render elements based on a given expression. You can use this attribute to check if a variable is not null or empty before displaying it.

Example: Checking for Null or Empty String

Suppose you have a variable name that you want to display only if it is not null or empty. You can achieve this using the th:if attribute as shown below:

<p th:if="${name != null and !#strings.isEmpty(name)}">
    Name: <span th:text="${name}"></span>
</p>

In this example, the <p> element will be rendered only if the name variable is not null and not an empty string. The #strings.isEmpty(name) function is a built-in utility in Thymeleaf to check for empty strings.

Using th:unless Attribute

The th:unless attribute is the opposite of th:if and is used to render an element only when the condition is false. This can be helpful when you want to display a message if a variable is null or empty.

Example: Displaying a Default Message

You can use th:unless to show a default message if the name variable is null or empty.

<p th:unless="${name != null and !#strings.isEmpty(name)}">
    Name not provided.
</p>
<p th:if="${name != null and !#strings.isEmpty(name)}">
    Name: <span th:text="${name}"></span>
</p>

In this case, the first <p> element will be rendered only if name is null or empty, displaying "Name not provided." If name has a value, the second <p> element will display the name.

Handling Collections and Lists

Thymeleaf also allows you to check if a collection or list is empty before rendering it. You can use the same th:if attribute to verify that a collection has elements.

Example: Checking a List

Suppose you have a list of items called itemsList that you want to display only if it is not empty.

<ul th:if="${itemsList != null and !#lists.isEmpty(itemsList)}">
    <li th:each="item : ${itemsList}" th:text="${item}"></li>
</ul>
<p th:unless="${itemsList != null and !#lists.isEmpty(itemsList)}">
    No items available.
</p>

Here, the <ul> element will be rendered only if itemsList is not null and contains elements. The <p> element will show "No items available." if the list is empty.

Conclusion

Thymeleaf provides powerful features for handling null or empty values in your templates. By using the th:if and th:unless attributes, you can conditionally render HTML elements based on the presence of data. This ensures that your web application displays information accurately and avoids errors related to null or empty values. By leveraging these capabilities, you can build dynamic and robust web applications with Thymeleaf.

Comments