当前位置 : 主页 > 编程语言 > java >

jsp – String to Long比较抛出Tomcat 7中的“ELException:无法转换”,适用于Tomcat 6

来源:互联网 收集:自由互联 发布时间:2021-06-25
以下代码段在Tomcat 6中运行正常, c:set var="abc" value="$12,345" /c:choose c:when test="${abc ne 0}" c:out value="PASS"/c:out /c:when c:otherwise c:out value="FAIL"/c:out /c:otherwise /c:choose 但在Tomcat 7中抛出异常. javax
以下代码段在Tomcat 6中运行正常,

<c:set var="abc" value="$12,345" />
<c:choose>
    <c:when test="${abc ne 0}">
        <c:out value="PASS"></c:out>
    </c:when>
    <c:otherwise>
        <c:out value="FAIL"></c:out>
    </c:otherwise> 
</c:choose>

但在Tomcat 7中抛出异常.

javax.el.ELException: Cannot convert $1,2345 of type class java.lang.String to class java.lang.Long
    at org.apache.el.lang.ELSupport.coerceToNumber(ELSupport.java:304)
    at org.apache.el.lang.ELSupport.coerceToNumber(ELSupport.java:283)
    at org.apache.el.lang.ELSupport.equals(ELSupport.java:143)
    at org.apache.el.parser.AstNotEqual.getValue(AstNotEqual.java:40)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
    at org.apache.jsp.WEB_002dINF.jsp.web.statussummary.status_005fsummary_jsp._jspx_meth_c_005fwhen_005f0(status_005fsummary_jsp.java:290)

看起来在Tomcat 7中表达式${abc ne 0}的方式有所不同.在Tomcat 6中,${abc}和${0}都被比较为字符串,但在Tomcat 7中我得到了这个异常.我不知道为什么会发生这种情况以及哪个API的哪个类文件对此负责.

这是怎么造成的,我该如何解决?

这是因为您正在尝试将字符串与int进行比较.根据 EL docs,第1.8.2节:

A {==,!=,eq,ne} B
■ If A==B, apply operator
■ If A is null or B is null return false for == or eq, true for != or ne.
■ If A or B is BigDecimal, coerce both A and B to BigDecimal and then:
    ■ If operator is == or eq, return A.equals(B)
    ■ If operator is != or ne, return !A.equals(B)
■ If A or B is Float or Double coerce both A and B to Double, apply operator
■ If A or B is BigInteger, coerce both A and B to BigInteger and then:
    ■ If operator is == or eq, return A.equals(B)
    ■ If operator is != or ne, return !A.equals(B)
If A or B is Byte, Short, Character, Integer, or Long coerce both A and B to Long, apply operator
■ If A or B is Boolean coerce both A and B to Boolean, apply operato
■ If A or B is an enum, coerce both A and B to enum, apply operatorr
■ If A or B is String coerce both A and B to String, compare lexically
■ Otherwise if an error occurs while calling A.equals(B), error
■ Otherwise, apply operator to result of A.equals(B)

您的测试的问题是您尝试将“$12,345”(字符串)与0(整数)进行比较.由于0是一个整数,因此在它们的文档(上图)中,它属于粗体If,其中A或B是整数.两者都试图强制进入Long,Java不会将String值“$12,345”转换为long.如果您将代码更改为以下任一项,您将看到它的工作原理:

字符串比较:

<c:set var="abc" value="$12,345" />
<c:choose>
    <c:when test="${abc ne '0'}"> <!-- Change Integer to String -->
        <c:out value="PASS"></c:out>
    </c:when>
    <c:otherwise>
        <c:out value="FAIL"></c:out>
    </c:otherwise> 
</c:choose>

整数比较:

<c:set var="abc" value="12345" /> <!-- Change String to Integer -->
<c:choose>
    <c:when test="${abc ne 0}">
        <c:out value="PASS"></c:out>
    </c:when>
    <c:otherwise>
        <c:out value="FAIL"></c:out>
    </c:otherwise> 
</c:choose>
网友评论