在组合组件中迭代时公开列表的项目 [英] Expose items of a list while iterating within composite component

查看:121
本文介绍了在组合组件中迭代时公开列表的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历复合组件中的项目列表.我想公开列表中的每个项目,以便可以在此复合组件的子组件中使用它们,以创建用于显示列表中所有项目的模板.

I'm iterating over a list of items in composite component. I want to expose each item of the list so that they could be used within the child component of this composite component, to create a template for how to display all items in the list.

这是我的复合组件实现:

Here is my Composite Component implementation:

customList.xhtml

<ui:component
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:cc="http://java.sun.com/jsf/composite"
    xmlns:ui="http://java.sun.com/jsf/facelets">    

    <cc:interface>
    </cc:interface>

    <cc:implementation>       
        ...
        ...   
        <ui:repeat value="#{listRetriever.list}" var="item">
            <cc:insertChildren />
        </ui:repeat>

    </cc:implementation>
</ui:component>

现在,我想在页面中定义复合组件的子组件时使用#{item}(类似于h:dataTableui:repeat).

Now I want to make use of #{item} while defining the child component of composite component in my page (similar to h:dataTable or ui:repeat).

  <my:customList>
        #{item}                <!--- over here--->
  </my:customList>

推荐答案

<ui:repeat>内部的<cc:insertChildren>上将不起作用,因为<cc:insertChildren>是在视图构建期间而不是在视图渲染期间进行评估的. #{item}仅在视图渲染期间运行,因为<ui:repeat>仅在视图渲染期间运行.

That won't work with <cc:insertChildren> inside <ui:repeat> as the <cc:insertChildren> is evaluated during view build time, not during view render time. The #{item} is only available during view render time as the <ui:repeat> runs during view render time only.

当您使用JSTL <c:forEach>时它会起作用,因为它也会在视图构建期间像<cc:insertChildren>本身那样进行评估.

It'll work when you use JSTL <c:forEach> instead as it's also evaluated during view build time like the <cc:insertChildren> itself.

xmlns:c="http://java.sun.com/jsp/jstl/core"
...
<c:forEach items="#{listRetriever.list}" var="item">
    <cc:insertChildren />
</c:forEach>

当在另一个重复的JSF组件中依次使用复合时,请小心,否则将无法使用.另请参见 JSF2 Facelets中的JSTL ...有意义吗?.

Be careful when you use the composite in turn inside another repeating JSF component, it wouldn't work then. See also JSTL in JSF2 Facelets... makes sense?.

除了创建扩展UIDataUIRepeat的自定义组件外,另一种方法是使用<ui:decorate>.

An alternative, apart from creating a custom component which extends UIData or UIRepeat, is using <ui:decorate> instead.

首先将customList.xhtml用作普通模板:

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
>
    ... 
    ...  
    <ui:repeat value="#{list}" var="item">
        <ui:insert name="item" />
    </ui:repeat>
    ...
    ...
</ui:composition>

然后您可以按以下方式使用它:

Then you can use it as follows:

<ui:decorate template="customList.xhtml">
    <ui:param name="list" value="#{bean.list}" />
    <ui:define name="item">
        #{item}
    </ui:define>
</ui:decorate>

这篇关于在组合组件中迭代时公开列表的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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