Using Thymeleaf when the value is null

serkan picture serkan · Dec 17, 2013 · Viewed 129.7k times · Source

I have some values in my database which can be null if they have not already been entered.

But when I use Thymeleaf in my html, it gives an error when parsing null values.

Is there any way to handle this?

Answer

Orest picture Orest · Jan 24, 2017

The shortest way is using '?' operator. If you have User entity with embedded Address entity in order to access fields of Address entity and print them if address is not null, otherwise here will be an empty column:

<td th:text="${user?.address?.city}"></td>