Using Thymeleaf when the value is null

HtmlNullThymeleaf

Html Problem Overview


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?

Html Solutions


Solution 1 - Html

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>



     

    

Solution 2 - Html

Sure there is. You can for example use the conditional expressions. For example:

<span th:text="${someObject.someProperty != null} ? ${someObject.someProperty} : 'null value!'">someValue</span>

You can even omit the "else" expression:

<span th:text="${someObject.someProperty != null} ? ${someObject.someProperty}">someValue</span>

You can also take a look at the Elvis operator to display default values like this:-

<span th:text="${someObject.someProperty} ?: 'default value'">someValue</span>

Solution 3 - Html

This can also be handled using the elvis operator ?: which will add a default value when the field is null:

<span th:text="${object.property} ?: 'default value'"></span>

Solution 4 - Html

You can use 'th:if' together with 'th:text'

<span th:if="${someObject.someProperty != null}" th:text="${someObject.someProperty}">someValue</span>

Solution 5 - Html

Also worth to look at documentation for #objects build-in helper: https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#objects

There is useful: ${#objects.nullSafe(obj, default)}

Solution 6 - Html

You've done twice the checking when you create

${someObject.someProperty != null} ? ${someObject.someProperty}

You should do it clean and simple as below.

<td th:text="${someObject.someProperty} ? ${someObject.someProperty} : 'null value!'"></td>

Solution 7 - Html

   <p data-th-text ="${#strings.defaultString(yourNullable,'defaultValueIfYourValueIsNull')}"></p>

Solution 8 - Html

you can use this solution it is working for me

<span th:text="${#objects.nullSafe(doctor?.cabinet?.name,'')}"></span>

Solution 9 - Html

I use

<div th:text ="${variable != null} ? (${variable != ''} ? ${variable} : 'empty string message') : 'null message' "></div>

Solution 10 - Html

The shortest way! it's working for me, Where NA is my default value.

<td th:text="${ins.eValue!=null}? ${ins.eValue}:'NA'" />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionserkanView Question on Stackoverflow
Solution 1 - HtmlOrestView Answer on Stackoverflow
Solution 2 - HtmltduchateauView Answer on Stackoverflow
Solution 3 - HtmlJuan Carlos MendozaView Answer on Stackoverflow
Solution 4 - HtmlRobertoView Answer on Stackoverflow
Solution 5 - HtmlAlex CumaravView Answer on Stackoverflow
Solution 6 - HtmlAh HiangView Answer on Stackoverflow
Solution 7 - HtmlVazgen TorosyanView Answer on Stackoverflow
Solution 8 - HtmlSAAD BELEFQIHView Answer on Stackoverflow
Solution 9 - HtmlPalashView Answer on Stackoverflow
Solution 10 - HtmlNitishView Answer on Stackoverflow