09Q71AO534
09Q71AO534

Reputation: 4440

Find the parent ID for every component which has an ID attribute

I am using JDK1.7, JSF2 and Primefaces.

I have a .xhtml as below. I want to list the Parent and the Child Relations as following <Component Id Tree Structure> | <Id> | <Child of Id- Parent> as shown in the Expected Output

xhtml :

<h:form id="rootParentForm">

    <p:treeTable id="singleTreeTble" value="#{ttSelectionView.root1}" var="document" selectionMode="single" selection="#{ttSelectionView.selectedNode}" style="margin-top:0">
        <f:facet name="header">
            Single
        </f:facet>
        <p:column headerText="Name">
            <h:outputText value="#{document.name}" />
        </p:column>
        <p:column headerText="Size">
            <h:outputText value="#{document.size}" />
        </p:column>
        <p:column headerText="Type">
            <h:outputText value="#{document.type}" />
        </p:column>
    </p:treeTable>

    <p:commandButton id="singleCmdBtn" value="Display" update="msgs" icon="ui-icon-newwin"
                        actionListener="#{ttSelectionView.displaySelectedSingle}"/>


    <h3 style="margin-top:0">Basic</h3>
    <p:accordionPanel id="basicAP">
        <p:tab id="basicTab1" title="Godfather Part I">
            <h:panelGrid columns="2" cellpadding="10">
                <p:graphicImage name="demo/images/godfather/godfather1.jpg" />
                <h:outputText
                    value="The story begins as Don Vito Corleone..." />
            </h:panelGrid>
        </p:tab>
        <p:tab id="basicTab2" title="Godfather Part II">
            <h:panelGrid columns="2" cellpadding="10">
                <p:graphicImage name="demo/images/godfather/godfather2.jpg" />
                <h:outputText value="Francis Ford Coppola's legendary..." />
            </h:panelGrid>
        </p:tab>
        <p:tab id="basicTab3" title="Godfather Part III">
            <h:panelGrid columns="2" cellpadding="10">
                <p:graphicImage name="demo/images/godfather/godfather3.jpg" />
                <h:outputText value="After a break of more than 15 years..." />
            </h:panelGrid>
        </p:tab>
    </p:accordionPanel>

    <h3>Multiple Selection</h3>
    <p:accordionPanel id="multipleAP" multiple="true">
        <p:tab id="multipleTab1"  title="Godfather Part I">
            <h:panelGrid columns="2" cellpadding="10">
                <p:graphicImage name="demo/images/godfather/godfather1.jpg" />
                <h:outputText
                    value="The story begins as Don Vito Corleone..." />
            </h:panelGrid>
        </p:tab>
        <p:tab id="multipleTab2"  title="Godfather Part II">
            <h:panelGrid columns="2" cellpadding="10">
                <p:graphicImage name="demo/images/godfather/godfather2.jpg" />
                <h:outputText value="Francis Ford Coppola's legendary..." />
            </h:panelGrid>
        </p:tab>
        <p:tab id="multipleTab3"  title="Godfather Part III">
            <h:panelGrid columns="2" cellpadding="10">
                <p:graphicImage name="demo/images/godfather/godfather3.jpg" />
                <h:outputText value="After a break of more than 15 years...." />
            </h:panelGrid>
        </p:tab>
    </p:accordionPanel>    
</h:form>

Expected Output: Expected Output

What is the best approach to do this? So, that all the parent and child relations will be listed as expected.

I will upload the xhtml page and then i want to find & show the parent-child relation.

Upvotes: 0

Views: 878

Answers (1)

iTollu
iTollu

Reputation: 1069

I believe, you should:

  1. Get current FacesContext with FacesContext.getCurrentInstance();

  2. Get view root with current instance's getViewRoot() -- you will get instance of UIComponent, which has visitTree() method;

  3. Use UIComponent instance's visitTree() method -- pass it callback function which tests, whether id attribute was set etc.

You may find JSF API Documentation bundled with JSF Specifications useful.

To perform it all, You will need to parse xhtml into component tree. If it is Facelets markup, classes from javax.faces.view.facelets package will be useful as well as javax.faces.application.ResourceHandlerWrapper.

If uploaded xhtml is irrelevant to your server-side application, you will also need to stub EL expressions.

Upvotes: 1

Related Questions