title: How to programmatically or dynamically create a composite component in JSF 2
body: <p>I need to programmatically create composite components in JSF 2. After few days of searching and experiments I figured out this method (highly inspired by Lexi at java.net): </p>
  
  <pre><code>/**
   * Method will attach composite component to provided component
   * @param viewPanel parent component of newly created composite component
   */
  public void setComponentJ(UIComponent viewPanel) {
      FacesContext context = FacesContext.getCurrentInstance();
      viewPanel.getChildren().clear();
  
      // load composite component from file
      Resource componentResource = context.getApplication().getResourceHandler().createResource("whatever.xhtml", "components/form");
      UIComponent composite = context.getApplication().createComponent(context, componentResource);
  
      // push component to el
      composite.pushComponentToEL(context, composite);
      boolean compcompPushed = false;
      CompositeComponentStackManager ccStackManager = CompositeComponentStackManager.getManager(context);
      compcompPushed = ccStackManager.push(composite, CompositeComponentStackManager.StackType.TreeCreation);
  
      // Populate the component with value expressions 
      Application application = context.getApplication();
      composite.setValueExpression("value", application.getExpressionFactory().createValueExpression(
              context.getELContext(), "#{stringValue.value}",
              String.class));
  
      // Populate the component with facets and child components (Optional)
      UIOutput foo = (UIOutput) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
      foo.setValue("Foo");
      composite.getFacets().put("foo", foo);
      UIOutput bar = (UIOutput) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
      bar.setValue("Bar");
      composite.getChildren().add(bar);
  
      // create composite components Root
      UIComponent compositeRoot = context.getApplication().createComponent(UIPanel.COMPONENT_TYPE);
      composite.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);
      compositeRoot.setRendererType("javax.faces.Group");
      composite.setId("compositeID");
  
      try {
          FaceletFactory factory = (FaceletFactory) RequestStateManager.get(context, RequestStateManager.FACELET_FACTORY);
          Facelet f = factory.getFacelet(componentResource.getURL());
          f.apply(context, compositeRoot); //&lt;==[here]
      } catch (Exception e) {
          log.debug("Error creating composite component!!", e);
      }
      composite.getFacets().put(
              UIComponent.COMPOSITE_FACET_NAME, compositeRoot);
  
      // attach composite component to parent componet
      viewPanel.getChildren().add(composite);
  
      // pop component from el
      composite.popComponentFromEL(context);
      if (compcompPushed) {
          ccStackManager.pop(CompositeComponentStackManager.StackType.TreeCreation);
      }
  }
  </code></pre>
  
  <p>Problem is that this code works for me only when <code>javax.faces.PROJECT_STAGE</code> is set to <code>PRODUCTION</code> (It took me all day to figure this out). If <code>javax.faces.PROJECT_STAGE</code> is set to <code>DEVELOPMENT</code> Exception is thrown on marked point (<code>&lt;==[here]</code>):</p>
  
  <pre><code>javax.faces.view.facelets.TagException: /resources/components/form/pokus.xhtml @8,19 &lt;cc:interface&gt; Component Not Found for identifier: j_id2.getParent().  
      at com.sun.faces.facelets.tag.composite.InterfaceHandler.validateComponent(InterfaceHandler.java:135)  
      at com.sun.faces.facelets.tag.composite.InterfaceHandler.apply(InterfaceHandler.java:125)  
      at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)  
      at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)  
      at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:82)  
      at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:152)  
      at cz.boza.formcreator.formcore.Try.setComponentJ(Try.java:83)  
      at cz.boza.formcreator.formcore.FormCreator.&lt;init&gt;(FormCreator.java:40)  
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)  
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)  
      at java.lang.reflect.Constructor.newInstance(Constructor.java:532)  
  </code></pre>
  
  <p>It's some problem with parent set in <code>compositeRoot</code> component (<code>j_id2</code> is automaticaly generated ID of <code>compositeRoot</code>). Also this code is not tested thoroughly enough, so I'm not sure if I can rely on it.</p>
  
  <p>I think it's very important to be able to manipulate Composite Components programmatically. Otherwise Composite Components are half useless.</p>