如何将 JSTL 标记的返回值返回到 EL 语句中?
基本上我有一个自定义标签来为我处理查询 java 对象。
<c:set var="profit">
<ct:get-profit transaction="${transaction}"/>
</c:set>
现在的问题是我想使用该值(它是 if 语句中的一个浮点数,我这样做:
<c:when test="${profit > 0}">
当我这样做时,虽然我最终得到了以下错误。
javax.el.ELException: Cannot convert -141.75 of type class java.lang.String to class java.lang.Long
我不知道如何才能完成这项工作我的印象是 JSTL 会为你处理选角,这是错误的吗?谢谢,你会如何制作这个作品?
Basically I have a custom tag that handles querying a java object for me.
<c:set var="profit">
<ct:get-profit transaction="${transaction}"/>
</c:set>
Now the problem is that I want to use that value (which is a float in an if statement, which I do as so:
<c:when test="${profit > 0}">
When I do that though I end up getting the following error.
javax.el.ELException: Cannot convert -141.75 of type class java.lang.String to class java.lang.Long
I have no idea how I can make this work. I was under the impression that JSTL's would handle casting for you, is that incorrect? Either way, how would you go about making this work? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用
0.00
而不是0
吗?
。您必须这样做的原因是因为 EL 解析器将 0 视为 Long。然而,“0.00”被视为浮点数。
Can you try doing
0.00
instead of0
?<c:when test="${profit > 0.00}">
.The reason you have to it is because 0 is getting treated as Long by the EL parser. However, "0.00" is getting treated as a float.
您在
的 body 中设置的所有内容都会通过Object#toString()< 隐式转换为
String
/代码>。您希望使用其value
属性来保持类型不变。我建议用 EL 函数替换
标签。由于此标记似乎不呈现任何标记,因此您可以使用 EL 函数来做到同样好(甚至更好)。有关
如何配置此类功能的更详细示例,请检查 这个答案。
Everything which you set in the body of
<c:set>
is implicitly converted toString
byObject#toString()
. You'd like to use itsvalue
attribute instead which keeps the type unchanged.I'd suggest to replace the
<ct:get-profit>
tag by an EL function. Since this tag doesn't seem to render any markup, you could do it as good (and better) with an EL function.in combination with
For a more detailed example how to configure such a function, check the bottom of this answer.