在passtrough属性中使用f:selectItems var [英] Using f:selectItems var in passtrough attribute

查看:116
本文介绍了在passtrough属性中使用f:selectItems var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将表达式传递给JSF 2传递属性吗? 以下代码无法正常工作.表达式#{country.isoCode}未求值.

can I pass expressions to JSF 2 passthrough-attributes? the following code is not working. expression #{country.isoCode} is not evaluated.

<h:selectOneMenu value="#{bean.selectedCountry}" styleClass="selectlist">
   <f:selectItems 
            value="#{bean.countries}" var="country"
            itemLabel="#{country.countryName}" 
            pt:data-icon="flag flag-#{country.isoCode}"/>                
</h:selectOneMenu>

我正在使用命名空间

xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"

并进行引导选择.属性数据图标"用于显示图像.看到:

and bootstrap-select. attribute "data-icon" is used to show an image. see:

http://silviomoreto.github.io/bootstrap-select/#data-图标

渲染的输出:

<i class="glyphicon flag flag-"></i> 

推荐答案

基本上,Facelet模板中的所有位置都支持/评估EL.外部标签/属性.即使在HTML注释中,也有很多入门者倒下.所以那不是问题.

EL is basically supported/evaluated over all place in a Facelet template. Also outside tags/attributes. Even in HTML comments, where many starters then fall over. So that's not the problem.

不幸的是,您的特殊情况是设计使然". 在呈现第一个<option>元素之前,<f:selectItems>仅被完全解析一次,并变成一个迭代器,在该迭代器中将评估所有EL表达式.然后,该组件将在渲染<option>元素时对其进行迭代,在此期间将评估所有传递属性.但是,由于在创建迭代器时已经对var进行了 评估,因此在呈现传递属性期间它在任何地方都不可用,并最终评估为空字符串.

Your particular case is, unfortunately, "by design". Before rendering the first <option> element, the <f:selectItems> is is wholly parsed only once and turned into an iterator during which all EL expressions will be evaluated. Then, the component will iterate over it while rendering <option> elements during which all passthrough attributes will be evaluated. However, as the var was already evaluated during creating the iterator, it isn't available anywhere during rendering the passthrough attributes and ultimately evaluates to an empty string.

修复该问题,需要对<f:selectItems>的标准JSF实现进行一些更改.我不确定JSF的家伙是否会为此全力以赴,但是您始终可以尝试创建问题.

Fixing that would require quite some changes in standard JSF implementation of <f:selectItems>. I'm not sure if JSF guys would be all ears for that, but you can always try to create an issue.

您可以通过在<c:forEach>的帮助下在视图构建期间实际创建多个<f:selectItem>实例来解决此问题.

You can work around this by creating physically multiple <f:selectItem> instances during view build time, with help of <c:forEach>.

<h:selectOneMenu ...>
    <c:forEach items="#{bean.countries}" var="country">
        <f:selectItem 
            itemValue="#{country}" 
            itemLabel="#{country.countryName}" 
            pt:data-icon="flag flag-#{country.isoCode}" />   
    </c:forEach>             
</h:selectOneMenu>

另请参见:

  • JSF2 Facelets中的JSTL ...有意义吗?
  • See also:

    • JSTL in JSF2 Facelets... makes sense?
    • 这篇关于在passtrough属性中使用f:selectItems var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆