【问题标题】:JSF Iterative composite component with customizable content具有可定制内容的 JSF 迭代复合组件
【发布时间】:2012-11-01 15:49:00
【问题描述】:

我想创建一个复合组件,可以将迭代元素的实际布局传递给复合组件。 这是一个简化的示例并且有效:

<composite:interface>
    <composite:attribute name="value"/>
</composite:interface>

<composite:implementation>
    <ul>
        <c:forEach var="i" items="#{cc.attrs.value}">
            <li>
                <h:outputText value="Test #{i.name}"/>
            </li>
        </c:forEach>
    </ul>

但我不希望 h:outputText 在组件中被硬编码。使用该组件时,我试图拥有这样的东西:

<my:list var="user" value="#{myBean.userList}">
  <h:outputText value="Test #{user.name}"/>
</my:list>

假设我必须使用var,但我不知道如何在我的组件中处理此问题并且正确访问子&lt;h:outputText value="Test #{user.name}"/&gt;。

【问题讨论】:

    标签: java jsf jsf-2 composite-component


    【解决方案1】:

    您可以使用&lt;composite:insertChildren /&gt; 来将定义的子组件“传递”到您的复合组件定义中。另外我推荐使用&lt;ui:repeat&gt; 而不是&lt;c:forEach&gt;,因为它是一个真正的迭代组件并且更适合JSF。以下是如何实现组件的示例:

    <composite:interface>
        <composite:attribute name="value"/>
    </composite:interface>
    
    <composite:implementation>
      <ul>
         <ui:repeat var="item" value="#{cc.attrs.value}">
           <li>
               <composite:insertChildren />
           </li>
         </ui:repeat>
      </ul>
    </composite:implementation>
    

    用法:

    <my:list value="#{myBean.userList}">
      <h:outputText value="Test #{item.name}"/>
    </my:list>
    

    【讨论】:

    • 谢谢,这行得通。但是硬编码var="item" 并不是很好。
    • 您可以尝试使用 但我不确定它是否会起作用。
    • 但是,硬编码的 var 名称是可用的,而且不是那么麻烦。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2013-11-15
    • 2014-02-21
    • 1970-01-01
    • 2012-07-07
    • 2015-09-05
    • 2017-02-13
    相关资源
    最近更新 更多