title: jsf dynamic component that restores state
body: <p>I am trying to display HtmlInputText dynamically in a JSF page. However, I am getting </p>
  
  <pre><code>javax.faces.FacesException: Cannot add the same component twice: j_idt10:hitDyn
  </code></pre>
  
  <p>During the first request to the page the input text renders well. That exception happens during postback of the page, when I enter some text in the input component and press Enter.
  In the .xhtml page, I have the following code:</p>
  
  <pre><code>&lt;h:form&gt;
      &lt;h:outputLabel value="Welcome!"&gt;&lt;/h:outputLabel&gt;
      &lt;f:metadata&gt;
          &lt;f:event type="preRenderView" listener="#{dynamicBacking.addDynComp}" /&gt;
      &lt;/f:metadata&gt;
      &lt;h:panelGroup id="dynOuter"&gt;&lt;/h:panelGroup&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>In the backing bean, I have the following code:</p>
  
  <pre><code>@ManagedBean(name="dynamicBacking")
  public class DynamicBacking {
      public void addDynComp() {
          Application app = FacesContext.getCurrentInstance().getApplication();
          HtmlInputText hit = (HtmlInputText)app.createComponent(HtmlInputText.COMPONENT_TYPE);
          hit.setId("hitDyn");
          UIComponent parent = findComponent("dynOuter");
          if( parent != null ) {
              parent.getChildren().add(hit);
          }
      }
  
      public UIComponent findComponent(final String id) {
          FacesContext context = FacesContext.getCurrentInstance(); 
          UIViewRoot root = context.getViewRoot();
          final UIComponent[] found = new UIComponent[1];
          root.visitTree(new FullVisitContext(context), new VisitCallback() {     
              @Override
              public VisitResult visit(VisitContext context, UIComponent component) {
                  if(component.getId().equals(id)){
                      found[0] = component;
                      return VisitResult.COMPLETE;
                  }
                  return VisitResult.ACCEPT;              
              }
          });
          return found[0];
      }
  }
  </code></pre>
  
  <p>I guess that there is some problem with restoring the state of the dynamic component in a postback. Am I adding the dynamic component too late in the lifecycle of the JSF page? I know that in ASP.NET I could add a dynamic control during Page.Load phase. But I can't so far figure out how to achieve the same in JSF. Please, help!</p>