Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing strings with JSTL

Tags:

java

jsf

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>
like image 972
Moon13 Avatar asked Feb 12 '10 20:02

Moon13


2 Answers

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

like image 198
John Vint Avatar answered Sep 20 '22 21:09

John Vint


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.

like image 21
fastcodejava Avatar answered Sep 16 '22 21:09

fastcodejava