比较表达式语言中的字符串和布尔值 [英] Comparing string and boolean in Expression language

查看:523
本文介绍了比较表达式语言中的字符串和布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种行为我真的不明白

I have this behaviour I do not really understand

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

输出

false
java.lang.Boolean
true




  1. 如何解释?

  2. 为了第一次写测试的正确方法是什么测试两个'东西'是否具有相同的类型然后它们的值是否相同?


推荐答案

这是 EL规范中定义的语言行为:

This is the behaviour of the language as defined in the EL specification:


A {==,!=,eq,ne} B




  • 其他规则被删除

  • 如果A或B是布尔强制将A和B强制转换为布尔值,则应用运算符


  • 如果A为空或,则返回urn false

  • 否则,如果A是布尔值,则返回A

  • 否则,如果A是String,则为Boolean.valueOf( A)不抛出异常,退货

  • 否则,错误

  • 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),其中返回错误

So, the string literal is coerced to a boolean via Boolean.valueOf("error") which returns false.

这篇关于比较表达式语言中的字符串和布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆