尝试在 EL 中连接字符串时出现 NumberFormatException

2023-12-28

这就是我想要生成的:

<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>.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试在 EL 中连接字符串时出现 NumberFormatException 的相关文章

随机推荐