title: How to use FaceletContext.includeFacelet() properly?
body: <p>I am using Mojarra 2.2.8 and Primefaces 5.2. I need to include different facelets on ajax request, so i use this code in managed bean:</p>
  
  <pre><code>FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
  if (type.equals("default")) {
          faceletContext.includeFacelet(this, "/defaultDlg.xhtml");
  }
  </code></pre>
  
  <p>In <em>defaultDlg.xhtml</em> i have Primefaces dynamic tabView:</p>
  
  <pre><code>&lt;p:tabView id="contentTabView" onTabShow="PF('contentDlg').initPosition();" dynamic="true"&gt;
      &lt;p:ajax event="tabChange" listener="#{folderView.onTabChange}"/&gt;
      &lt;p:tab title="#{msg['title.documentCard']}"&gt;
          &lt;p:panel&gt;
  
              &lt;!--  some widgets here... --&gt;
  
          &lt;/p:panel&gt;
      &lt;/p:tab&gt;
      &lt;p:tab title="#{msg['title.performance']}"&gt;
          &lt;!--  DataTable with subTable ... --&gt;
          &lt;p:dataTable  value="#{folderView.performance}" 
                                  id="historyTable" widgetVar="historyTable" var="wf"&gt;
              &lt;p:columnGroup type="header"&gt;
                  &lt;p:row&gt;
                      &lt;p:column headerText="#{msg['audit.performance.sendbydesc']}" /&gt;
                      &lt;p:column  headerText="#{msg['audit.performance.performerdesc']}" /&gt;
                  &lt;/p:row&gt;
              &lt;/p:columnGroup&gt;
  
              &lt;p:subTable var="task" value="#{wf[5]}"&gt;
                  &lt;f:facet name="header"&gt;
                      &lt;h:outputText value="#{wf[2]}" /&gt;
                  &lt;/f:facet&gt;
                  &lt;p:column&gt;
                      &lt;h:outputText value="#{task[2]}" /&gt;
                  &lt;/p:column&gt;
                  &lt;p:column&gt;
                      &lt;h:outputText value="#{task[4]}" /&gt;
                  &lt;/p:column&gt;
              &lt;/p:subTable&gt;
          &lt;/p:dataTable&gt;
      &lt;/p:tab&gt;
      &lt;!--  more tabs ... --&gt;
  </code></pre>
  
  <p>
  I looked <a href="https://stackoverflow.com/a/24578871/6065134">this answer on related question</a> where user says that <em>"Use FaceletContext is the wrong way to do it, because later it causes duplicate id and state management issues"</em> without any explanation. In my example this method correctly includes UIComponents from <em>defaultDlg.xhtml</em> to view root and renders them on page.</p>
  
  <p>But when i click on second tab with subTable or make any ajax request i get this error:</p>
  
  <blockquote>
    <p>Severe: java.lang.NumberFormatException: Trying to extract rowIndex from clientId 'ntikDoc:contentTabView:historyTable:j_id492561291_69346d22:j_id492561291_69346d58' For input string: "j_id492561291_69346d22"
        at javax.faces.component.UIData.invokeOnComponent(UIData.java:1021)
        at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1503)
        at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:713)
        at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1503)
        at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:713)
        at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1503)
        at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:713)
        at org.primefaces.component.api.UITabPanel.invokeOnComponent(UITabPanel.java:798)
        at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1503)
        at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:713)
        at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1503)
        at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:713)
        at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1503)
        at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:713)
        at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1503)
        at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:713)
        at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1503)
        at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:713)
        at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1503)
        at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:713)
        at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1503)
        at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:713)
        at com.sun.faces.application.view.FaceletViewHandlingStrategy.locateComponentByClientId(FaceletViewHandlingStrategy.java:2063)
        at com.sun.faces.application.view.FaceletViewHandlingStrategy.reapplyDynamicAdd(FaceletViewHandlingStrategy.java:2127)
        at com.sun.faces.application.view.FaceletViewHandlingStrategy.reapplyDynamicActions(FaceletViewHandlingStrategy.java:2114)
        at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:961)
        at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:99)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
        at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
        at sun.reflect.GeneratedMethodAccessor167.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:323)
        at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:321)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAsPrivileged(Subject.java:536)
        at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:356)
        at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:212)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1543)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
        at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:88)
        at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:200)
        at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:196)
        at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:100)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
        at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:88)
        at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:200)
        at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:196)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
        at java.lang.Thread.run(Thread.java:745)</p>
  </blockquote>
  
  <p>I thik that there might be a problem with state saving during ajax request because if i remove subTable from second tab i get this warnings on every ajax request:</p>
  
  <blockquote>
    <p>Warning: Unable to save dynamic action with clientId 'ntikDoc:j_id69584408_969820d' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:j_id69584408_969820d_1' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:idtFrom:j_id69584408_969829e' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:idtFrom:j_id69584408_96982b4' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:j_id69584408_96983ee' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:idtTo:j_id69584408_9698056' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:idtTo:j_id69584408_969806c' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:idtTo:j_id69584408_969800a' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:contentTabView:j_id492561291_69346015' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:contentTabView:j_id492561291_69346015_1' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:contentTabView:contentTable:j_id492561291_69346b92' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:contentTabView:contentTable:j_id492561291_69346bfc' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:contentTabView:perTable:j_id492561291_69346cc4' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:contentTabView:perTable:j_id492561291_69346c26' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:contentTabView:perTable:j_id492561291_69346c10' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:contentTabView:historyTable:j_id492561291_69346d22:j_id492561291_69346d6e' because the UIComponent cannot be found
    Warning: Unable to save dynamic action with clientId 'ntikDoc:contentTabView:historyDocTable:j_id492561291_69346ef9' because the UIComponent cannot be found</p>
  </blockquote>
  
  <p>So how to use <code>faceletContext.includeFacelet</code> correctly?</p>