如何给 f:selectItems 添加提示信息

9
例如,在某些版本的JSF中,f:selectItems组件不支持title属性。
使用JSFC,是否可以将JSF组件替换为它们的纯HTML对应项,并执行以下操作?
   <select jsfc="h:selectOneMenu" value="#{cc.data}">
     <option jsfc="f:selectItems" value="${cc.listItems}" var="item" title="#{item.tooltip}"></option>
   </select>

代替
   <h:selectOneMenu value="#{cc.data}">
     <f:selectItems value="#{cc.listItems}" />
   </h:selectOneMenu>

按照这种方式操作,将后者替换为前者,我遇到了 " <f:converter> Parent not an instance of ValueHolder: javax.faces.component.html.HtmlPanelGroup " 的 Facelet TagExceptions 错误。

2个回答

14
不可能。最终,带有“jsfc”属性的HTML元素将被转换为JSF组件树中的真正JSF组件,并且仅受该组件支持的属性将被解析并设置为组件属性。 “title”属性不在UISelectItem 组件支持的属性之列。我不确定你所说的“一些JSF版本”是什么意思。标准JSF API首先就不支持它。JSF规范问题529描述了这个缺陷,目前仍然未解决。如果您使用JSF 2.2,请使用透传属性。您只需要用<c:forEach><f:selectItem>替换<f:selectItems>,详见 Using f:selectItems var in passtrough attribute
<... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">

<c:forEach value="#{bean.items}" var="item">
    <f:selectItem itemValue="#{item}" a:title="#{item.tooltip}" />
</c:forEach>

根据您的问题历史记录,您似乎还没有使用JSF 2.2。如果您无法升级,则基本上需要为<h:selectOneMenu>创建自定义渲染器。 在创建自定义渲染器时,您可以利用UISelectItem类的未使用(!) description属性。我曾在回答类似问题时提到过此方法,该问题针对的是<p:selectManyCheckbox>Primefaces tooltip for p:selectManyCheckbox or other p:selectMany*/One*

<f:selectItems ... var="item" itemDescription="#{item.tooltip}" />

请注意,为<h:selectOneMenu>创建自定义渲染器是一件痛苦的事情,尤其是如果你希望独立于JSF实现。理论上,自定义ResponseWriter应该能够捕获它,但不幸的是,<h:selectOneMenu>只在写入<option>时传递自身,而不是相关的UISelectItem


非常感谢您深刻的回答,我确实指的是不支持任何小于2.2版本的属性通行功能,组件上缺少的普通标题属性是一个规范缺陷,正如您所说的。 - user3280015

1
在我的情况下(JSF 2.2 / Mojarra 2.2.14),itemDescription 可以直接使用,即:
<c:forEach items="#{bean.items}" var="item">
    <f:selectItem itemValue="#{item}" itemLabel="#{item}" itemDescription="#{item.tooltip}" />
</c:forEach>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接