2015-03-02 112 views
0

我正在使用JDK1.7,JSF2和Primefaces。查找具有ID屬性的每個組件的父ID

我有一個.xhtml如下。我想列出Parent and the Child Relations如如圖所示Expected Output

XHTML以下<Component Id Tree Structure> | <Id> | <Child of Id- Parent>

<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

什麼是做到這一點的最好辦法?所以,所有的父母和子女關係都將按預期列出。

我會上傳xhtml頁面,然後我想找到&顯示父子關係。

+0

這是什麼意思:「我會上傳xhtml頁面,然後我想找到並顯示父子關係」?你想在上傳一些Facelets文件之後構建和分析組件樹,而無需重新部署應用程序?這個上傳的xhtml應該與正在運行的應用程序相關還是與真空中的任何「球形Facelets文件」相關? – iTollu 2015-03-02 12:30:37

+0

@iTollu我的意思是有一個FileUpload頁,因爲它在[ShowCase](http://www.primefaces.org/showcase/ui/file/upload/basic.xhtml)中,並且一旦上傳成功,我想顯示父子關係到文件中 – 09Q71AO534 2015-03-02 13:33:31

+0

然後,您可以將上傳的文件視爲現有上下文的一部分(所有表達式都將被解析)或完全獨立的代碼。你的情況哪一個是真的? – iTollu 2015-03-02 13:46:34

回答

1

我相信,你應該:

  1. 獲取當前的FacesContext與FacesContext.getCurrentInstance();與當前實例的getViewRoot()

  2. 獲取視圖根 - 你會 得到UIComponent,其中有visitTree()方法實例;

  3. 使用UIComponent實例的visitTree()方法 - 通過它回調 函數測試,id屬性是否設置等

您可能會發現與JSF Specifications有用捆綁JSF API文檔。

要執行這一切,您需要將xhtml解析爲組件樹。如果是Facelets標記,javax.faces.view.facelets包的類將會很有用,以及javax.faces.application.ResourceHandlerWrapper

如果上傳的xhtml與您的服務器端應用程序無關,您還需要存儲EL表達式。