0

I'm having a problem with JSTL.

I'm trying to compare a number inside JSTL and I'm struggling to get the result right:

(menu.TipoMenu is of Integer type but the tests with int, long and BigDecimal were had all the same result)

This doesn't work (always false)

 <c:if test="#{ menu.tipoMenu.compareTo(2) eq 0 }">ok</c:if>

This is always false

<c:if  test="#{ menu.tipoMenu.compareTo(2) eq 0 }" var="result"/>
       #{result}

But, using JSF tags, all is bright :)

This renders ok

And this shows true or false, depending on the case

<h:outputText value="#{ menu.tipoMenu.compareTo(2) eq 0  }" />

I'm using .compareTo but I tried using == with int and long (and also with Integer at some point).

The point is that the expression evaluates ok in JSF tags but not in JSTL.

What could I be doing wrong?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
dsantos80
  • 13
  • 2
  • 1
    Regardless of the awkward `compareTo()` approach (you should have used `equals()` or, in EL better, `eq` instead), where exactly is `#{menu}` defined? Is it defined as `var` of a parent `` or so? – BalusC Sep 15 '15 at 13:24
  • I've tried eq also. equals() I've tried just now and it also doesn't work. Menu is a var with an entity object that comes from the MBean. – dsantos80 Sep 15 '15 at 14:48
  • I said, "regardless", so it's not intented as the solution. But now you have given a clue, the `#{menu}` is indeed only available during view render time, exactly as I guessed. Now, checkout the duplicate for the answer. – BalusC Sep 15 '15 at 14:52
  • Thanks, I had no idea that the rendering order would affect this. – dsantos80 Sep 15 '15 at 14:57
  • 1
    Make sure that you read the "See also" link over there too. Just to make sure that you **understand** it. – BalusC Sep 15 '15 at 14:58

0 Answers0