这就是我想要产生的:
<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>
这与NumberFormatException失败,因为EL解释器尝试将“images / map”转换为数字。经过相当多的搜索后,我发现+仅用于添加数字。任何想法如何达到预期的效果?
答案 0 :(得分:13)
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>
。
答案 1 :(得分:2)
您可以使用concat
函数来连接JSP EL中的字符串。在您的示例中:
<ui:repeat value="#{myBean.items}" var="item" varStatus="status">
<h:panelGroup layout="block" styleClass="marker" style="background:transparent url(#{resource['images/map:'.concat( (status.index+1).concat('.png')]} no-repeat center top;"/>
</ui:repeat>