Tôi muốn lập trình nhanh các thành phần hỗn hợp hoặc thẻ.Làm thế nào để lập trình nhanh một thành phần hỗn hợp hoặc một thành phần thẻ?
Sự khởi tạo này sẽ được thực hiện bởi thành phần tùy chỉnh, thường thêm các thành phần hỗn hợp hoặc thẻ đó làm trẻ em.
Câu trả lời hay nhất tôi tìm thấy khi thu thập thông tin diễn đàn là: http://www.java.net/node/701640#comment-791881. Nó trông giống như một câu trả lời tôi tìm thấy trên diễn đàn này: How to programmatically or dynamically create a composite component in JSF 2.
Trong khi làm việc về câu hỏi này, cuối cùng tôi đã viết mã hoạt động cho tổng hợp instanciation sử dụng MyFaces (ví dụ trong liên kết có vẻ là Mojarra cụ thể). Tôi sao chép nó ở đó vì nó đã cho tôi một thời gian để viết nó và hy vọng nó sẽ giúp người khác:
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;
}
Xin đừng chú ý đến việc xử lý ngoại lệ xấu xí: nó chỉ là tự động tạo ra bởi netbeans ... Tôi sẽ hỏi MyFaces developpers nếu có một cách để tránh hacks phản chiếu xấu xí.
Làm thế nào tôi có thể làm tương tự với thành phần thẻ, tôi có nghĩa là thành phần tuyên bố như:
<tag>
<description>blah blah</description>
<tag-name>myWonderfulTag</tag-name>
<source>tags/mwl/myWonderfulTag.xhtml</source>
<!-- attributes -->
</tag>
trong taglib.
Xin cảm ơn trước.