<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

    --- 博客已遷移至: http://www.sealyu.com/blog

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks

    c:forEach vs ui:repeat in Facelets

    Posted by Roger Keays, 7 June 2007, 12:28 PM

    This is probably one of the most frequently asked questions on the Facelets mailing list. Why doesn't my c:forEach tag work correctly? Unfortunately, there are may ways to misuse the jstl tags available in Facelets, so the answer isn't always simple. Here is an explanation of the differences between c:forEach and ui:repeat, along with some examples which will hopefully save you some headaches.

    TagHandlers vs Components

    The most important thing to understand about the jstl tags in Facelets is that they do not represent components and never become a part of the component tree once the view has been built. Rather, they are tags which are actually responsible for building the tree in the first place. Once they have done their job they expire, are no more, cease to be, etc etc.

    Here is a table of the semantics of several common tags. I just discovered, reading the Facelets code, that validators and converters are classified separately. I had always thought they were just tag handlers, but I imagine they behave in much the same way.

    TagHandlers
    Components
    Other

    c:forEach
    c:choose
    c:set
    c:if
    f:facet
    f:actionListener
    f:valueChangeListener
    ui:include
    ui:decorate
    ui:composition
    any custom tag file

    ui:repeat
    ui:fragment
    ui:component
    f:view
    f:verbatim
    f:selectItems
    h:inputText
    h:datatable
    any custom UIComponent

    f:validator
    f:converter

    One of the problems here is that there is no naming convention to indicate which tags correspond to which constructs. You've just got to know, or find out.

    When is the view built?

    Now that you understand that tag handlers are only effective when the tree is built, the next logical question should be well, when is tree built?

    The short answer is that a new view is built for every request which is not a postback. During a postback, the view is reconstructed from saved state. Quite confusing, and not very obvious I know, but there you have it.

    Common laments

    The most common pitfalls are either with the JSF lifecycle, EL evaluation or combining tag handlers with components.

    My c:if always evaluates to false

    <h:dataTable values="${numbers}" var="number">
    <h:column>
    <c:if test="${number > 5}">
    <h:outputText value="${number}"/>
    </c:if>
    </h:column>
    </h:datatable>

    Yes, the c:if is always evaluating to false! But it is only ever evaluated once - when the tree is built. The h:outputText component never makes it into the tree. Solution: replace the c:if with:

    <ui:fragment rendered="${number > 5}"> ... </ui:fragment>

    You could also use the rendered attribute on the h:outputText component in this example.

    My ui:include fails inside ui:repeat

    <ui:repeat value="#{bean.items}" var="item">
       <ui:include src="#{item.src}"/>
    </ui:repeat>

    The EL for the ui:include is evaluated when the view is built and is invalid since it relies on a variable only made available by the ui:repeat during rendering. Use c:forEach in this case.

    My recursive tag never stops

    myTag.xhtml:
    <ul>
    <ui:repeat value="${item.children} var="child">
    <li><eg:myTag item="${child}"/></li>
    </ui:repeat>
    </ul>

    The stop condition in this recursion is supposed to be when you run out of children. The problem is that the custom eg:myTag is just a tag handler, like a special version of ui:include. When the view is built, the ui:repeat has no influence on the building process and can't stop the recursion. Use c:forEach here instead of ui:repeat. Or better still, convert your tag file to a real UIComponent.

    You might also recognise that the ${child} EL expression is meaningless during build time in this example, unless you use c:foreach.

    My list doesn't change size after deleting or adding an item

    <h:form>
    <c:forEach items="${list}" var="item">
    <h:outputText value="${item.name}"/><br/>
    </c:forEach>
    <h:commandButton value="Create new item" action="..."/>
    <h:commandButton value="Delete an item" action="..."/>
    </h:form>

    When your view was built you only had, say, 5 items. If you post back to this view and add or delete an item, your view still has 5 h:outputText components in it since it was restored from saved state. In this simple case, you should use ui:repeat and your tree will always contain one h:ouputText component which is iterated over with differing values of ${item}.

    If you rely on using c:forEach to dynamically include different form components you could run into difficulty. Always try to think about what the resulting tree looks like and remember it doesn't change on a postback.

    Suggestions for the future

    Probably the best relief to this problem would be to come up with a better syntax or naming convention to distinguish between tag handlers and components. I imagine you could also improve compilation performance if you did this.

    Secondly, we need better terminology. I've used the terms tag handler and component in this blog which isn't too bad. The Facelets' FAQ [1] uses the terms build-time tags and render-time tags which is a bit misleading because render-time tags (components) are involved in all phases of the JSF lifecycle, not just the Render View phase.

    Whatever happens, tag handlers are very useful (would you use facelets without ui:decorate?) so let's not get rid of them.

    References

    [1] http://wiki.java.net/.../FaceletsFAQ#Why_doesn_t_my_c_if_ui_repeat_ui

    posted on 2009-04-13 08:52 seal 閱讀(1537) 評論(0)  編輯  收藏 所屬分類: Seam
    主站蜘蛛池模板: 午夜男人一级毛片免费| 人成免费在线视频| 全黄a免费一级毛片人人爱| 精品久久久久久国产免费了 | 国产成人免费视频| 亚洲精品高清在线| 亚洲AV无码专区在线观看成人| 精品亚洲一区二区三区在线播放| 最近2019免费中文字幕6| 国产亚洲精品欧洲在线观看| 亚洲AV无码专区亚洲AV伊甸园| 午夜男人一级毛片免费 | 国产zzjjzzjj视频全免费 | 综合亚洲伊人午夜网| 国产成人精品免费视频大| 思思久久99热免费精品6| 亚洲一区二区三区免费视频| 91在线老王精品免费播放| 最新亚洲人成无码网www电影| 亚洲免费视频在线观看| 亚洲av无码不卡私人影院| 免费手机在线看片| 亚洲精品无码专区久久久| 女人张开腿给人桶免费视频| 日本卡1卡2卡三卡免费| 污网站免费在线观看| 亚洲日本乱码卡2卡3卡新区| 成全视频在线观看免费高清动漫视频下载 | 久久久久亚洲AV片无码| 亚洲国产成人乱码精品女人久久久不卡 | 99国产精品免费视频观看| 亚洲乱码卡三乱码新区| 国产资源免费观看| 五月婷婷综合免费| 久久国产精品萌白酱免费| 国产精品免费大片一区二区| 亚洲av综合av一区二区三区| wwwxxx亚洲| 亚洲中文字幕无码专区| 16女性下面扒开无遮挡免费| 性色av极品无码专区亚洲|