1

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.

arg20
  • 4,893
  • 1
  • 50
  • 74
  • have a look at http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times – Mark Mar 28 '11 at 01:52
  • Yes, that's 3 or 4 times, but is it normal for a getter to be called 8 to 10 times? – arg20 Mar 28 '11 at 02:13
  • As BalusC mentioned in that question - you should not be doing business logic in your getter method as you are doing in your code. Could you move that code to an init() or contructor() methods? That way when you getter gets called it just returns the pre-loaded list? – CoolBeans Mar 28 '11 at 03:34

1 Answers1

2

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)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555