Skip to main content

adding composite components dynamically

2 replies [Last post]
Joined: 2009-03-28

I'm trying to add a composite component to a page dynamically.

mypage.xhtml contains this: (a managed, request scoped bean) contains this:

public void addTheComponents(ComponentSystemEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
Resource componentResource = context.getApplication().getResourceHandler().createResource("mycomp.xhtml", "components");
UIComponent component = context.getApplication().createComponent(context, componentResource);

/resources/components/mycomp.xhtml is just a very simple composite component with an h:outputText. It works correctly when referenced directly in mypage.xhtml.

When I load the page, addTheComponents is called, the componentResource.getURL() looks correct (it's jndi:/server/Jsf2Test/resources/components/mycomp.xhtml), but the newly created component seems to be incomplete. Looking at it in the debugger, it's got null for just about every attribute except parent (after the add()). Id and children are both null, which seems weird. Aside from that oddity, addTheComponents() completes without issue. However, when the view is rendered, I get an exception: PENDING_I18N: Unable to find composite component root for composite component with id null and class javax.faces.component.UINamingContainer
at com.sun.faces.renderkit.html_basic.CompositeRenderer.encodeChildren(
at javax.faces.component.UIComponentBase.encodeChildren(
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(
at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(
at javax.faces.component.UIComponentBase.encodeChildren(
at javax.faces.component.UIComponent.encodeAll(
at javax.faces.component.UIComponent.encodeAll(
at javax.faces.component.UIComponent.encodeAll(
at javax.faces.component.UIComponent.encodeAll(
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(
at com.sun.faces.application.view.MultiViewHandler.renderView(
at com.sun.faces.lifecycle.RenderResponsePhase.execute(
at com.sun.faces.lifecycle.Phase.doPhase(
at com.sun.faces.lifecycle.LifecycleImpl.render(
at javax.faces.webapp.FacesServlet.service(
at org.apache.catalina.core.StandardWrapper.service(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.core.StandardPipeline.invoke(
at com.sun.enterprise.web.WebPipeline.invoke(
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.doService(
at org.apache.catalina.connector.CoyoteAdapter.service(
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(
at com.sun.grizzly.http.ProcessorTask.doProcess(
at com.sun.grizzly.http.ProcessorTask.process(
at com.sun.grizzly.http.DefaultProtocolFilter.execute(
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(
at com.sun.grizzly.DefaultProtocolChain.execute(
at com.sun.grizzly.DefaultProtocolChain.execute(
at com.sun.grizzly.http.HttpProtocolChain.execute(
at com.sun.grizzly.ProtocolChainContextTask.doCall(
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(
at java.util.concurrent.ThreadPoolExecutor$

What do I do to dynamically add a composite component? Should I be listening for a different event and adding it there, or am I doing something else wrong?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2009-03-11

You should look at chapter 3.6 of the specification, which describes the composite component.

The following works for simple composite component, but not with components using and similar tags...

I wish there was an easier way to work with composite components in java, but this is the only one I've found so far.

Joined: 2009-03-28

Thanks for the information! I've been looking all over for documentation and have not been able to find anything useful, but the link you gave me is great.

Your code gave me a lot of ideas to search for. Have you looked at jsf-ri\src\com\sun\faces\facelets\tag\jsf\CompositeComponentTagHandler, particularly the method applyCompositeComponent? It is pretty similar to your code, which makes me think that to fully support dynamic composite components, one would need to replicate pretty much the entire class. I don't know how much of it is implementation specific and how much is pure API though.

Alternatively, I wonder if it's possible to trigger the built-in behavior by publishing an event or manually calling some of the phase handlers on the newly created but empty composite component. Otherwise it seems like too much work to be the right way.