我有这种行为我不太明白
${someVar}
${someVar.class.name}
${someVar == 'error'}
输出
false
java.lang.Boolean
true
- 如何解决?
- 为了首先测试两个“事物”是否具有相同的类型然后它们的值是否相同,编写测试的正确方法是什么?
最佳答案
这是在 EL specification 中定义的语言行为:
A {==,!=,eq,ne} B
- other rules elided
- If A or B is Boolean coerce both A and B to Boolean, apply operator
Coerce A to Boolean
- If A is null or "", return false
- Otherwise, if A is a Boolean, return A
- Otherwise, if A is a String, and Boolean.valueOf(A) does not throw an exception, return it
- Otherwise, error
因此,字符串文字通过 Boolean.valueOf("error")
强制转换为 boolean 值 returns false .
关于java - 比较表达式语言中的字符串和 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8168821/