Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic ui:include inside ui:repeat. Is there a simple solution?

Tags:

java

jsf

facelets

I want to dynamically pick a facelet to render some item in my data list. The first try would be:

<ui:repeat value="#{panels}" var="panel">
  <ui:include src="#{panel.facelet}">
</ui:repeat>

But it won't work since src of ui:include is evaluated too early. The facelet information is truly dynamic, so I cannot use c:forEach (not really recommended to mix with facelets either). I guess it all boils down to finding a component based ui:include alternative.

Is there such thing or I need to write my own?

like image 347
mrembisz Avatar asked Jul 29 '10 12:07

mrembisz


2 Answers

I think I've found that relatively simple solution you've been looking for.

I too started with a ui:include inside a ui:repeat like yours, but I accepted that I had to use a c:forEach, and the c:forEach worked great for dynamically getting a different set of xhtml/components to include even with user interaction changing things in the View like I think you have. It looked like this:

<c:forEach var="thing" items="#{view.things}">
        <ui:include src="#{thing.renderComponent}">
            <ui:param name="thing" value="#{thing}"/>
        </ui:include>
</c:forEach>

However, my ui:param wasn't working - every component I included was passed the same "thing"/Object even though we had successfully used different things/Objects to dynamically include different components.

That's when I found this post which inspired me to wrap my ui:include in a f:subview. And now everything is working great with the following code:

<c:forEach var="thing" items="#{view.things}" varStatus="loop">
    <f:subview id="thing_#{loop.index}">
        <ui:include src="#{thing.renderComponent}">
            <ui:param name="thing" value="#{thing}"/>
        </ui:include>
    </f:subview>
</c:forEach>
like image 174
Mike Saris Avatar answered Sep 22 '22 23:09

Mike Saris


c:forEach will solve it, why can't you use it?

Interesting article regarding that issue: https://rogerkeays.com/jsf-c-foreach-vs-ui-repeat

like image 31
DuduAlul Avatar answered Sep 24 '22 23:09

DuduAlul