Moon13
Moon13

Reputation: 283

Comparing strings with JSTL

I have two strings that i need to compare, but even if they have the same values or different , it always enters the statement...

<c:when test="#{bean.name1 != bean.name2}">
     fields that are supposed to appear _only_ when name1 is different from name2
</c:when>

Upvotes: 11

Views: 33147

Answers (6)

fastcodejava
fastcodejava

Reputation: 41097

Should it be ?

<c:if test="#{bean.name1 != bean.name2}">
     // code
</c:if>

EDIT : <c:when> is supposed to be inside <c:choose>. Cannot ask why, that is just the syntax. It is like asking why if will not work in place of switch in C/C++/Java. They are just different animals.

Upvotes: 4

John Vint
John Vint

Reputation: 40256

The problem is that you probably did not wrap the when in a choose tag.

if you have:

    <c:choose>
    <c:when test="${bean.name1 != bean.name2}">
        fields that are supposed to appear _only_ when name1 is different from name2
    </c:when>
</c:choose>

It will work

Upvotes: 7

GreenieMeanie
GreenieMeanie

Reputation: 3610

I have noticed some flakiness when using c:if or c:choose and c:when inside some jsf iteration components, such as rich:datatable. What is the full context?

As a workaround, I would commonly have to wrap things in an a4j:outputPanel and set the rendered attribute accordingly.

Upvotes: 0

Mr. Shiny and New 安宇
Mr. Shiny and New 安宇

Reputation: 13908

Does it make any difference if you do this:

<c:when test="${bean.name1 != bean.name2}">
     fields that are supposed to appear _only_ when name1 is different from name2
</c:when>

Upvotes: 0

curv
curv

Reputation: 3844

Try this...

<c:if test="${bean.name1 ne bean.name2}">
     fields that are supposed to appear _only_ when name1 is different from name2
</c:if>

ne = not equal

Also

# should be $

Upvotes: -1

fastcodejava
fastcodejava

Reputation: 41097

Should it be fields that are supposed to appear only when name1 is different from name2

Upvotes: -1

Related Questions