如何在百里香叶中将值设置为“NULL”

问题描述 投票:1回答:1

我使用thymeleaf作为UI框架。我从数据库中提取数据并将其存储为对象。当我在表格中显示我的值时,我有一些空白的单元格。而不是空白,我如何输入值为“NULL”?

<table>
    <tr>
        <th>id</th>
        <th>name</th>
        <th>age</th>
        <th>years in school</th>
    </tr>
    <tr th:each="student : ${studentinfo}">
        <td th:text="${student.id}"></td>
        <td th:text="${student.name}"></td>
        <td th:text="${student.age}"></td>
        <td th:text="${student.years}">NULL</td>
        <!-- attempted -->
        <td th:text="${student.years != null} ? ${student.years}">NULL</td>
    </tr>
</table>

有些学生多年来有null。但是当我在我的UI中显示它时,它只是一个空白单元格。如果单元格为空,我想显示“NULL”。

html css thymeleaf
1个回答
2
投票

只需要使用三元表达式的其余部分:P

<td th:text = "${student.years != null ? student.years : 'NULL'}" />
© www.soinside.com 2019 - 2024. All rights reserved.