codeomnitrix
codeomnitrix

Reputation: 4249

java.lang.Character comparison in EL

I've a java.lang.Character bean property which I'd like to compare in EL as below:

#{q.isMultiple eq 'Y'}

It does not ever evaluate true.

How is this caused and how can I solve it?

Upvotes: 0

Views: 2326

Answers (4)

CashIsClay
CashIsClay

Reputation: 2320

In contrary to "plain Java", whether you single or double-quote your literals in EL, they represent both java.lang.String instances. Your method is returning a java.lang.Character instance, so this will never return true in an equals() call between both instances.

The solution is to change it to a String or boolean return type. The property name isMultiple strongly suggests a boolean. You only need to remove that is from the property name and keep it in the getter method.

private boolean multiple;

public boolean isMultiple() {
    return multiple;
}
#{q.multiple}

An alternative is using an enum. This would only be applicable if you have more than two states (or perhaps three, a Boolean also includes null).

Upvotes: 2

Shams
Shams

Reputation: 3667

Can also do this by converting the Character to String by printing it as body of <c:set> as below:

<c:set var="isMultipleVal">#{q.isMultiple}</c:set>

And then comparing it instead:

#{isMultipleVal eq 'Y'}

Upvotes: 2

Marcio J
Marcio J

Reputation: 780

If you're using EL 2.2, one workaround is:

#{q.isMultiple.toString() eq 'Y'}

Upvotes: 1

Selvakumar Ponnusamy
Selvakumar Ponnusamy

Reputation: 5533

You may need to look the property isMultiple in the Class of variable 'q'. Please verify your class having a getter method with signature as public char getIsMultiple(){}.

Also, are you sure ${q.isMultiple eq 'Y'} gives true?

Upvotes: -1

Related Questions