gpt4 book ai didi

java - JSF 用户界面 :fragment rendered performance

转载 作者:行者123 更新时间:2023-12-02 09:14:16 25 4
gpt4 key购买 nike

我有一组 jsf 组件,它们是从一组 excel 文件静态生成的(它们由业务人员更新)。每个生成的文件代表一个业务对象,其数据略有不同,并且它们都属于同一类。

为了动态渲染它,我找到的唯一解决方案是设置一堆 ui:fragment并在运行时分派(dispatch)到正确的组件:

<!-- 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 other 的参数ui: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");

最佳答案

是的,rendered属性在渲染时评估,而不是在构建时评估。是的,这是比较可怕的。想象一下,一个这样的条件需要 1 毫秒,评估其中 10 个条件总共需要花费 10 倍的时间,即 10 毫秒。如果分页表中有 10 个这样的组件,则 Web 应用程序加载时间将多花 0.1 秒。大约眨眼时间更长。但如果您不分页和/或使用 MSIE 作为引用浏览器,那么会花费更长的时间。您是否对数据进行分页并在适当的浏览器中进行测试?

您能做的最好的事情就是替换 <ui:fragment>通过 JSTL 标签,如 <c:if>/<c:choose>以便它在构建时而不是渲染时进行评估。或者,也可以在支持 bean 构造函数中而不是在 View 中构建组件树。

关于java - JSF 用户界面 :fragment rendered performance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6015609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com