当值为空时使用 Thymeleaf [英] Using Thymeleaf when the value is null

查看:66
本文介绍了当值为空时使用 Thymeleaf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一些值,如果尚未输入,它们可以为 null.

I have some values in my database which can be null if they have not already been entered.

但是当我在我的 html 中使用 Thymeleaf 时,它在解析空值时出错.

But when I use Thymeleaf in my html, it gives an error when parsing null values.

有什么办法可以解决这个问题吗?

Is there any way to handle this?

推荐答案

最短的方法是使用 '?'操作员.如果您有带有嵌入地址实体的用户实体,以便访问地址实体的字段并在地址不为空时打印它们,否则这里将是一个空列:

The shortest way is using '?' operator. If you have User entity with embedded Address entity in order to access fields of Address entity and print them if address is not null, otherwise here will be an empty column:

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

这篇关于当值为空时使用 Thymeleaf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆