0

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.

Bill Leeper
  • 683
  • 1
  • 10
  • 20
  • 1
    What's the functional requirement? Why beforephase and not constructor or postconstruct? What getters are you talking about? Why are they invoked? Do you have a fair understanding of [JSF lifecycle](http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html)? – BalusC Apr 22 '10 at 16:41
  • It is a member that is used throughout the life of the session storing things like header information and also used for bread-crumb navigation. It is session scope. As such if I put it in the postContruct then it doesn't re-initialize when the subject changes. At one point I had it request scope and had a lot of difficulty constantly getting it initialized on every page as you navigated around levels of the app below the level that the information was normally initialized. – Bill Leeper Apr 22 '10 at 18:59
  • I would like to see code for the beforePhaseSummary method. – Naganalf Apr 22 '10 at 23:27
  • I added the beforePhaseSummary call and also included some logging output – Bill Leeper Apr 23 '10 at 17:48
  • I currently am working around this by doing some careful analysis of existing objects in the getPermit method, but ideally I would rather use the beforePhaseSummary call. – Bill Leeper Apr 23 '10 at 17:49
  • Figured this out. See accepted solution – Bill Leeper Apr 27 '10 at 18:19

1 Answers1

0

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
  • 683
  • 1
  • 10
  • 20
  • Related: [JSTL in JSF2 Facelets… makes sense?](http://stackoverflow.com/q/3342984/1065197). Next time it would be better to explain where your problem arises. – Luiggi Mendoza May 28 '14 at 05:39