tags:

views:

275

answers:

1

I got a recommendation to put all data lookups in the beforePhase for a given page, however, now that I am doing some deeper analysis it appears that some getter methods are being called before the beforePhase is fired.

It became very obvious when I added support for a url parameter and I was getting NPEs on objects that are initialized in the beforePhase call.

Any thoughts? Something I have set wrong.

I have this in my JSP page:

<f:view beforePhase="#{someController.beforePhaseSummary}">

That is only the 5th line in the JSP file and is right after the taglibs.

Here is the code that is in the beforePhaseSummary method:

public void beforePhaseSummary(PhaseEvent event) {
    logger.debug("Fired Before Phase Summary: " + event.getPhaseId());
    if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
        HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if (request.getParameter("application_id") != null) {
            loadApplication(Long.parseLong(request.getParameter("application_id")));
        }
        /* Do data fetches here */
    } 
}

The logging output above indicates that an event is fired. The servlet request is used to capture the url parameters. The data fetches gather data. However, the logging output is below:

2010-04-23 13:44:46,968 [http-8080-4] DEBUG ...SomeController 61 - Get Permit
2010-04-23 13:44:46,968 [http-8080-4] DEBUG ...SomeController 107 - Getting UnsubmittedCount
2010-04-23 13:44:46,984 [http-8080-4] DEBUG ...SomeController 61 - Get Permit
2010-04-23 13:44:47,031 [http-8080-4] DEBUG ...SomeController 133 - Fired Before Phase Summary: RENDER_RESPONSE(6)

The logs indicate 2 calls to the getPermit method and one to getUnsubmittedCount before the beforePhase is fired.

A: 

Figured this out. If you have JSTL mixed into your page those methods may/will fire before the JSF methods are called. I had methods like:

<c:if test="${someController.logic == true}">
    <p>Some Text</p>
</c:if>

The someController.logic call was firing before the someController.beforePhase call.

I replaced the above with

<h:outputText rendered="#{someController.logic == true}">
    <p>Some Text</p>
</h:outputText>

Now things fire in the correct order. I also found other pages in my app that had strange results when mixing JSTL with JSF. So I am in the process of banishing the JSTL from the application now.

Bill Leeper