Hi WarnerJan,

WarnerJan Veldhuis wrote:

I am too :) I read somewhere in the docs that database access and
similar operations should be done in the onRender method. I guess I
never questioned that method.

Lesson learned: always create your form and stuff in the constructor or
the onInit() method, correct?


Yes most the time you should place expensive operations in onRender, while the creation of the control is best done in constructor or onInit. Why this practice?

Consider the case where you could navigate away from the Page via a redirect:

  public void onInit() {
    Table t = new Table("t");

    // Expensive operation below
    t.setRows(selectRowsFromDb());
  }

  public boolean onSave() {
    // Save stuff to db
    ...

    setRedirect(OtherPage.class);
    // Since we're redirecting, don't process any further
    return false;
  }

The onInit above contains a potentially expensive operation because it selects rows from the db.

And since you might be redirecting to another page via the onSave method, the table is never displayed and the db hit was redundant.

By moving the call "t.setRows(selectRowsFromDb())" to the onRender method you save yourself the database hit if you were to redirect because onRender won't be invoked since onSave returns false.

Thing is though, if you don't redirect or forward to another Page, then it really doesn't matter where you place the setRows method.

With FormTable your options are more limited because the FormTable rows must be filled *before* onProcess occurs, otherwise the Field and Button values won't be processed and their actions won't be invoked.

Also note that you still have access to HttpServletRequest and can inspect it for certain parameters and conditionally render the FormTable in the onInit method.

kind regards

bob

Reply via email to