Search code examples
jsflifecycle

How to determine current JSF lifecycle phase in the code


I'm using JSF version 1.2_09-b02-FCS

Let I've an inner class TableList of some other class:

private class TableList extends AbstractList<T> {

    private List<T> list;

    private int size;

    public T get(int i) {
        if (needToUpdateList || currPage != i / pageSize) {
            currPage = i / pageSize;
            list = getDataList(currPage * pageSize, pageSize);
            needToUpdateList = false;
        }
        return list.get(i % pageSize);
    }

    public int size() {
        if (needToUpdateSize) {
            size = getDataSize();
            needToUpdateSize = false;
        }
        return size;
    }
}

whose get method is going to invoke during the JSF lifecycle phases. The problem is that i don't want to invoke list = getDataList(currPage * pageSize, pageSize); suring the render response phase. Is it possible to do that?


Solution

  • Check if FacesContext#getRenderResponse() returns true.

    if (FacesContext.getCurrentInstance().getRenderResponse()) {
        // We're currently in the render response phase.
    }
    

    In case you're using JSF 2.0, you shouldn't use that method, but instead FacesContext#getCurrentPhaseId() because the getRenderResponse() falsely returns false during render response of an initial GET request.