title: JSF ui:fragment rendered performance
body: <p>I have a set of jsf components that are statically generated from a set of excel files (they are updated by business people). Each generated file represents a business object that has slightly different data, and all of them belong to a same class.</p>
  
  <p>In order to render this dynamically, the only solution I found was to set up a bunch of <code>ui:fragment</code> and dispatch to the right component at runtime:</p>
  
  <p>
      
          
          
      </p>
  
  <pre><code>&lt;!-- IMPLEMENTATION --&gt;          
  &lt;composite:implementation&gt; 
      &lt;ui:fragment rendered="#{cc.attrs.type eq 'cartcred'}"&gt;
          &lt;limites:limites-cartcred  limite="#{cc.attrs.limite}"/&gt;
      &lt;/ui:fragment&gt;
      &lt;ui:fragment rendered="#{cc.attrs.type eq 'cdcp'}"&gt;
          &lt;limites:limites-cdcp limite="#{cc.attrs.limite}"/&gt;
      &lt;/ui:fragment&gt;
      &lt;ui:fragment rendered="#{cc.attrs.type eq 'cheqpredatado'}"&gt;
          &lt;limites:limites-cheqpredatado limite="#{cc.attrs.limite}"/&gt;
      &lt;/ui:fragment&gt;
      &lt;ui:fragment rendered="#{cc.attrs.type eq 'confirming'}"&gt;
          &lt;limites:limites-confirming limite="#{cc.attrs.limite}"/&gt;
      &lt;/ui:fragment&gt;
     &lt;!-- many more lines --&gt;
     &lt;!-- many more lines --&gt;
     &lt;!-- many more lines --&gt;
      &lt;ui:fragment rendered="#{cc.attrs.type eq 'contacorr'}"&gt;
          &lt;limites:limites-contacorr limite="#{cc.attrs.limite}"/&gt;
      &lt;/ui:fragment&gt;
  </code></pre>
  
  <p></p>
  
  <p>But I found out that the perfomance of this is terrible. I tought that JSF would only render a single component, but it seems that it is rendering <strong>all</strong> of them and "hiding" the others at runtime.</p>
  
  <p>Is there a more efficient way of achieving my goal? I want to render <strong>a single component</strong> based on runtime information about a business class (much like an if-then-else), but I can only determine what is the component to render at runtime.</p>
  
  <hr>
  
  <p><strong>Clarification:</strong>
  what happens is that each component referenced by <code>limites:limites*</code> is a huge complex page with lots of other components. At runtime, the parameter named <code>type' will decide what component to render. But my tests show that if I only render one component, but leave the other</code>ui:fragments` (even knowing that they will not be rendered), it will render <em>much</em> slower than if I remove the components.</p>
  
  <p>So if my page is exactly like this:</p>
  
  <pre><code>&lt;composite:interface&gt;
      &lt;composite:attribute name="type" required="true" /&gt;
      &lt;composite:attribute name="limite" required="true" /&gt;
  &lt;/composite:interface&gt;         
  &lt;composite:implementation&gt; 
      &lt;ui:fragment rendered="#{cc.attrs.type eq 'cartcred'}"&gt;
          &lt;limites:limites-cartcred  limite="#{cc.attrs.limite}"/&gt;
      &lt;/ui:fragment&gt;
  &lt;/composite:implementation&gt;
  </code></pre>
  
  <p>it will render <em>much</em> (around 10x) faster than the initial version, even though the parameters are the same. I suspect that JSF will create the entire component tree and only at runtime it will decide (depending on the supplied parameter) if it will render each other or not.</p>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p>Almost there. I just need to include my <strong>composite component</strong> <em>dynamically</em>. I tried evaluating an ELExpression but that didn't work. What I need is a way of accessing the current scope within the component creation, and using that to generate the proper file name:</p>
  
  <pre><code>//obviously, ELExpressions don't work here
  Resource resource = application.getResourceHandler().createResource("file-#{varStatus.loop}.xhtml", "components/dynamicfaces");
  </code></pre>