<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 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評(píng)論 :: 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 閱讀(1545) 評(píng)論(0)  編輯  收藏 所屬分類: Seam
    主站蜘蛛池模板: 777成影片免费观看| 免费欧洲毛片A级视频无风险| 两性刺激生活片免费视频| 亚洲色偷拍区另类无码专区| 亚洲人成伊人成综合网久久| 成人a毛片视频免费看| 韩国免费一级成人毛片| 亚洲国产成人AV网站| 国产成人午夜精品免费视频| 亚洲福利视频网站| 香蕉免费一区二区三区| AV在线播放日韩亚洲欧| 亚洲一区二区三区免费观看| 亚洲国产精品久久久久网站| 精品久久久久久无码免费| 亚洲Av无码国产情品久久 | 色在线亚洲视频www| 97青青草原国产免费观看| 久久精品国产精品亚洲艾草网| 久久性生大片免费观看性| 亚洲二区在线视频| 国产亚洲美日韩AV中文字幕无码成人| jizz中国免费| 亚洲啪啪AV无码片| 国产在线观看免费av站| 亚洲高清国产拍精品26U| 久久成人免费播放网站| 久久精品国产亚洲AV果冻传媒| 色吊丝最新永久免费观看网站| 亚洲精品无码成人| 国产成人免费福利网站| 国产精品九九久久免费视频| 亚洲中文字幕一二三四区| 国产免费黄色大片| 久草免费在线观看视频| 亚洲av无码片vr一区二区三区| 国产一区二区三区免费视频| 精品香蕉在线观看免费| 99久久人妻精品免费二区| aa级毛片毛片免费观看久| 黄色网址在线免费观看|