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

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

问题描述

我可以将表达式传递给 JSF 2 passthrough-attributes 吗?以下代码不起作用.表达式 #{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> 

推荐答案

EL 基本上在 Facelet 模板的所有地方都得到支持/评估.也在标签/属性之外.即使在 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.

不幸的是,您的特殊情况是设计使然".渲染第一个 元素之前, 只被完全解析一次,并在将评估所有 EL 表达式.然后,组件将在渲染 元素时对其进行迭代,在此期间将评估所有传递属性.然而,由于 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.

解决这个问题需要对 的标准 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.

您可以通过在视图构建期间在 的帮助下创建物理上的多个 实例来解决此问题.>

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>

另见:

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