dynamic - Programmatically create and add composite component in backing bean -
i working dynamic dashboard users can pin , remove items like. have problem want add existing composite component view backing bean. i've tried find correct way internet no success far. here simple composite component want 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 code should return composite component:
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 here how using function:
column column = new column(); uicomponent test = htmlutil.getcompositecomponent("test.xhtml", "comp"); column.getchildren().add(test);
but nothing rendered inside column. ideas how done? don't want go rendered="#{bean.isthisrendered}" way because not fit in use case.
this code incomplete. need use faceletcontext#includefacelet()
afterwards include composite component resource in composite component implementation. here's utility method job. it's important have parent @ hands, context #{cc}
should created in el scope. utility method adds composite child of given parent. further, it's important give composite component fixed id, otherwise jsf wouldn't able process form/input/command components inside composite.
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); // creates <ui:component> based on <composite:interface>. resource resource = application.getresourcehandler().createresource(resourcename, libraryname); uicomponent composite = application.createcomponent(context, resource); composite.setid(id); // mandatory case composite part of uiform! otherwise jsf can't find inputs. // creates <composite:implementation>. uicomponent implementation = application.createcomponent(uipanel.component_type); implementation.setrenderertype("javax.faces.group"); composite.getfacets().put(uicomponent.composite_facet_name, implementation); // include composite component file in given parent. parent.getchildren().add(composite); parent.pushcomponenttoel(context, composite); // makes #{cc} available. try { faceletcontext.includefacelet(implementation, resource.geturl()); } catch (ioexception e) { throw new facesexception(e); } { parent.popcomponentfromel(context); } }
so, in particular example, use follows:
includecompositecomponent(column, "comp", "test.xhtml", "someuniqueid");
Comments
Post a Comment