【问题标题】:NumberFormatException when trying to concatenate a String in EL尝试在 EL 中连接字符串时出现 NumberFormatException
【发布时间】:2011-05-13 14:56:10
【问题描述】:

这就是我想要生成的:

<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 失败。经过一番搜索,我发现 + 仅用于添加数字。任何想法如何达到预期的结果?

【问题讨论】:

标签: jsf jsf-2 facelets el


【解决方案1】:

EL 无法将+ 运算符识别为字符串连接运算符。 + 运算符在 EL 中最终仅用于求和。您需要使用&lt;ui:param&gt; 创建另一个表达式变量,其中您只需在值中内联 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 &lt;c:set&gt; 而不是 Facelets &lt;ui:param&gt;

【讨论】:

  • 刚刚意识到我没有对此投赞成票。我知道它已经过时了,但这是我不断回想的答案。 :)
  • 太棒了,非常有用@Baluc
【解决方案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>

【讨论】:

  • html 中字符串连接的终极解决方案,我很难找到这个简单的解决方案,而不是@Kalanj Djordje Djordje,我还更新了我的问题,这个问题很久没有回答了。
猜你喜欢
  • 2014-05-28
  • 1970-01-01
  • 2012-06-20
  • 2012-07-03
  • 2010-09-22
  • 2020-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多