webtier@glassfish.java.net

Programmatic creation of composite components in JSF 2.0

From: <webtier_at_javadesktop.org>
Date: Tue, 31 Mar 2009 05:50:59 PDT

Hi all

With the cool JSF 2.0 composite component feature coming along, I would like to know if it is possible to programmatically add composite components to the component tree ?

I have tried doing this inside a standard component binding i.e. <h:form binding="bb.setComponent"/> with the backing bean containing the following method:

public void setComponent(UIComponent component) {
UIComponent compositeComponent = application.createComponent(context, resource);
UIPanel facetComponent = application.createComponent(UIPanel.COMPONENT_TYPE);
facetComponent.setRendererType("javax.faces.Group");
compositeComponent.getFacets().put(UIComponent.COMPOSITE_FACET_NAME, facetComponent);

FaceletContext faceletContext = (FaceletContext)context.getFacesContext().getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
FaceletFactory faceletFactory = (FaceletFactory)RequestStateManager.get(context.getFacesContext(), RequestStateManager.FACELET_FACTORY);
                
VariableMapper originalVariableMapper = faceletContext.getVariableMapper();

faceletContext.setVariableMapper(new VariableMapperWrapper(originalVariableMapper));
try {
  Facelet facelet = faceletFactory.getFacelet(compositeComponentResource.getURL());
  facelet.apply(context.getFacesContext(), facetComponent);
} finally {
  faceletContext.setVariableMapper(originalVariableMapper);
}

component.getChildren().add(compositeComponent);
}

However this fails when performing a postback as the facelet context and factory are not present.

For our purposes it would be really great if someone can provide instructions on how to do this programmatically.

thanks in advance :-)
[Message sent by forum member 'genlbm' (genlbm)]

http://forums.java.net/jive/thread.jspa?messageID=339830