JSF <c: if> with another type of object

I am trying to compare two different objects in JSF. String and integer, this does not work ...

// myVar ==> Integer object
// myVar 2 ==> String

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

I am trying to use myVar.toString, but that is wrong. So how to do this?

Thanks x

+3
source share
2 answers

I am trying to compare two different objects in JSF. String and integer, this does not work ...

That doesn't sound right - I would check the values. For bean:

public class CoercedBean {

  public int getValueAsInt() {
    return 123;
  }

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

}

... these expressions express true :

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

The JSP 2.1 (EL) specification talks about equality assessment:

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

A B - Byte, Short, Character, A, B to Long,

+4

fmt JSTL:

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


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

(, , fmt: formatNumber Integer ).

+2

Source: https://habr.com/ru/post/1710008/


All Articles