如何在thymeleaf中将值设置为“NULL”
问题描述:
我正在使用thymeleaf作为UI框架。我从数据库中提取数据并将其作为对象存储。当我在表格中显示我的值时,我有一些空白的单元格。而不是它是空白我怎样才能输入值为“NULL”如何在thymeleaf中将值设置为“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>
有些学生多年无效。但是当我在UI中显示它时,它只是一个空白单元格。如果单元格为空白,我想显示“NULL”。
答
只需要使用三元表达式的其余部分:P
<td th:text = "${student.years != null ? student.years : 'NULL'}" />