Help understanding JSF's multiple calls to managed bean

為{幸葍}努か 提交于 2019-12-11 08:12:56

问题


I'm using h:datatable, here's the revelant line of my code:

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

then I have a Request scoped managedBean with the getter for the latest Incomes:

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

this getter gets called 8 times, and I'm not using it anywhere else, only on the value for the dataTable. Why is this happening? If you need more code please ask. But that's the only place where I use that property.


回答1:


It get called as many as JSF needs to access it. You should technically not worry about this.

However, with the given code snippet, it should be called at highest 3 times, all during the render response phase. Once during encodeBegin(), once during encodeChildren() and once during encodeEnd(). Or does it contain input elements and did you count during a form submit?

Regardless, debugging the stack and the current phase ID in the getter should give some insights.

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

(as you see, I moved the list loading to the right place)



来源:https://stackoverflow.com/questions/5453862/help-understanding-jsfs-multiple-calls-to-managed-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!