Search code examples
thymeleaf

Thymeleaf: Conditional th:value


Problem:
${map} can be null.

<input type="text" th:value="${map.name}" />

What I need:
If name is not null then th:value=name otherwise th:value=""

 <input type="text" th:value="${map.name != null ? map.name : ''}" />

But my above code is not valid


Solution

  • Solution:

    <input th:value="${map !=null}? ${map.name} : ''" />
    

    Or Better (with Elvis Operator):

    <input type="text" th:value="${map?.name}"