2 Replies Latest reply on Dec 2, 2008 5:35 AM by 843833

    Access component tree in AJAX PhaseListener with JSF

      I have a page that displays a form and a dataTable. When a new entry is entered on the form ad submitted, an AJAX request is sent to the server where it is intercepted by a custom PhaseListener. The PhaseListener updates the database with the new information.

      Now, what I would like to do is get the UIComponent instance for the dataTable, call encodeBegin, encodeChildren, and encodeEnd on it and have that returned as the response from the AJAX request. The problem I am having is the UIViewRoot is not populated after the RestoreView phase.

      I have also tried doing a method binding on a bean that has a property bound to the dataTable and it is null as well.

      So, my questions are:
      a) Is it possible to get the component?
      b) Is it possible to have the component render itself as the response to the AJAX request?

      Thanks for any help/suggestions.

        • 1. Re: Access component tree in AJAX PhaseListener with JSF
          Hi Adam,

          Yes it is possible and this is exactly what Dynamic Faces does. DynaFaces in an open source project lead by the JSF spec lead, Ed Burns. More information about the project can be found at https://jsf-extensions.dev.java.net

          Another way to accomplish the task the way you are going is to get the state restored to be used in your phaselistener. To accomplish this you have to make sure that the listener is called after the restore-view phase. Also, you need to send back the jsf specific field "javax.faces.ViewState" in your request so the FacesServlet can restore the view using that infomation.

          Once you have the view restored, you can look up the component by invoking findComponent of the view root you get from the facescontext. Then with the response you have to reset the client's state, so the next request will work properly.

          Using Dynamic Faces, should be much easier.

          This can be a complex subject, so for further reading I would recommend Ed Burns' co-authored book "JavaServer Faces: The Complete Reference".

          Hope this helps - Thanks - Mark
          • 2. Re: Access component tree in AJAX PhaseListener with JSF

            I have also similar question related to this. that's why i am posting to this same request.

            The scenario that I am trying out is, (we are using XHTML as UI for JSF)

            I have a form which displays data table (custom component). this is in one xhtml and in another xhtml I have similar kind of data table. These two xhtmls are included in a outer page.

            When the initial request is submitted each child page returns some rows (with expand/collapse icons at the start of row) and displays on UI. But when user clicks on expand icon, I am making a simple AJAX call to get child rows for the selected row. I managed to paint those child rows to the parent row (using javascript) and dispalys on UI.

            Now the problem is, if I do any action(or form submission) or clicking on any command links page doesn't work and reloads the same page with initial rows (as it was a fresh request). but doing any operation on parent row works fine.

            what I doubt on this approach is, view root on the server did not get updated when I make AJAX call (as it goes a separate request) to get child rows.

            If I am suspecting right, can some body please suggest me how to update view root of the JSF when I make AJAX call. If my assumption on the issue is not correct, then please advice me how to work with this kind of scenario.

            thanks in advance.
            Satish. G