尝试在 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。经过一番搜索,我发现 + 只能用于添加数字。有什么想法如何达到预期的结果吗?
This is what I'm trying to generate:
<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...
Here's my code:
<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>
This is failing with a NumberFormatException because the EL interpreter attempts to convert "images/map" to a number. After searching quite a bit, I found that + is only for adding numbers. Any ideas how to achieve the desired results?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
EL 无法将
+
运算符识别为字符串连接运算符。+
运算符在 EL 中最终仅用于对数字求和。您需要使用
创建另一个表达式变量,其中通过在值中内联 EL 表达式来连接各部分,然后在最终表达式中使用它。注意:如果您使用 JSP 而不是 Facelets,则应使用 JSTL
而不是 Facelets
。EL does not recognize the
+
operator as String concatenation operator. The+
operator is in EL ultimately to sum numbers only. You need to use<ui:param>
to create another expression variable wherein you concatenate the parts by just inlining the EL expression in the value and then use it in the final expression instead.Note: if you were using JSP instead of Facelets, you'd have used JSTL
<c:set>
instead of Facelets<ui:param>
.您可以使用
concat
函数来连接 JSP EL 中的字符串。在你的例子中,这将是:You can use
concat
function to concatenate strings in JSP EL. In your example that would be: