mercredi 7 juin 2017

JSTL c:if result is opposite of what is expected

<c:set var="rowEntityExists" value="#{ !empty row.entity }" />

<h:outputText value="line 3 - #{ rowEntityExists }" /><br/>

<c:if test="#{ rowEntityExists }">
    <h:outputText value="line 6 - #{ rowEntityExists }" /><br/>
</c:if>

<h:outputText value="line 9 - #{ rowEntityExists }" /><br/>

The above code sets the variable "rowEntityExists" to the evaluation of "!empty row.entity". Previously the if statement was simply:

<c:if test="#{ !empty row.entity }">

...but I added the variable declaration and the "h:output" lines because of the bizarre results I was getting. The variable "row" can be one of two types of object, one of which has an attribute named "entity" while the other does not.

Here is what happens: The first and last "h:output" lines output the value "true", but the code inside the "c:if" block never runs. The code should result in an output of:

line 3 - true
line 6 - true
line 9 - true

But instead it outputs:

line 3 - true
line 9 - true

Now, what makes this so confusing for me, is that if I change the "c:if" to:

<c:if test="#{ !rowEntityExists }">

...or...

<c:if test="#{ rowEntityExists == false }">

...then the code within the "c:if" block runs. Making it even more perplexing it outputs the expected

line 3 - true
line 6 - true
line 9 - true

...meaning that at all three points "rowEntityExists" contains the value "true", but when it is evaluated in the "c:if" statement, it somehow is false. If however, I change the first line to this:

<c:set var="rowEntityExists" value="#{ true }" />

...then the code within the "c:if" block runs and the code outputs the 3 lines with "true". So... this somehow means that the variable "rowEntityExists" shows as true when output, but evaluates to false.

I feel like I must be missing something painfully obvious, but I can't figure out what it is - any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire