【问题标题】:How to add tooltip to f:selectItems如何将工具提示添加到 f:selectItems
【发布时间】:2014-08-26 17:03:09
【问题描述】:

例如 f:selectItems 组件在某些 JSF 版本中不支持 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>

这样做,用上面的替换后者,我得到"&lt;f:converter&gt; Parent not an instance of ValueHolder: javax.faces.component.html.HtmlPanelGroup" Facelet TagExceptions

【问题讨论】:

    标签: jsf tooltip selectonemenu


    【解决方案1】:

    是否可以使用 JSFC 将 JSF 组件替换为纯 HTML 组件并执行类似的操作

    不。最终,这种具有jsfc 属性的 HTML 元素将在 JSF 组件树中变成真正的 JSF 组件,并且只有相关组件支持的属性会被解析并设置为组件属性。 title 属性不在UISelectItem 组件支持的属性中。我不确定“某些版本的 JSF”到底是什么意思。标准的 JSF API 一开始就不支持它。 JSF spec issue 529 描述了这个缺点,目前仍处于开放状态。

    如果您使用的是 JSF 2.2,请使用直通属性。您只需将&lt;f:selectItems&gt; 替换为&lt;c:forEach&gt;&lt;f:selectItem&gt;,另见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。如果不能升级,基本上需要&lt;h:selectOneMenu&gt;的自定义渲染器。 在创建自定义渲染器时,您可以使用 UISelectItem 类的未使用(!)description 属性。我之前在&lt;p:selectManyCheckbox&gt;:Primefaces tooltip for p:selectManyCheckbox or other p:selectMany*/One* 的类似问题上回答过这个问题。

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

    需要注意的是,为&lt;h:selectOneMenu&gt; 创建自定义渲染器很痛苦,特别是如果您打算独立于 JSF 实现。理论上,自定义ResponseWriter 应该能够捕捉到这一点,但不幸的是,&lt;h:selectOneMenu&gt; 仅在编写&lt;option&gt; 时通过自身,而不是有问题的UISelectItem

    【讨论】:

    • 谢谢。非常深刻的答案,确实我的意思是
    【解决方案2】:

    就我而言(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>
    

    【讨论】:

      猜你喜欢
      • 2011-06-06
      • 2010-11-12
      • 2019-01-04
      • 2012-11-02
      • 2017-08-03
      • 2017-06-03
      • 2022-01-07
      • 1970-01-01
      相关资源
      最近更新 更多