JSF Iterative composite component with customizable content

眉间皱痕 提交于 2019-12-05 02:23:27

问题


I want to create a composite component where to acutal layout of of iterating elements can be passed to the composite. This is a simplified example and works:

<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>

But I don't want the h:outputText to be hardcoded in the component. When using the component I'm trying to have something like this:

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

Is assume that I have to use a var, but I don't know how to handle this in my component and access the child <h:outputText value="Test #{user.name}"/> correctly.


回答1:


You could use <composite:insertChildren /> to be able to "pass" the child components defined to your composite component definition. Also I recommend using <ui:repeat> instead of <c:forEach> because it's a real iterative component and better suited for JSF. Here is an example how to implement your component:

<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>

Usage:

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


来源:https://stackoverflow.com/questions/13356967/jsf-iterative-composite-component-with-customizable-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!