gpt4 book ai didi

java - 帮助理解JSF对托管bean的多次调用

转载 作者:行者123 更新时间:2023-11-30 05:04:49 25 4
gpt4 key购买 nike

我正在使用 h:datatable,这是我的代码的相关行:

 <h:dataTable value="#{account.latestIncomes}" var="mov" >
.....
</h:dataTable>

然后我有一个请求范围的托管Bean,其中包含最新收入的 getter:

 public List<Movs> getlatestIncomes() {
if (incomes == null)
incomes = this.cajaFacade.getLatestIncomes(20);
return incomes;
}

这个 getter 被调用了 8 次,而且我没有在其他地方使用它,只在 dataTable 的值上使用它。为什么会发生这种情况?如果您需要更多代码,请询问。但这是我使用该属性的唯一地方。

最佳答案

JSF 需要访问它时就会调用它。从技术上讲,您不应该担心这一点。

但是,对于给定的代码片段,它最多应该被调用 3 次,全部在渲染响应阶段。一次encodeBegin()期间,一次在encodeChildren()期间一次在encodeEnd()期间。或者它是否包含输入元素并且您在表单提交期间是否进行了计数?

无论如何,调试堆栈和 getter 中的当前阶段 ID 应该会提供一些见解。

private List<Movs> latestIncomes;
private AtomicInteger counter = new AtomicInteger();

@PostConstruct
public void init() {
latestIncomes = cajaFacade.getLatestIncomes(20);
}

public List<Movs> getlatestIncomes() {
System.err.printf("Get call #%d during phase %s%n", counter.incrementAndGet(),
FacesContext.getCurrentInstance().getCurrentPhaseId());
Thread.dumpStack();

return latestIncomes;
}

(如您所见,我将列表加载移动到正确的位置)

关于java - 帮助理解JSF对托管bean的多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5453862/

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