JSF 具有不同的对象类型

时间:2021-12-13 22:51:28

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

我正在尝试比较JSF中的两个不同对象。一个字符串和一个整数,它不起作用...

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

// myVar ==>整数对象// myVar2 ==>字符串

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

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

我尝试使用myVar.toString,但这是错误的。那怎么办呢?

Thank's

2 个解决方案

#1


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

我正在尝试比较JSF中的两个不同对象。一个字符串和一个整数,它不起作用...

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:

...这些示例表达式的计算结果为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:

JSP 2.1(EL)规范说评估了相等性:

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

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

如果A或B是Byte,Short,Character,Integer或Long强制A和B强制为Long,则应用运算符

#2


Try using the JSTL fmt tags:

尝试使用JSTL fmt标记:

<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).

(或者,相反,您可以使用fmt:formatNumber将Integer格式化为String并与其他字符串进行比较)。

#1


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

我正在尝试比较JSF中的两个不同对象。一个字符串和一个整数,它不起作用...

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:

...这些示例表达式的计算结果为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:

JSP 2.1(EL)规范说评估了相等性:

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

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

如果A或B是Byte,Short,Character,Integer或Long强制A和B强制为Long,则应用运算符

#2


Try using the JSTL fmt tags:

尝试使用JSTL fmt标记:

<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).

(或者,相反,您可以使用fmt:formatNumber将Integer格式化为String并与其他字符串进行比较)。