Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to programmatically instantiate a composite component or a tag component?
    primarykey
    data
    text
    <p>I would like to programmatically instantiate composite or tag components.</p> <p>This instantiation would be performed by a custom component, typically adding those composite or tag components as children.</p> <p>The best answer I found when crawling forums is : <a href="http://www.java.net/node/701640#comment-791881" rel="nofollow noreferrer">http://www.java.net/node/701640#comment-791881</a> . It looks a lot like another answer I found on this forum : <a href="https://stackoverflow.com/questions/5370184/how-to-programmatically-or-dynamically-create-a-composite-component-in-jsf-2">How to programmatically or dynamically create a composite component in JSF 2</a> .</p> <p>While working on this question, I finally wrote code that works for <em>composite</em> instanciation using MyFaces (examples in link seems to be Mojarra specific). I copy it there as it took me some time to write it and hope it will help someone else :</p> <pre><code>public UIComponent instantiateComposite(String namespace, String componentName) { FacesContext ctx = FacesContext.getCurrentInstance(); Resource resource = ctx.getApplication().getResourceHandler().createResource( componentName + ".xhtml", namespace ); UIComponent cc = ctx.getApplication().createComponent( ctx, resource ); UIPanel panel = (UIPanel) ctx.getApplication().createComponent( UIPanel.COMPONENT_TYPE ); // set the facelet's parent cc.getFacets().put( UIComponent.COMPOSITE_FACET_NAME, panel ); FaceletFactory ff = (DefaultFaceletFactory) DefaultFaceletFactory.getInstance(); if(ff == null) { FaceletViewDeclarationLanguage vdl = new FaceletViewDeclarationLanguage(ctx); Method createCompiler = null; Method createFaceletFactory = null; try { createCompiler = FaceletViewDeclarationLanguage.class.getDeclaredMethod("createCompiler",FacesContext.class); createFaceletFactory = FaceletViewDeclarationLanguage.class.getDeclaredMethod("createFaceletFactory",FacesContext.class,org.apache.myfaces.view.facelets.compiler.Compiler.class); createCompiler.setAccessible(true); createFaceletFactory.setAccessible(true); org.apache.myfaces.view.facelets.compiler.Compiler compiler = (org.apache.myfaces.view.facelets.compiler.Compiler) createCompiler.invoke(vdl, ctx); ff = (FaceletFactory) createFaceletFactory.invoke(vdl, ctx, compiler); } catch (IllegalAccessException ex) { Logger.getLogger(SenatDataTableEntryDetail.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(SenatDataTableEntryDetail.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(SenatDataTableEntryDetail.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException ex) { Logger.getLogger(SenatDataTableEntryDetail.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(SenatDataTableEntryDetail.class.getName()).log(Level.SEVERE, null, ex); } } try { Facelet facelet = ff.getFacelet(resource.getURL()); facelet.apply( ctx, panel ); } catch ( IOException e ) { e.printStackTrace(); } return cc; } </code></pre> <p>Please do not pay attention to ugly exception handling : it is just as automatically generated by netbeans... I will ask MyFaces developpers if there is a way to avoid ugly reflection hacks.</p> <p>How can I do the same with tag components , I mean components declared like :</p> <pre><code>&lt;tag&gt; &lt;description&gt;blah blah&lt;/description&gt; &lt;tag-name&gt;myWonderfulTag&lt;/tag-name&gt; &lt;source&gt;tags/mwl/myWonderfulTag.xhtml&lt;/source&gt; &lt;!-- attributes --&gt; &lt;/tag&gt; </code></pre> <p>in taglib.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload