java - 比较表达式语言中的字符串和 boolean 值

标签 java jsp weblogic el

我有这种行为我不太明白

${someVar}
${someVar.class.name}      
${someVar == 'error'}

输出

false
java.lang.Boolean
true
  1. 如何解决?
  2. 为了首先测试两个“事物”是否具有相同的类型然后它们的值是否相同,编写测试的正确方法是什么?

最佳答案

这是在 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/

相关文章:

java - 远程访问 Weblogic 服务器

java - 从 Android 客户端连接到 servlet 时出现问题

jsp - 使用 <jsp :include> tag 将参数传递到另一个 JSP 文件

java - Spring @Controller 未按预期返回 View

proxy - 带 Spring 的 CGLIB 抛出 IllegalAccessError

java - 如何使用java读取weblogic配置的myrealm用户和密码

java - 为什么我的 SwingWorker 不工作?

java - 如果我滚动如何保存复选框选中的项目

java - iText : PDFPTable - Not able to insert data in specific column

JavaScript - 将选定的下拉列表值传递给方法