I have this behaviour I do not really understand
${someVar}
${someVar.class.name}
${someVar == 'error'}
outputs
false
java.lang.Boolean
true
- How can it be exlpained?
- What it the correct way to write the test in order to first test if the two 'things' have the same type and then if their value is the same?
If one of the both sides in EL is a
Boolean
(orboolean
) and the other side is aString
, then theString
will be parsed toBoolean
byBoolean#valueOf()
whose javadoc says the following:So, it returns
false
and this is indeed equal tofalse
.You need to rewrite your EL expression to take into account that the type can be both a boolean and a string, or just to stick to a single type and not to mix types in a single attribute.
This is the behaviour of the language as defined in the EL specification:
So, the string literal is coerced to a boolean via
Boolean.valueOf("error")
which returns false.As for point 2.
will check that the two 'things' are instances of the same class.
But it might be better to convert
someVar
to string, and then compare the two strings, since (at least in my case) someVar is not guaranteed to be a String or a Boolean, so it could be anything else that can be converted to a String.Which outputs