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

Tar*_*rik 4 jsf facet composite-component jsf-2

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

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

这是我的login.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: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>
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

我试图从组件树中找出原因,但是当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">
....
Run Code Online (Sandbox Code Playgroud)

使用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">
 ....
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 12

<cc:insertFacet>并不是复合材料本身的意图,而是复合材料的孩子,而后者又需要一个需要在外部提供的方面.

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

<cc:implementation>
    <h:dataTable> 
        <cc:insertFacet name="header">
Run Code Online (Sandbox Code Playgroud)

哪个用作

<my:composite>
    <f:facet name="header">
Run Code Online (Sandbox Code Playgroud)

这与没有复合的情况完全相同

<h:dataTable>
    <f:facet name="header">
Run Code Online (Sandbox Code Playgroud)

<cc:insertFacet>确实使用很少.

<cc:renderFacet>是为了复合实现本身,基本上与模板和标签文件中的<ui:define>/ 相同<ui:insert>.