简体   繁体   English

如何正确检查 Spring EL 表达式中的 Boolean 值?

[英]How to properly check for Boolean value in Spring EL expression?

I'm a bit new to using Spring Expression Language but I'm trying to see what is the best way to check a that a Boolean value is not null and true?我对使用 Spring 表达式语言有点陌生,但我想看看检查 Boolean 值不是 null 的最佳方法是什么?

For type "Display" there is a property "removeMessage" which is of type java.lang.Boolean .对于“显示”类型,有一个类型为java.lang.Boolean的属性“removeMessage”。 So I want to first check that this value is not null and that it is true.所以我想首先检查这个值不是 null 并且它是真的。 Based on if this is true or not I will either return an empty string or a calculatedMessage.根据这是否属实,我将返回一个空字符串或一个计算消息。

I am currently checking with below expression:我目前正在检查以下表达式:

display?.removeMessage != null && display?.removeMessage ? '' : display.calculatedMessage

While this works, I was hoping to find a more nicer way to do the null and true check.虽然这可行,但我希望找到一种更好的方法来执行 null 和真正的检查。 I was thinking/hoping for something more like how we would check normally such as eg BoolenUtils.isTrue(value) (apache), or even just checking like Boolean.TRUE.equals(value) which takes care of the null check.我正在考虑/希望更多类似于我们如何正常检查的东西,例如BoolenUtils.isTrue(value) (apache),甚至只是像Boolean.TRUE.equals(value)这样的检查,它负责 null 检查。

Just having this should work, and already include your null check:有了这个就可以了,并且已经包含了您的 null 检查:

display?.removeMessage ? null : display.calculatedMessage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM