Programmatically create and add a composite component to a bean backup

I work with a dynamic panel where users can attach and remove items as they wish. Now I have a problem that I want to add an existing compound component to the view from a backup bean. I tried to find the right way to do this from the Internet, but so far have not achieved anything. Here is a simple composite component that I want to add:

<?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> 

Here is the code that the composite component should return:

 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; } 

And this is how I use the function:

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

But nothing is displayed inside the column. Any ideas how to do this? I do not want to go with the rendered = "# {bean.isThisRendered}" method, because it does not fit in my use case.

+7
source share
1 answer

This code is incomplete. After that, you will need FaceletContext#includeFacelet() to include the component component component into the component component implementation. Here is a useful method that does the work. It is important to have a parent in hand, as this is the context in which #{cc} must be created in the EL area. Thus, this utility method also immediately adds the composite as a child of this parent. In addition, it is important to provide the composite component with a fixed identifier, otherwise JSF will not be able to process any form / input / command components within the composite element.

 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); } } 

So, in your specific example, use it like this:

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

All Articles