1

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

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

然后我有一个请求范围的 managedBean 和最新收入的 getter:

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

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

4

1 回答 1

2

只要 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;
}

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

于 2011-03-28T12:35:25.717 回答