PrimeFaces 5.2:如何呈现依赖于不同上下文的元素?

PrimeFaces 5.2: How to render Element that dependent on different contexts?

比方说:为了在不同上下文中重用可重复的代码片段。我有一个 fragment.xhtml 将包含在多个其他不同的 xhtml 中,比如 main1.xhtml 和 main2.xhtml

fragment.xhtml 为:

<h:body>
  <p:panelGrid id="qualifyGrid">
    <f:facet name="header">
        <p:row>
            <p:column colspan="4">#{staffBean.currentStaff.name} Qualify</p:column>
        </p:row>
    </f:facet>
   ......
    <p:row>
        <p:column styleClass="addlabel"><h:outputLabel for="id" value="#{clientqualify['web.qualify.label.id']}"/></p:column>
        <p:column styleClass="wideField" colspan="3">
            <h:inputText id="id1" value="#{qualifyvalue1}" rendered=""/>
            <h:inputText id="id" value="#{qualifyvalue}" rendered=""/>
        </p:column>
    </p:row>
   ......
    <p:row>
        <p:column styleClass="addlabel"><h:outputLabel for="name" value="#{clientqualify['web.qualify.label.name']}"/></p:column>
        <p:column styleClass="wideField" colspan="3"><p:inputText styleClass="field" id="name" value="#{qualifyControlBean.currentQualify.qualifyName}" required="true"/></p:column>
    </p:row> 
  </p:panelGrid>
</h:body>

main1.xhtml:

<h:body>
  <ui:composition template="/WEB-INF/ozssc-staff-template.xhtml">
    <ui:define name="content">
        <h3>#{clientqualify['web.qualify.edit.heading']}</h3>
        <p:messages id="messages" showDetail="true" autoUpdate="true" />
        <h:form id="qualifyeditform" prependId="false">
            <ui:include src="fragment.xhtml"/>
            <h:panelGrid style="display:block" border="0">
                <p:commandButton id="submit" value="Save" action="#{qualifyControlBean.saveQualify}" icon="fa fa-fw fa-save" ajax="false" disabled="#{qualifyControlBean.currentQualify.status=='Verified'}"/>
            </h:panelGrid>
        </h:form>
    </ui:define>
  </ui:composition>
</h:body>

main2.xhtml

<h:body>
  <ui:composition template="/WEB-INF/ozssc-staff-template.xhtml">
    <ui:define name="content">
        <h3>#{clientqualify['web.qualify.edit.heading']}</h3>
        <p:messages id="messages" showDetail="true" autoUpdate="true" />
        <h:form id="qeditform" prependId="false">
            <ui:include src="fragment.xhtml"/>
            <h:panelGrid style="display:block" border="0">
                <p:commandButton id="submit" value="Save" action="#{qualifyControlBean.saveQualify}" icon="fa fa-fw fa-save" ajax="false" disabled="#{qualifyControlBean.currentQualify.status=='Verified'}"/>
            </h:panelGrid>
        </h:form>
    </ui:define>
  </ui:composition>
</h:body>

从您看到的上面的代码片段中,fragment.xhtml 包含在两种不同的表单中,在 main1.xhtml 中,表单 ID 是 "qualifyeditform",在 main2.xhtml 中,表单 ID 是"qeditform".

我很难找到一种方法让 fragment.xhtml 元素根据不同的父上下文呈现不同的输入文本。即,如果此 fragment.xhtml 包含在 main1.xhtml 中(表单 ID 为:"qualifyeditform"),则呈现 inputText id "id1"。如果此 fragment.xhtml 包含在 main2.xhtml 中(表单 ID 为:"qeditform"),则呈现 inputText id "id".

如何设置 EL 表达式来查找父表单元素的 ID?并在我的 rendered="" 表达式中使用它?

我觉得你可以

<h:inputText id="id1" 
             value="#{qualifyvalue1}" 
             rendered="#{component.parent.clientId eq 'qualifyeditform'}"/>