讓我們假設:在不同的上下文中重複使用代碼片段。我有一個fragment.xhtml將被包含在多個不同的XHTML,說main1.xhtml和main2.xhtmlPrimeFaces 5.2:如何渲染依賴不同上下文的元素?
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被包含在兩個不同的表單中s,在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」),那麼render inputText id爲「id」。
如何設置EL表達式來查找父窗體元素的ID?並在我的渲染=「」表達式中使用它?
最後。通過使用rendered =「#{component.parent.parent.parent.parent.parent.clientId eq'qualifyeditform'}」 –