【发布时间】:2011-08-26 07:33:34
【问题描述】:
我有一组 jsf 组件,它们是从一组 excel 文件中静态生成的(它们由业务人员更新)。每个生成的文件代表一个业务对象,其数据略有不同,并且都属于同一个类。
为了动态渲染,我找到的唯一解决方案是设置一堆ui:fragment 并在运行时调度到正确的组件:
<!-- IMPLEMENTATION -->
<composite:implementation>
<ui:fragment rendered="#{cc.attrs.type eq 'cartcred'}">
<limites:limites-cartcred limite="#{cc.attrs.limite}"/>
</ui:fragment>
<ui:fragment rendered="#{cc.attrs.type eq 'cdcp'}">
<limites:limites-cdcp limite="#{cc.attrs.limite}"/>
</ui:fragment>
<ui:fragment rendered="#{cc.attrs.type eq 'cheqpredatado'}">
<limites:limites-cheqpredatado limite="#{cc.attrs.limite}"/>
</ui:fragment>
<ui:fragment rendered="#{cc.attrs.type eq 'confirming'}">
<limites:limites-confirming limite="#{cc.attrs.limite}"/>
</ui:fragment>
<!-- many more lines -->
<!-- many more lines -->
<!-- many more lines -->
<ui:fragment rendered="#{cc.attrs.type eq 'contacorr'}">
<limites:limites-contacorr limite="#{cc.attrs.limite}"/>
</ui:fragment>
但我发现它的性能很糟糕。我坚持认为 JSF 只会渲染单个组件,但似乎它正在渲染所有它们并在运行时“隐藏”其他组件。
有没有更有效的方法来实现我的目标?我想根据有关业务类的运行时信息(很像 if-then-else)呈现单个组件,但我只能确定要在运行时呈现的组件是什么。
说明:
发生的情况是limites:limites* 引用的每个组件都是一个巨大的复杂页面,其中包含许多其他组件。在运行时,名为 type' will decide what component to render. But my tests show that if I only render one component, but leave the otherui:fragments 的参数(即使知道它们不会被渲染),它的渲染速度会比我移除组件时慢得多。
所以如果我的页面是这样的:
<composite:interface>
<composite:attribute name="type" required="true" />
<composite:attribute name="limite" required="true" />
</composite:interface>
<composite:implementation>
<ui:fragment rendered="#{cc.attrs.type eq 'cartcred'}">
<limites:limites-cartcred limite="#{cc.attrs.limite}"/>
</ui:fragment>
</composite:implementation>
即使参数相同,它的渲染速度也会比初始版本快很多(大约 10 倍)。我怀疑 JSF 会创建整个组件树,并且只有在运行时才会决定(取决于提供的参数)是否会相互渲染。
编辑
差不多了。我只需要包含我的复合组件 动态。我尝试评估一个 ELExpression,但没有奏效。我需要的是一种在组件创建中访问当前范围的方法,并使用它来生成正确的文件名:
//obviously, ELExpressions don't work here
Resource resource = application.getResourceHandler().createResource("file-#{varStatus.loop}.xhtml", "components/dynamicfaces");
【问题讨论】:
标签: java performance jsf components