尝试连接EL中的字符串时发生NumberFormatException异常 [英] NumberFormatException when trying to concatenate a String in EL
本文介绍了尝试连接EL中的字符串时发生NumberFormatException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我尝试生成的内容:
<div class="marker" style="background:transparent url('/myApp/faces/javax.faces.resource/1.png?ln=images/map') no-repeat center top;"></div>
<div class="marker" style="background:transparent url('/myApp/faces/javax.faces.resource/2.png?ln=images/map') no-repeat center top;"></div>
<div class="marker" style="background:transparent url('/myApp/faces/javax.faces.resource/3.png?ln=images/map') no-repeat center top;"></div>
etc...
以下是我的代码:
<ui:repeat value="#{myBean.items}" var="item" varStatus="status">
<h:panelGroup layout="block" styleClass="marker" style="background:transparent url(#{resource['images/map:'+(status.index+1)+'.png']} no-repeat center top;"/>
</ui:repeat>
由于EL解释器试图将"图像/映射"转换为数字,因此出现NumberFormatException失败。找了很久,我发现+只是用来加数字的。有什么办法可以达到预期的效果吗?
推荐答案
EL不将+
运算符识别为字符串串联运算符。+
运算符在EL中最终仅用于对数字求和。您需要使用<ui:param>
创建另一个表达式变量,在该变量中,您只需在值中内联EL表达式,然后在最终的表达式中使用它。
<ui:repeat value="#{myBean.items}" var="item" varStatus="status">
<ui:param name="key" value="images/map#{status.index + 1}.png" />
<h:panelGroup layout="block" styleClass="marker" style="background:transparent url(#{resource[key]} no-repeat center top;"/>
</ui:repeat>
注意:如果您使用的是JSP而不是Facelets,则应该使用JSTL<c:set>
而不是Facelets<ui:param>
。
这篇关于尝试连接EL中的字符串时发生NumberFormatException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文