views:

4042

answers:

2

Hello,

I'm trying to compare two different object in JSF. A String and an Integer, of cours it don't work...

//myVar ==> Integer object
//myVar2 ==> String

<c:if test="${myVar == myVar2}">
 YES!!!!!!!!
</c:if>

I try with myVar.toString but it's wrong. So how to do it ?

Thank's

+1  A: 

Try using the JSTL fmt tags:

<fmt:parseNumber type="number" var="myVar2AsNumber" value=${myVar2} />


<c:if test="${myVar == myVar2AsNumber}">
        YES!!!!!!!!
</c:if>

(or, in reverse, you could use fmt:formatNumber to format the Integer as a String and compare to the other string).

JacobM
+1  A: 

I'm trying to compare two different object in JSF. A String and an Integer, of cours it don't work...

That does not sound right - I would check the values. For the bean:

public class CoercedBean {

  public int getValueAsInt() {
    return 123;
  }

  public String getValueAsString() {
    return "123";
  }

}

...these example expressions evaluate to true:

${coercedBean.valueAsInt == coercedBean.valueAsString}
<h:outputText style="color: blue"
    value="#{coercedBean.valueAsInt eq coercedBean.valueAsString}" />

The JSP 2.1 (EL) spec says of evaluating equality:

A {==,!=,eq,ne} B

If A or B is Byte, Short, Character, Integer, or Long coerce both A and B to Long, apply operator

McDowell