ChatGPT解决这个技术问题 Extra ChatGPT

如何检查 EL 中的布尔条件?

这个对吗?

<c:if test="${theBooleanVariable == false}">It's false!</c:if>

或者我可以这样做吗?

<c:if test="${!theBooleanVariable}">It's false!</c:if>

R
Romain Linsolas

您可以查看 EL(表达式语言)描述 here

您的两个代码都是正确的,但我更喜欢第二个,因为将布尔值与 truefalse 进行比较是多余的。

为了提高可读性,您还可以使用 not 运算符:

<c:if test="${not theBooleanVariable}">It's false!</c:if>

k
kiritsuku

两者都有效。您可以写 eq 而不是 ==


S
Shams

你也可以这样检查

<c:if test="${theBooleanVariable ne true}">It's false!</c:if>