Comparing string and boolean in Expression language
Asked Answered
F

3

10

I have this behaviour I do not really understand

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

outputs

false
java.lang.Boolean
true
  1. How can it be exlpained?
  2. 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?
Forgiven answered 17/11, 2011 at 14:32 Comment(1)
Removed the old test case and replaced witha more compact and straightforward one.Forgiven
F
16

This is the behaviour of the language as defined in the 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

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

Foretoken answered 17/11, 2011 at 16:1 Comment(0)
A
6

If one of the both sides in EL is a Boolean (or boolean) and the other side is a String, then the String will be parsed to Boolean by Boolean#valueOf() whose javadoc says the following:

Returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true".

So, it returns false and this is indeed equal to false.

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.

Alesandrini answered 17/11, 2011 at 15:59 Comment(0)
F
0

As for point 2.

<c:set var="errorString" value="${'error'}" />
${someVar.class == errorString.class}

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.

<c:set var="strsomeVar">${someVar}</c:set>
//${someVar}
//${someVar.class}
//${someVar == 'error'}

Which outputs

//false
//class java.lang.String
//false 
Forgiven answered 18/11, 2011 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.