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?
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?
You can use 'th:if' together with 'th:text'
Sure there is. You can for example use the conditional expressions. For example:
You can even omit the "else" expression:
You can also take a look at the Elvis operator to display default values.
You've done twice the checking when you create
You should do it clean and simple as below.
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:
This can also be handled using the elvis operator
?:
which will add a default value when the field is null: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)}