Search code examples
facelets

Getting the component tree from UIViewRoot with Facelets as view


I have an application that was coded with JSPs and now we are in the process to migrate to the pages to Facelets.

One of our PhaseListeners is manipulating the component tree, because it needs to find out some components in the tree and extract some of its values. But with Facelets, UIViewRoot does not return any children.

How can I get the same type of functionality with Facelets?

Thanks in advance, Paulo


Solution

  • Retrieving UIViewRoot with this line should work:

    FacesContext.getCurrentInstance().getViewRoot
    

    My guess is that you are trying to manipulate the component tree in the first phase of JSF lifecycle. (link)

    The first phase consists of retrieving the view, that is, the component tree, for the requested page. Therefore you can just explore the component tree after this phase has completed.

    Use

    phaseEvent.getPhaseId()
    

    to see which phase you are working in.