【问题标题】:Find the parent ID for every component which has an ID attribute查找每个具有 ID 属性的组件的父 ID
【发布时间】:2015-03-02 10:07:47
【问题描述】:

我正在使用 JDK1.7、JSF2 和 Primefaces。

我有一个.xhtml,如下所示。我想列出Parent and the Child Relations 如下<Component Id Tree Structure> | <Id> | <Child of Id- Parent>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>

预期输出:

执行此操作的最佳方法是什么?因此,所有的父子关系都将按预期列出。

我将上传 xhtml 页面,然后我想查找并显示父子关系。

【问题讨论】:

  • 这些话是什么意思:“我将上传 xhtml 页面,然后我想查找并显示父子关系”?您想在上传一些 Facelets 文件后构建和分析组件树而不重新部署应用程序吗?这个上传的 xhtml 应该与正在运行的应用程序相关还是只是任何“真空中的球形 Facelets 文件”?
  • @iTollu 我的意思是有一个文件上传页面,就像它在ShowCase 中一样,一旦成功上传,我想将父子关系显示到文件中
  • 然后您可以将上传的文件视为现有上下文的一部分(所有表达式都将被解析)或完全独立的代码。在你的情况下哪一个是正确的?
  • @iTollu 在我的情况下,上传的文件是现有上下文的一部分。

标签: jsf jsf-2 components tree-traversal


【解决方案1】:

我相信,你应该:

  1. 使用FacesContext.getCurrentInstance()获取当前FacesContext;

  2. 使用当前实例的getViewRoot() 获取视图根目录——您将 获取UIComponent的实例,其中有visitTree()方法;

  3. 使用UIComponent 实例的visitTree() 方法——传递它回调 测试的函数,是否设置了 id 属性等。

您可能会发现与 JSF Specifications 捆绑在一起的 JSF API 文档很有用。

要执行这一切,您需要将 xhtml 解析为组件树。如果是 Facelets 标记,javax.faces.view.facelets 包中的类和javax.faces.application.ResourceHandlerWrapper 一样有用。

如果上传的 xhtml 与您的服务器端应用程序无关,您还需要存根 EL 表达式。

【讨论】:

    猜你喜欢
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    相关资源
    最近更新 更多