Vorrei creare un'istanza di componenti compositi o di tag in modo programmatico.Come creare un'istanza di un componente composito o di un componente di tag in modo programmatico?
Questa istanziazione verrà eseguita da un componente personalizzato, in genere aggiungendo quei componenti compositi o di tag come bambini.
La risposta migliore che ho trovato durante la scansione dei forum è: http://www.java.net/node/701640#comment-791881. Sembra molto simile a un'altra risposta che ho trovato su questo forum: How to programmatically or dynamically create a composite component in JSF 2.
Mentre lavoravo a questa domanda, ho finalmente scritto il codice che funziona per composito instanciation usando MyFaces (gli esempi nel collegamento sembrano essere specifici di Mojarra). Copio lì come mi c'è voluto del tempo per scrivere e spero che aiutare qualcun altro:
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;
}
Si prega di non prestare attenzione alla gestione delle eccezioni brutto: è proprio generato come automaticamente da NetBeans ... I chiederò agli sviluppatori di MyFaces se c'è un modo per evitare gli attacchi di brutti riflessi.
Come posso fare lo stesso con i componenti di tag, voglio dire i componenti dichiarati come:
<tag>
<description>blah blah</description>
<tag-name>myWonderfulTag</tag-name>
<source>tags/mwl/myWonderfulTag.xhtml</source>
<!-- attributes -->
</tag>
in taglib.
Grazie in anticipo.