2013-04-04 29 views
5

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.

Trả lời

0

ok, tôi đã tìm cách thực hiện với MyFaces. Nó cũng đòi hỏi một chút suy nghĩ xấu xí ...

public static UIComponent instantiateTagComponent(Location loc, String namespace, String localPrefix, String componentName, TagComponentParam params[]) { 
    final String ERROR_MESSAGE = "Erreur lors de l'instanciation d'un tag component"; 
    FacesContext ctx = FacesContext.getCurrentInstance(); 
    AbstractFaceletContext fctx = (AbstractFaceletContext) ctx.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY); 
    fctx.pushPageContext(new PageContextImpl()); 

    FaceletViewDeclarationLanguage vdl = new FaceletViewDeclarationLanguage(ctx); 

    UIPanel panel = (UIPanel) ctx.getApplication().createComponent(UIPanel.COMPONENT_TYPE); 

    try { 
     Method createCompiler = FaceletViewDeclarationLanguage.class.getDeclaredMethod("createCompiler",FacesContext.class); 
     Method 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); 
     FaceletFactory ff = (FaceletFactory) createFaceletFactory.invoke(vdl, ctx, compiler); 
     TagLibrary tl = compiler.createTagLibrary(); 
     TagConfig tc = new JSFUtilsTagUnit(tl, namespace, localPrefix, componentName,params,loc); 
     TagHandler th = tl.createTagHandler(namespace, componentName, tc); 
     Field declaredField = FaceletCompositionContext.class.getDeclaredField("FACELET_COMPOSITION_CONTEXT_KEY"); 
     declaredField.setAccessible(true); 
     FaceletCompositionContextImpl faceletCompositionContextImpl = new FaceletCompositionContextImpl(ff,ctx); 
     Class<?> dfcClass = Class.forName("org.apache.myfaces.view.facelets.impl.DefaultFaceletContext"); 
     Field fMCTX = dfcClass.getDeclaredField("_mctx"); 
     fMCTX.setAccessible(true); 
     fMCTX.set(fctx, faceletCompositionContextImpl); 
     FacesContext.getCurrentInstance().getAttributes().put((String)declaredField.get(null),faceletCompositionContextImpl); 
     FaceletCompositionContext mctx = (FaceletCompositionContext) FaceletCompositionContext.getCurrentInstance(fctx); 
     mctx.startComponentUniqueIdSection(); 
     th.apply(fctx, panel); 
    } catch (IOException ex) { 
     log.error(ERROR_MESSAGE, ex); 
    } catch (InvocationTargetException ex) { 
     log.error(ERROR_MESSAGE, ex); 
    } catch (NoSuchMethodException ex) { 
     log.error(ERROR_MESSAGE, ex); 
    } catch (NoSuchFieldException ex) { 
     log.error(ERROR_MESSAGE, ex); 
    } catch (SecurityException ex) { 
     log.error(ERROR_MESSAGE, ex); 
    } catch (ClassNotFoundException ex) { 
     log.error(ERROR_MESSAGE, ex); 
    } catch (IllegalArgumentException ex) { 
     log.error(ERROR_MESSAGE, ex); 
    } catch (IllegalAccessException ex) { 
     log.error(ERROR_MESSAGE, ex); 
    } 
    finally { 
     fctx.popPageContext(); 
    } 
    return panel; 
} 

Lớp JSFUtilsTagUnit là:

static class JSFUtilsTagUnit implements TagConfig { 

    private final TagLibrary library; 
    private final String namespace; 
    private final String name; 
    private Tag tag; 

    public JSFUtilsTagUnit(TagLibrary library, String namespace, String localPrefix, String name, TagComponentParam params[], Location loc) { 
     this.library = library; 
     this.namespace = namespace; 
     this.name = name; 
     if(loc == null) { 
      loc = new Location("virtual", -1, -1); 
     } 
     TagAttribute[] tas; 
     if((params != null) && (params.length > 0)) { 
      tas = new TagAttribute[params.length]; 
      int iCurrent = 0; 
      for(TagComponentParam tcpCur : params) { 
       tas[iCurrent] = new TagAttributeImpl(loc,"",tcpCur.getName(), tcpCur.getName(), tcpCur.getValue()); 
       iCurrent++; 
      } 
     } else { 
      tas = new TagAttribute[0]; 
     } 
     TagAttributes tattrs = new TagAttributesImpl(tas); 
     tag = new Tag(loc, namespace, name, localPrefix+":"+name, tattrs); 
    } 

    public FaceletHandler getNextHandler() { 
     return new FaceletHandler() { 

      public void apply(FaceletContext ctx, UIComponent parent) throws IOException { 
      } 
     }; 
    } 

    public Tag getTag() { 
     return tag; 
    } 

    public String getTagId() { 
     return "-1"; 
    } 

} 

Hy vọng nó sẽ giúp người khác!

Các vấn đề liên quan