[英]Dynamic ui:include inside ui:repeat. Is there a simple solution?
I want to dynamically pick a facelet to render some item in my data list.我想动态选择一个 facelet 来呈现我的数据列表中的某些项目。 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.但它不会工作,因为 ui:include 的 src 评估过早。 The facelet information is truly dynamic, so I cannot use c:forEach (not really recommended to mix with facelets either).
facelet 信息是真正动态的,所以我不能使用 c:forEach(也不推荐与 facelets 混合使用)。 I guess it all boils down to finding a component based ui:include alternative.
我想这一切都归结为找到一个基于组件的 ui:include 替代方案。
Is there such thing or I need to write my own?有这样的事情还是我需要自己写?
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 ac: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.我也开始使用 ui:include 在 ui:repeat 中,就像你的一样,但我接受我必须使用 ac:forEach,并且 c:forEach 非常适合动态获取一组不同的 xhtml/components 以包含即使与用户交互改变视图中的事物,就像我认为的那样。 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.但是,我的 ui:param 不起作用 - 我包含的每个组件都传递了相同的“事物”/对象,即使我们已成功使用不同的事物/对象来动态包含不同的组件。
That's when I found this post which inspired me to wrap my ui:include in af:subview.那时我发现这篇文章启发了我将我的 ui:include 包含在 af: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>
c:forEach will solve it, why can't you use it? c:forEach 都会解决的,为什么不能用呢?
Interesting article regarding that issue: https://rogerkeays.com/jsf-c-foreach-vs-ui-repeat关于该问题的有趣文章: https : //rogerkeays.com/jsf-c-foreach-vs-ui-repeat
I remember trying to do something similar using a custom tag and FaceletHandler etc. In the end all the little rendering-time issues made it not worth the effort.我记得尝试使用自定义标签和 FaceletHandler 等做类似的事情。最后,所有小的渲染时间问题都不值得付出努力。 Mind you I was ( and still am :-(... ) using facelets for jsf 1.1, so not sure if this is better in the later versions.
请注意,我曾经(并且仍然是 :-(... )在 jsf 1.1 中使用 facelets,所以不确定这在以后的版本中是否更好。
How dynamic / how many different facelets do you have to deal with?您必须处理多少动态/多少不同的facelets? The reason I ask is that you could (to borrow a term from the compiler wizards) unroll your loop.
我问的原因是你可以(从编译器向导那里借用一个术语)展开你的循环。 Instead of
代替
<ui:repeat value="#{panels}" var="panel">
<ui:include src="#{panel.facelet}">
</ui:repeat>
You could do你可以做
<custom:panelOneFacelet rendered="#{hasPanel1}" />
<custom:panelTwoFacelet rendered="#{hasPanel2}" />
<!-- etc... -->
And in your facelet, you would have something like :在你的 facelet 中,你会有类似的东西:
<c:if test="#rendered" >
<!-- EVERYTHING IN THE FACELET HERE!!!-->
</c:if>
This sort of low-tech approach is fine for a small controlled set, but if you have a very large and varying set of facelets this may not work.这种低技术含量的方法适用于小型受控组,但如果您有一组非常大且变化多端的小面,这可能行不通。
Might I ask why, b/c sometimes with an understanding of the high-level, SO gurus may suggest much simpler ideas for accomplishing the same goal我可能会问为什么 b/c 有时了解高级别的,SO 大师可能会提出更简单的想法来实现相同的目标
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.