简体   繁体   English

查找每个具有ID属性的组件的父ID

[英]Find the parent ID for every component which has an ID attribute

I am using JDK1.7, JSF2 and Primefaces. 我正在使用JDK1.7,JSF2和Primefaces。

I have a .xhtml as below. 我有一个.xhtml如下。 I want to list the Parent and the Child Relations as following <Component Id Tree Structure> | <Id> | <Child of Id- Parent> 我想按照以下<Component Id Tree Structure> | <Id> | <Child of Id- Parent>列出Parent and the Child Relations <Component Id Tree Structure> | <Id> | <Child of Id- Parent> <Component Id Tree Structure> | <Id> | <Child of Id- Parent> <Component Id Tree Structure> | <Id> | <Child of Id- Parent> as shown in the Expected Output <Component Id Tree Structure> | <Id> | <Child of Id- Parent>Expected Output所示

xhtml : 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: 预期产量: 预期产量

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. 我将上传xhtml页面,然后我想查找并显示父子关系。

I believe, you should: 我相信您应该:

  1. Get current FacesContext with FacesContext.getCurrentInstance() ; 使用FacesContext.getCurrentInstance()获取当前的FacesContext;

  2. Get view root with current instance's getViewRoot() -- you will get instance of UIComponent , which has visitTree() method; 使用当前实例的getViewRoot()获取视图根-您将获得UIComponent实例,该实例具有visitTree()方法;

  3. Use UIComponent instance's visitTree() method -- pass it callback function which tests, whether id attribute was set etc. 使用UIComponent实例的visitTree()方法- visitTree()传递回调函数,以测试是否设置了id属性等。

You may find JSF API Documentation bundled with JSF Specifications useful. 您可能会发现与JSF规范捆绑在一起的JSF API文档很有用。

To perform it all, You will need to parse xhtml into component tree. 要执行所有操作,您将需要将xhtml解析为组件树。 If it is Facelets markup, classes from javax.faces.view.facelets package will be useful as well as javax.faces.application.ResourceHandlerWrapper . 如果是Facelets的标记,从班javax.faces.view.facelets包将是有益的,以及javax.faces.application.ResourceHandlerWrapper

If uploaded xhtml is irrelevant to your server-side application, you will also need to stub EL expressions. 如果上载的xhtml与服务器端应用程序无关,则还需要对EL表达式进行存根。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM