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

标签 jsf jsf-2 facelets el

这就是我想要生成的:

<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 解释器尝试将“图像/ map ”转换为数字,因此失败并出现 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> .

关于jsf - 尝试在 EL 中连接字符串时出现 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5993774/

相关文章:

validation - Primefaces tabView 在选项卡更改时执行表单验证

jsf - 执行托管 bean 操作方法后显示 alert()

eclipse - 如何在 Eclipse 中查看 jsf/facelets 元素的文档

java - JSF : How to replace "\\" in a string

tomcat - 异常找不到工厂 : javax. faces.application.ApplicationFactory

java - JSF网页包含

使用消息包的 JSF 本地化和多元化

JSF:数据更改后重新加载页面

java - PrimeFaces SelectOneMenu : Empty String is submitted as null

jsf - 如何在不使用 faces-config.xml 中的导航案例的情况下实现两个页面之间的 JSF 导航