I'm trying to implement a simple query by example view (qbe.jsp) containing 
* a form with the query criteria, 
* a table with query results
* a form for editing a selected table row

<tr:panelFormLayout rendered="#{formController.entity==null}">
...the query form
   <tr:inputText value="#{qbeController.example.criteria}"/>
</tr:panelFormLayout>

<tr:table value="#{qbeController.model}" var="object"
        rows="5" width="200" rendered="#{formController.entity==null}">
...the query result
...

<tr:column sortable="true" headerText="criteria">
        <tr:commandLink action="#{qbeController.select}">
                <tr:outputText value="#{object.type}" />
        </tr:commandLink>
</tr:column>

</tr:table>

<tr:panelFormLayout rendered="#{formController.entity!=null}">
        <tr:subform>
...the editor form
...
        </tr:subform>
</tr:panelFormLayout>

In the most simple implementation the table is populated by a getModel()
method (#{qbeController.model}).

protected DataModel model = new ListDataModel(Collections.EMPTY_LIST);
@Override
public DataModel getModel() {
        model.setWrappedData(service.queryByExample(example));
        return model;
}

As the page is going through the JSF lifecycle the getModel() method is
called multiple times resulting in multiple database hits. The database hits
are unnecessary, it should be sufficient to query the database once.
The query should be performed after the UPDATE_MODEL_VALUES phase (i.e after
the values from the query form have been applied to the backing bean ->
#{qbeController.example.criteria} ). This is the earlies t phase where it
makes sense to perform the query!

So I was trying to do some optimization to avoid this. Using the JSF 1.2
<f:view> afterPhase/beforePhase method bindings I tried to perform the
actual database query...


public void beforePhase(PhaseEvent event) {
        if (event.getPhaseId().equals(PhaseId.APPLY_REQUEST_VALUES)) {
                System.out.println();
        }
        System.out.println("before " + event.getPhaseId());
        if (event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
                if (model.getWrappedData() == Collections.EMPTY_LIST) {
                        model.setWrappedData(service.queryByExample(example));
                }
        }
}

public void afterPhase(PhaseEvent event) {
        System.out.println("after " + event.getPhaseId());
        if (event.getPhaseId().equals(PhaseId.UPDATE_MODEL_VALUES)) {
                if (model.getWrappedData() == Collections.EMPTY_LIST) {
                        model.setWrappedData(service.queryByExample(example));
                }
        }
}

protected DataModel model = new ListDataModel(Collections.EMPTY_LIST);

@Override
public DataModel getModel() {
        // model.setWrappedData(getCrudService().findByExample(example));
        return model;
}


The query result is correct! But the table dows not allow to click a row and
call the row editor! Seems that the table is missing something if no rows
are available during the APPLY_REQUEST_VALUES phase

Ok next, I try to do the query before the APPLY_REQUEST_VALUES phase ->
selecting a table row works but the query result is not correct as the query
criteria has not yet been applied to the backing bean
(#{qbeController.example.criteria}).

Hmmm, interested in your ideas about this... thx
-- 
View this message in context: 
http://www.nabble.com/-trinidad--what-is-the-proper-lifecycle-phase-for-populating-a-%3Ctr%3Atable%3E-tf4654950.html#a13300085
Sent from the MyFaces - Users mailing list archive at Nabble.com.

Reply via email to