0

I'm trying to create a list of elements, but the elements may be added if they satisfy their given conditions, for example :

<\div th:with = "$list = { {[condition1] ? [value1 if true] : null, [condition2] ? [value2 if true] : null } }">

What is intended is that, the size will vary depending on each conditions for the elements, so that list could either have 2 or 1 or no element at all. However, thymeleaf treats null as an element too, so I don't know if there is a way to instruct the engine to literally 'not add' when condition is false.

1 Answers1

0

I would need to see your code to evaluate your conditional statements, but perhaps you could still have the nulls, just not access them?

Spring has a safe navigation operator, which looks like this: ?.

It helps to avoid NullPointerException by directly skipping the object, if it is null, instead of trying to access its methods or properties.

Here is a Thymeleaf use case from this post:

<td th:text="${user?.address?.city}"></td>

Hope this helps you.

Petar Bivolarski
  • 1,599
  • 10
  • 19