سؤال

            <ui:repeat  value="#{bean.allResults}" var="result">
                <ui:fragment rendered="#{result.condition1}">
                    <ui:include src="/item1.xhtml">
                </ui:fragment>
                <ui:fragment rendered="#{result.condition2}">
                    <ui:include src="/item2.xhtml">
                </ui:fragment>
                <ui:fragment rendered="#{result.condition3}">
                    <ui:include src="/item3.xhtml">
                </ui:fragment>
                <ui:fragment rendered="#{result.condition4}">
                    <ui:include src="/item4.xhtml">
                </ui:fragment>
            </ui:repeat>

I have a couple of questions regarding best practices when using JSF/JSTL/Facelets
I have a JSF page that looks like the code above. Let's assume this loop returns 10 results and for each result only 1 condition evalutates to true. So the user will see 10 results on the html page once it loads.

  1. Eventhough only 10 items will display in html the component tree will contain 40 items, it would contain those where rendered evaluated to false. Is the behavior described above accurate?

  2. If 1 is true and we could have lots of hidden components in the component tree is that a performance concern, let's say it was 400 items instead of 40?

  3. In Faceletes or JSF is there a conditional tag handler which behaves like the c:if and won't include the component in the tree? Or should I use c:if as a last resort? I'm trying to avoid using JSTL.

Thanks.

هل كانت مفيدة؟

المحلول

Eventhough only 10 items will display in html the component tree will contain 40 items

You are correct that the included components will be part of the component tree even if rendered is false. However, <ui:repeat> does not duplicate its content in the component tree, but will iterate over its child components repeatedly in each phase of the JSF lifecycle. Therefore, the component tree will only contain 4 "item" components.

If 1 is true and we could have lots of hidden components in the component tree is that a performance concern, let's say it was 400 items instead of 40?

It can be, as documented in a related answer, but it takes a lot of components to be noticeable. (I did a benchmark a while back about this. I don't have the numbers at hand, but IIRC, adding 600 not-rendered components added 300 ms to web application response time.)

is there a conditional tag handler which behaves like the c:if and won't include the component in the tree? Or should I use c:if as a last resort?

TagHandlers with value expressions have all sorts of limitations, so I recommend staying clear of them where possible. But yes, to actually prevent inclusion in the component tree, <c:if> is what I use, too.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top