composite:insertFacet 和 composite:renderFacet 有什么区别?

What is the difference between composite:insertFacet and composite:renderFacet?

我正在使用 GlassFish 3.1.2 并尝试在复合组件中使用 composite:insertFacet,但是在使用我的 CC 时未生成 HTML 标记。

相同的代码在我使用 composite:renderFacet 时工作正常,但我想了解为什么 composite:insertFacet 在该示例中不起作用?什么时候使用它们?

这是我的 login.xhtml 文件(复合组件实现,“heading”方面是问题所在):

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:composite="http://java.sun.com/jsf/composite">

       <composite:interface>
          <composite:editableValueHolder name="nameInput" targets="form:name"/>
          <composite:editableValueHolder name="passwordInput" targets="form:password"/>
          <composite:editableValueHolder name="inputs"
                targets="form:name form:password"/>
          <composite:actionSource name="loginButton" targets="form:loginButton"/>

          <composite:attribute name="name"/>
          <composite:attribute name="password"/>

          <composite:attribute name="namePrompt"/>
          <composite:attribute name="passwordPrompt"/>

          <composite:attribute name="loginValidate" 
             method-signature="void validateLogin(ComponentSystemEvent e)
                throws javax.faces.event.AbortProcessingException"/>

          <composite:attribute name="loginAction" 
             method-signature="java.lang.String action()"/>

          <composite:facet name="heading"/>
          <composite:facet name="error"/> 
       </composite:interface>

       <composite:implementation>
          <h:outputScript library="util" name="login.js" target="head"/>
          <h:form id="form" onsubmit="return checkForm(this, '#{cc.clientId}')">
             <composite:insertFacet name="heading"/>
             <h:panelGrid columns="2">
                #{cc.attrs.namePrompt}
                <h:panelGroup>
                   <h:inputText id="name" value="#{cc.attrs.name}"/>
                   <h:message for="name"/>
                </h:panelGroup>

                #{cc.attrs.passwordPrompt}

                <h:panelGroup>
                   <h:inputSecret id="password" value="#{cc.attrs.password}" size="8"/>
                   <h:message for="password"/>
                </h:panelGroup>
             </h:panelGrid>

             <p>
                <h:commandButton id="loginButton"
                   value="#{cc.attrs.loginButtonText}"
                   action="#{cc.attrs.loginAction}"/>
             </p>

          </h:form>

          <composite:renderFacet name="error"/>

          <p><composite:insertChildren/></p>

          <p>#{cc.resourceBundleMap.footer}</p>
       </composite:implementation>
    </html>

index.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:util="http://java.sun.com/jsf/composite/util"
      xmlns:ui="http://java.sun.com/jsf/facelets">
   <h:head>
      <title>#{msgs.loginHeading}</title>
      <h:outputStylesheet library="css" name="styles.css" />
   </h:head>
   <h:body>
      <util:login namePrompt="#{msgs.namePrompt}"
            passwordPrompt="#{msgs.passwordPrompt}"
            name="#{user.name}"
            password="#{user.password}"
            loginAction="#{user.login}"
            loginButtonText="#{msgs.loginButtonText}">

         <f:validateLength minimum="4" for="nameInput"/>
         <f:validator validatorId="com.corejsf.Password" for="passwordInput"/>
         <f:actionListener type="com.corejsf.LoginActionListener" for="loginButton"/>

         <f:facet name="heading" styleClass="header">
            <div class="prompt">#{msgs.loginPrompt}</div>
         </f:facet>

         <f:facet name="error" styleClass="error">
            <h:messages layout="table" styleClass="error"/>
         </f:facet>

         <!-- Child component -->
         <h:link outcome="register">#{msgs.registerLinkText}</h:link>

      </util:login>
      <ui:debug/>
   </h:body>
</html>

我试图从组件树中找出原因,但是当 insertFacet 出现时我更加困惑,这是 组件树 中的部分使用 insertFacet 时:

    <UIOutput id="j_idt7" inView="true" rendered="true" transient="false">

        <UINamingContainer id="j_idt8" inView="true" rendered="true" transient="false">
            error

            <HtmlMessages globalOnly="false" id="j_idt10" inView="true" layout="table" redisplay="true" rendered="true" showDetail="false" showSummary="true" styleClass="error" tooltip="false" transient="false"/>

            javax.faces.component.COMPOSITE_FACET_NAME

            <UIPanel inView="true" rendered="true" transient="false">

                <HtmlForm enctype="application/x-www-form-urlencoded" id="form" inView="true" onsubmit="return checkForm(this, '')" prependId="true" rendered="true" submitted="false" transient="false">
                    header

                    <div class="prompt">#{msgs.loginPrompt}</div>

                    <HtmlPanelGrid border="-2147483648" columns="2" id="j_idt15" inView="true" rendered="true" transient="false">
....

使用renderFacet时的组件树:

<UIOutput id="j_idt7" inView="true" rendered="true" transient="false">

    <UINamingContainer id="j_idt8" inView="true" rendered="true" transient="false">
        error

        <HtmlMessages globalOnly="false" id="j_idt10" inView="true" layout="table" redisplay="true" rendered="true" showDetail="false" showSummary="true" styleClass="error" tooltip="false" transient="false"/>

        heading

        <div class="prompt">#{msgs.loginPrompt}</div>

        javax.faces.component.COMPOSITE_FACET_NAME

        <UIPanel inView="true" rendered="true" transient="false">

            <HtmlForm enctype="application/x-www-form-urlencoded" id="form" inView="true" onsubmit="return checkForm(this, '')" prependId="true" rendered="true" submitted="false" transient="false">

                <UIOutput id="j_idt15" inView="true" rendered="true" transient="false"/>

                <HtmlPanelGrid border="-2147483648" columns="2" id="j_idt16" inView="true" rendered="true" transient="false">
 ....

<cc:insertFacet> 不是针对组合本身,而是针对组合的子项,后者又需要外部提供的方面。

例如支持 <f:facet name="header"><h:dataTable> (糟糕的复合示例,但这只是为了给出想法):

<cc:implementation>
    <h:dataTable> 
        <cc:insertFacet name="header">

将用作

<my:composite>
    <f:facet name="header">

这与下面没有复合的效果完全相同

<h:dataTable>
    <f:facet name="header">

<cc:insertFacet>确实很少用到。

<cc:renderFacet> 用于复合实现本身,基本上与模板和标记文件中的 <ui:define>/<ui:insert> 相同。