Dear all,

how can I programatically instantiate a composite component or a tag component from a custom component ?

I proceed this way to instantiate a composite component, but I do not really like it as I use reflection to hijack FaceletViewDeclarationLanguage methods...

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;
}

How can I do the same stuff in a cleanier way ?

And how can I instantiate a tag component I mean a component declared the following way :

<tag>
    <description>blah blah</description>
    <tag-name>myWonderfulTag</tag-name>
    <source>tags/mwl/myWonderfulTag.xhtml</source>
<!-- attributes -->
</tag>

in a taglib.

Thanks in advance,

Ludovic
PS : question also asked on stackoverflow : http://stackoverflow.com/questions/15813582/how-to-programmatically-instantiate-a-composite-component-or-a-tag-component


|
| AVANT D'IMPRIMER, PENSEZ A L'ENVIRONNEMENT.
|

Reply via email to