Html 当值为空时使用 Thymeleaf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20636456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:31:47  来源:igfitidea点击:

Using Thymeleaf when the value is null

htmlnullthymeleaf

提问by serkan

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 values.

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

Is there any way to handle this?

有没有办法处理这个?

回答by Orest

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>

回答by tduchateau

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:

你甚至可以省略“else”表达式:

<span th:text="${someObject.someProperty != null} ? ${someObject.someProperty}">someValue</span>

You can also take a look at the Elvis operatorto display default values.

您还可以查看Elvis 运算符以显示默认值。

回答by Roberto

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

您可以将 'th:if' 与 'th:text' 一起使用

<span th:if="${someObject.someProperty != null}" th:text="${someObject.someProperty}">someValue</span>

回答by Juan Carlos Mendoza

This can also be handled using the elvis operator?:which will add a default value when the field is null:

这也可以使用elvis 运算符来处理,该运算符?:将在字段为 null 时添加一个默认值:

<span th:text="${object.property} ?: 'default value'"></span>

回答by Ah Hiang

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>

回答by Vazgen Torosyan

   <p data-th-text ="${#strings.defaultString(yourNullable,'defaultValueIfYourValueIsNull')}"></p>

回答by Alex Cumarav

Also worth to look at documentation for #objects build-in helper: https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#objects

还值得查看#objects 内置助手的文档:https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#objects

There is useful: ${#objects.nullSafe(obj, default)}

有一个有用的: ${#objects.nullSafe(obj, default)}

回答by SAAD BELEFQIH

you can use this solution it is working for me

你可以使用这个对我有用的解决方案

<span th:text="${#objects.nullSafe(doctor?.cabinet?.name,'')}"></span>

回答by Palash

I use

我用

<div th:text ="${variable != null} ? (${variable != ''} ? ${variable} : 'empty string message') : 'null message' "></div>