The pattern I have been using for this sort of thing it to have the value accessor in the bean check to see if the data has been retrieved and, if not, sets the value up. E.g.,

public class SomeBean {
  private Object value = null;

  public Object getValue() {
    if (value == null) {
      // Initialize value
    }
    return value;
  }

  public void setValue(Object value) {
    this.value = value;
  }
}

The data table then accesses the value through the getter:

<h:dataTable var="item" value="#{someBean.value}">

You could use a PhaseListener on the Render Response phase (and JSF 1.2 will have listeners that can be more conveniently attached to a particular view), but then you may run into problems if some other view logic needs to access the value in a prior phase. For this reason, I have found most recommendation for using a PhaseListener to be wrong-headed.

--David Gadbois


Jonathan Eric Miller wrote:
Does anyone know if there is a recommended way to have a bean method executed before a JSF page is loaded? For example, I have a page with a h:dataTable in it. What I want to do is make it so that when a user brings up the page directly (by entering the URL in the browser text box rather than as the result of submitting a form), a bean method is executed which does a query to a database. I want the bean to be populated before the page is rendered. Currently, I'm doing this using a servlet filter. However, one problem with this is that Faces isn't initialized at this point. This isn't a big issue, but, one issue that I ran into is that I was using a managed bean and that bean might not have been created by Faces when the filter executes. So, I put some code in to test if the bean is null or not and it creates it if necessary. I'm wondering if there is a more recommended way to do this within the context of Faces itself. i.e. maybe using a listener or something? Note, I only want the code to execute for a specific page. It seems like there should be some kind of onInit() method that could be overriden to perform initialization before a page is renderered... I don't know much about ASP.NET, but, I think it allows you to do something like this. I guess I could use a scriptlet in the JSP to do it.

Jon

Reply via email to