Unis
Unis

Reputation: 377

JSTL tag set/if

Evaluating using JSTL if

Why my value isOnline is not being evaluated as true using JSTL if

<c:set var="isOnline" value="${friend.online}"/>
<h:outputText value="#{isOnline}" />
<br/>
(isOnline) is: ${isOnline}
<c:if test="${isOnline == 'true'}" var="theTruth">
  <h:outputText value="hello"/>
</c:if>
<br/>
(isOnline == 'true') is: ${theTruth}

The result:

true
(isOnline) is: true
(isOnline == 'true') is: false

I am not really sure why the condition is being evaluated as false even though i am printing the value of isOnline which shows it's true.

any help would be appreciated.. Thanks in advance

Upvotes: 0

Views: 3038

Answers (1)

BalusC
BalusC

Reputation: 1108642

If it's a boolean, then you should not compare it to string. Just do

<c:if test="${isOnline}">

Further, I see that you're using JSF. You should really try to avoid JSTL tags as much as possible whenever JSF provides the same functionality out the box. That's because they doesn't run in sync as you'd expect from coding and may lead to unforeseen behaviour in certain circumstances. Just use the JSF rendered attribute.

<h:outputText value="Hello" rendered="#{friend.online}" />

For more examples of using the rendered attribute, check this answer.

Upvotes: 3

Related Questions