Using Thymeleaf when the value is null
Asked Answered
T

11

114

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?

Ticino answered 17/12, 2013 at 14:15 Comment(0)
S
180

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())
Segregation answered 24/1, 2017 at 15:7 Comment(9)
The ?. operator is called the "safe navigation" operator, per the Spring Expression Language docs.Syncom
While the above syntax is accepted as valid in spring boot 2.0.5 that comes with thymeleaf 3.0.9, at least for me it does not do what is claimed here. Is that a special feature that you have to enable?Obstruent
This feature is not from thymeleaf it is from springCalla
using ? on a numeric field that is 0 will also produce false. Thus you'd need to use the full != null conditional on number fields.Aretino
This is the best answer as this is more conciseLimewater
I get ognl.ExpressionSyntaxException: Malformed OGNL expression: error in SpringBoot 2.6Windpipe
I too get the same exception @Windpipe got. Using the same version of Spring Boot.Senter
It got error: Neither BindingResult nor plain target object for bean name 'user?' available as request attributeHoarsen
its a SpringStandardDialect of thymeleaf , non standard non default to use it detached from spring mvc follow link below: northcoder.com/post/use-springs-thymeleaf-dialect-withoFen
M
88

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>
Millinery answered 2/1, 2014 at 2:6 Comment(3)
Edit: Added the ${...} for the if condition as you have missed it. +1 for the ternary expression in th:text.Zolly
Could you please explain why there are multiple ${...} needed? What if I want to prefix both alternatives with e.g. 'Foo: '. Would I have to specify it twice inside the alternatives?Mweru
Your answer should be admitted as solution. Thank you.Bobbie
F
38

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>
Foote answered 1/9, 2018 at 6:20 Comment(1)
This is probably the most elegant way showing default values in thymeleaf. deserves way more upvotes!Secant
W
23

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

<span th:if="${someObject.someProperty != null}" th:text="${someObject.someProperty}">someValue</span>
Weltpolitik answered 4/7, 2017 at 7:12 Comment(2)
No need for != null only <span th:if="${someObject.someProperty}" ... is enoughCalla
What about th:if for a block why its not working ?Unrig
T
9

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

Tetrameter answered 25/10, 2018 at 20:22 Comment(0)
F
8

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>
Forceful answered 14/7, 2016 at 7:41 Comment(0)
A
5
   <p data-th-text ="${#strings.defaultString(yourNullable,'defaultValueIfYourValueIsNull')}"></p>
Ankney answered 24/3, 2017 at 10:24 Comment(0)
P
4

you can use this solution it is working for me

<span th:text="${#objects.nullSafe(doctor?.cabinet?.name,'')}"></span>
Procambium answered 31/8, 2019 at 18:55 Comment(0)
G
3

I use

<div th:text ="${variable != null} ? (${variable != ''} ? ${variable} : 'empty string message') : 'null message' "></div>
Goneness answered 7/12, 2018 at 4:29 Comment(0)
T
0

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

<td th:text="${ins.eValue!=null}? ${ins.eValue}:'NA'" />
Tallis answered 20/8, 2020 at 2:42 Comment(0)
B
0

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>
Bebebebeerine answered 30/6, 2023 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.