比较EL表达式中的数字似乎不起作用 [英] Comparing numbers in EL expression does not seem to work
问题描述
在JSP中,我想比较两个变量
In JSP, I want to compare two variables
如果我这样做:
<c:set var="pagerTotDisp" value="9"/>
<c:if test="${pagerTotDisp > 8}">
<span>pagerTotDisp above 8</span>
</c:if>
按预期显示pagerTotDisp 8以上
It displays "pagerTotDisp above 8" as expected
<c:set var="TotalPages" value="10"/>
<c:if test="${TotalPages > 2}">
<span>TotalPages above 2</span>
</c:if>
按预期显示pagerTotDisp 8以上
It displays "pagerTotDisp above 8" as expected
但是如果我这样做
<c:set var="pagerTotDisp" value="9"/>
<c:set var="TotalPages" value="10"/>
<c:if test="${TotalPages < pagerTotDisp}">
<span>This condition is not true. This text should not be displayed</span>
</c:if>
显示此条件不成立。不应显示此文本。
It displays "This condition is not true. This text should not be displayed".
发生了什么事?
JSP是否能够在同一条件下处理两个变量?
What's going on? Is that JSP not being able to handle two variables in a same condition??
谢谢
推荐答案
您正在对< c:set> $的
更远的数字位置。值
属性中的值进行硬编码C $ C>。 < c:set>
将硬编码值视为 String
。因此EL也将它们评估为 String
。按字典顺序, 9
大于 10
,因为 9
是在比$ code> 1
You're hardcoding the value in the value
attribute of <c:set>
. The <c:set>
treats the hardcoded values as String
. EL is therefore also evaluating them as String
. Lexicographically, 9
is greater than 10
, because 9
is at a further numerical position than 1
.
有两种解决方法:
-
通过EL表达式设置值。它将被解释为
Long
而不是String
。
<c:set var="pagerTotDisp" value="${9}" />
<c:set var="TotalPages" value="${10}" />
或者,使用 < fmt:parseNumber>
,这将是是唯一的解决方案,如果你有来自其他地方的 String
变量,你无法控制。
Or, use <fmt:parseNumber>
, which would be the only solution if you have those as String
variables from elsewhere which you have no control over.
<fmt:parseNumber var="pagerTotDisp" value="9" />
<fmt:parseNumber var="TotalPages" value="10" />
这篇关于比较EL表达式中的数字似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!