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

时间:2015-01-25 13:54:43

标签: jsf jsf-2 composite-component facet

我正在使用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>

的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时组件树的部分:< / p>

    <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">
 ....

1 个答案:

答案 0 :(得分:11)

<cc:insertFacet>并非用于复合材料本身,而是用于复合材料的孩子,后者反过来需要一个需要在外部提供的方面。

E.g。一个支持<h:dataTable>的{​​{1}}(糟糕的复合示例,但它只是为了提出这个想法):

<f:facet name="header">

将用作

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

这与没有复合

时的效果完全相同
<my:composite>
    <f:facet name="header">

确实很少使用<h:dataTable> <f:facet name="header">

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