2013-04-05 108 views
7

我正在使用动态仪表板,用户可以根据需要固定和删除项目。现在我有一个问题,我想从支持bean中将现有的复合组件添加到视图中。我试图找到从互联网上做到这一点的正确方法,但迄今为止没有成功。下面是简单的复合组件我想补充:以编程方式在支持bean中创建和添加复合组件

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:cc="http://java.sun.com/jsf/composite" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:p="http://primefaces.org/ui" 
     xmlns:composite="http://java.sun.com/jsf/composite"> 
    <!-- INTERFACE --> 
    <cc:interface> 

    </cc:interface> 

    <!-- IMPLEMENTATION --> 
    <cc:implementation> 
     <h:outputText value="TEST"/> 
    </cc:implementation> 
</html> 

这里是一个要返回复合组件的代码:

public static UIComponent getCompositeComponent(String xhtml, String namespace) { 
    FacesContext fc = FacesContext.getCurrentInstance(); 
    Application app = fc.getApplication(); 
    Resource componentResource = app.getResourceHandler().createResource(xhtml, namespace); 

    UIPanel facet = (UIPanel) app.createComponent(UIPanel.COMPONENT_TYPE); 
    facet.setRendererType("javax.faces.Group"); 
    UIComponent composite = app.createComponent(fc, componentResource); 
    composite.getFacets().put(UIComponent.COMPOSITE_FACET_NAME, facet); 

    return composite; 
} 

,这里是我如何使用功能:

Column column = new Column(); 
UIComponent test = HtmlUtil.getCompositeComponent("test.xhtml", "comp"); 
column.getChildren().add(test); 

但是在列内没有任何内容。任何想法如何做到这一点?我不想用render =“#{bean.isThisRendered}”的方式,因为它不适合我的用例。

+0

出于兴趣(题目是可怕的严重覆盖,这会给我带来前进光年):你在哪里调用

public static void includeCompositeComponent(UIComponent parent, String libraryName, String resourceName, String id) { // Prepare. FacesContext context = FacesContext.getCurrentInstance(); Application application = context.getApplication(); FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY); // This basically creates <ui:component> based on <composite:interface>. Resource resource = application.getResourceHandler().createResource(resourceName, libraryName); UIComponent composite = application.createComponent(context, resource); composite.setId(id); // Mandatory for the case composite is part of UIForm! Otherwise JSF can't find inputs. // This basically creates <composite:implementation>. UIComponent implementation = application.createComponent(UIPanel.COMPONENT_TYPE); implementation.setRendererType("javax.faces.Group"); composite.getFacets().put(UIComponent.COMPOSITE_FACET_NAME, implementation); // Now include the composite component file in the given parent. parent.getChildren().add(composite); parent.pushComponentToEL(context, composite); // This makes #{cc} available. try { faceletContext.includeFacelet(implementation, resource.getURL()); } catch (IOException e) { throw new FacesException(e); } finally { parent.popComponentFromEL(context); } } 

所以,在你的具体的例子,按如下方式使用它3行调用'HtmlUtil.getCompositeComponent'? – 2017-06-21 16:27:56

回答

11

此代码不完整。之后需要使用FaceletContext#includeFacelet()将复合组件资源包含在复合组件实现中。这是一个实用的方法。让父母掌握这一点非常重要,因为这是在EL范围内创建#{cc}的上下文。因此,此实用程序方法也会立即将组合作为给定父项的子项添加。此外,为复合组件提供一个固定的ID非常重要,否则JSF将无法处理组合内的任何窗体/输入/命令组件。

includeCompositeComponent(column, "comp", "test.xhtml", "someUniqueId"); 
+0

非常有趣!是否可以使用这种方法(可能有点改变)来包含外部组件?我不想使用现有资源,而是希望包含未在应用程序中定义的组件。 – fnst 2013-04-10 08:45:54

+1

@fnst:如果需要,可以使用自定义资源处理程序控制资源处理。只要你最终得到一个有效的'URL',它就可以基本指向所有的东西(包括'file://','http://'等等,甚至自定义的协议,以便例如DB访问在理论上是可能的) 。 – BalusC 2013-04-10 10:57:06

+0

感谢您的回答,必须尝试:)是否也可以将值表达式作为属性添加到复合组件中? – drodil 2013-04-11 11:45:39

相关问题