如何在选定的 XHTML 与托管 bean 接口作为参数之间共享 f:event 的 f:metadata

How share some f:event of f:metadata between selected XHTML with managed bean interface as parameter

注意到 JSF view metadata demystified:

Since this tag is about current view metadata it doesn't participate in XHTML templates (the page author must ensure that the element does not appear on a template or included page; it can be in a template client) and it is direct child of <f:view>.

我有多个 <f:viewParam><f:event> 在许多 XHTML 页面中重复,它们又是 template.xhtml 的客户端。每个 f:event 都依赖于一个给定的托管 bean,其中每个 XHTML 页面的托管 bean 都不同(但遵循一个公共接口,即,每个相关的托管 bean 都存在相同的监听器操作):

<f:view>
    <f:metadata>

        <!-- I want to share this across many XHTML parametrised by the bean -->
        <f:viewParam name="id" value="#{particularBean.id}"/>
        <f:event type="preRenderView" listener="#{particularBean.opCommon1}"/>
        <f:event type="preRenderView" listener="#{particularBean.opCommon2}"/>
        <!-- END SHARED PORTION -->

        <f:event type="preRenderView" listener="#{particularBean.onlyForMe}"/>

    </f:metadata>
</f:view>

<ui:composition template="/template.xhtml">

问:如何封装共享的 <f:viewParam><f:event> 部分,以便它可以 "included" 并被视为具有特定托管 bean 的公共策略片段(满足公共接口)为每个 XHTML 页面传入?

我在我自己的一个旧项目中找到了答案(正如我发布的那样,我模糊地记得我已经在几年前的某个阶段对此进行了调查)。这有效:

/include/shared.xhtml中:

<ui:composition 
 xmlns="http://www.w3.org/1999/xhtml"
 xmlns:f="http://xmlns.jcp.org/jsf/core"
 xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
 >
    <f:viewParam name="id" value="#{particularBean.id}"/>
    <f:event type="preRenderView" listener="#{compatibleBean.opCommon1}"/>
    <f:event type="preRenderView" listener="#{compatibleBean.opCommon2}"/>
</ui:composition>

并且在任何客户端中:

<f:view>
 <f:metadata>

  <ui:include src="/included/shared.xhtml">
    <ui:param name="compatibleBean" value="#{particularBean}"/>
  </ui:include>

  <f:event type="preRenderView" listener="#{particularBean.onlyForMe}"/>

 </f:metadata>
</f:view>