serkan
serkan

Reputation: 1237

Using Thymeleaf when the value is null

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?

Upvotes: 114

Views: 227982

Answers (11)

tduchateau
tduchateau

Reputation: 4471

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>

The Elvis operator can be combined with the Noop token to use the template value instead. It acts like the th:text property is not set at all, when the value is not present.

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

Upvotes: 89

Orest
Orest

Reputation: 1921

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>

Note: this feature is from SpringStandardDialect, not the Thymeleaf standard dialect.

// call it if using thymeleaf without spring mvc
templateEngine.setDialect(new SpringStandardDialect())

Upvotes: 180

Valerij Dobler
Valerij Dobler

Reputation: 2764

The cleanest solution would be to only display it if it was set. Thymeleaf is being javascripty here:

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

Upvotes: 0

Nitish
Nitish

Reputation: 39

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

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

Upvotes: 0

SAAD BELEFQIH
SAAD BELEFQIH

Reputation: 372

you can use this solution it is working for me

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

Upvotes: 4

Palash
Palash

Reputation: 159

I use

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

Upvotes: 3

Alex Cumarav
Alex Cumarav

Reputation: 557

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)}

Upvotes: 9

Juan Carlos Mendoza
Juan Carlos Mendoza

Reputation: 5794

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>

Upvotes: 38

Roberto
Roberto

Reputation: 4869

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

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

Upvotes: 23

Vazgen Torosyan
Vazgen Torosyan

Reputation: 1295

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

Upvotes: 6

Ah Hiang
Ah Hiang

Reputation: 592

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>

Upvotes: 8

Related Questions