मैं प्रोग्रामेटिक रूप से समग्र या टैग घटकों को तुरंत चालू करना चाहता हूं।एक समग्र घटक या टैग घटक को प्रोग्रामेटिक रूप से कैसे चालू करें?
यह तत्कालता एक कस्टम घटक द्वारा की जाएगी, आमतौर पर उन समग्र या टैग घटकों को बच्चों के रूप में जोड़ना।
मंचों को क्रॉल करते समय मैंने पाया सबसे अच्छा जवाब है: http://www.java.net/node/701640#comment-791881। यह इस मंच पर मिले एक और जवाब की तरह दिखता है: How to programmatically or dynamically create a composite component in JSF 2।
इस प्रश्न पर काम करते समय, मैंने आखिरकार कोड लिखा था जो मिश्रित माइफेस का उपयोग करके instanciation (लिंक में उदाहरण Mojarra विशिष्ट प्रतीत होता है) के लिए काम करता है। मैं इसे वहाँ कॉपी के रूप में यह मुझे कुछ समय लगा यह लिखने के लिए और आशा है कि यह मदद मिलेगी किसी और:
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;
}
कृपया बदसूरत अपवाद संचालन पर ध्यान देना नहीं है: यह बस के रूप में स्वचालित रूप से NetBeans द्वारा उत्पन्न होता है ... मैं अगर बदसूरत प्रतिबिंब हैक से बचने का कोई तरीका है तो माईफेस डेवलपर से पूछेगा।
<tag>
<description>blah blah</description>
<tag-name>myWonderfulTag</tag-name>
<source>tags/mwl/myWonderfulTag.xhtml</source>
<!-- attributes -->
</tag>
taglib में
:
मैं टैग घटकों के साथ भी ऐसा ही कर सकते हैं कैसे, मैं घटकों की तरह घोषित मतलब है।
अग्रिम धन्यवाद।