此代码不完整。你需要使用
FaceletContext#includeFacelet()
然后将复合组件资源包括在复合组件实现中。这是一种实用的方法。父母在身边很重要,因为这是
#{cc}
应在EL范围内创建。因此,这个实用程序方法也会立即将复合对象添加为给定父对象的子对象。此外,给复合组件一个固定的ID很重要,否则JSF将无法处理复合组件中的任何表单/输入/命令组件。
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);
}
}
因此,在您的特定示例中,请按如下方式使用它:
includeCompositeComponent(column, "comp", "test.xhtml", "someUniqueId");