Using Thymeleaf when the value is null

后端 未结 10 1769
傲寒
傲寒 2020-11-29 21:01

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 v

相关标签:
10条回答
  • 2020-11-29 21:14
       <p data-th-text ="${#strings.defaultString(yourNullable,'defaultValueIfYourValueIsNull')}"></p>
    
    0 讨论(0)
  • 2020-11-29 21:14

    you can use this solution it is working for me

    <span th:text="${#objects.nullSafe(doctor?.cabinet?.name,'')}"></span>
    
    0 讨论(0)
  • 2020-11-29 21:17

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

    <td th:text="${ins.eValue!=null}? ${ins.eValue}:'NA'" />
    
    0 讨论(0)
  • 2020-11-29 21:18

    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>
    
    0 讨论(0)
  • 2020-11-29 21:19

    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.

    0 讨论(0)
  • 2020-11-29 21:23

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

    <span th:if="${someObject.someProperty != null}" th:text="${someObject.someProperty}">someValue</span>
    
    0 讨论(0)
提交回复
热议问题