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?

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
serkan
  • 1,237
  • 2
  • 12
  • 14

11 Answers11

178

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())
Mikhail Boyarsky
  • 2,908
  • 3
  • 22
  • 37
Orest
  • 1,801
  • 1
  • 11
  • 5
  • 13
    The `?.` operator is called the "safe navigation" operator, per the [Spring Expression Language docs](https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/expressions.html#expressions-operator-safe-navigation). – rdguam Jan 31 '18 at 02:35
  • 2
    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? – keyboardsamurai Nov 06 '18 at 11:10
  • This feature is not from thymeleaf it is from spring – deFreitas Mar 24 '19 at 18:01
  • 4
    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. – Dave Sep 23 '19 at 19:27
  • This is the best answer as this is more concise – Peter Chaula Apr 26 '20 at 08:19
  • 2
    I get ognl.ExpressionSyntaxException: Malformed OGNL expression: error in SpringBoot 2.6 – sarath Jan 04 '22 at 05:37
  • 1
    I too get the same exception @sarath got. Using the same version of Spring Boot. – Aspiring Dev Feb 11 '22 at 16:54
  • It got error: Neither BindingResult nor plain target object for bean name 'user?' available as request attribute – Chen Jiling Nov 13 '22 at 03:06
  • its a SpringStandardDialect of thymeleaf , non standard non default to use it detached from spring mvc follow link below: https://northcoder.com/post/use-springs-thymeleaf-dialect-witho/ – Mikhail Boyarsky Jul 08 '23 at 04:41
85

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>
Ashish Lahoti
  • 648
  • 6
  • 8
tduchateau
  • 4,351
  • 2
  • 29
  • 40
  • `Edit:` Added the `${...}` for the if condition as you have missed it. +1 for the ternary expression in `th:text`. – Lucky Mar 03 '16 at 06:14
  • 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? – lilalinux Aug 31 '16 at 11:43
  • Your answer should be admitted as solution. Thank you. – joninx Jan 24 '17 at 13:06
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>
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
23

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

<span th:if="${someObject.someProperty != null}" th:text="${someObject.someProperty}">someValue</span>
Roberto
  • 4,524
  • 1
  • 38
  • 30
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)}

Alex Cumarav
  • 537
  • 5
  • 7
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>
Ah Hiang
  • 592
  • 6
  • 13
5
   <p data-th-text ="${#strings.defaultString(yourNullable,'defaultValueIfYourValueIsNull')}"></p>
Vazgen Torosyan
  • 1,255
  • 1
  • 12
  • 26
4

you can use this solution it is working for me

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

I use

<div th:text ="${variable != null} ? (${variable != ''} ? ${variable} : 'empty string message') : 'null message' "></div>
Palash
  • 139
  • 2
  • 7
0

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

<td th:text="${ins.eValue!=null}? ${ins.eValue}:'NA'" />
Nitish
  • 39
  • 3
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>
Valerij Dobler
  • 1,848
  • 15
  • 25