Skip to main content

Programmatic creation of composite components in JSF 2.0

4 replies [Last post]
genlbm
Offline
Joined: 2004-08-13

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. 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 :-)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Ed Burns

>>>>> On Fri, 03 Apr 2009 10:13:53 -0700, Ryan Lubke said:

RL> On 3/31/09 5:50 AM, webtier@javadesktop.org wrote:

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

W> I have tried doing this inside a standard component binding i.e. with the backing bean containing the following method:

W> Facelet facelet = faceletFactory.getFacelet(compositeComponentResource.getURL());
W> facelet.apply(context.getFacesContext(), facetComponent);

RL> How recent of a build are you using to try this out?

That's not the easiest way to do it. The feature you request was
designed to be used via a call to:

Application.createComponent(FacesContext context, Resource componentResource)

where the second argument is a pointer to an Facelet page resource
within a library. See Application.getResourceHandler() for how to
create a Resource instance.

Ed

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

lexi
Offline
Joined: 2004-04-09
Ryan Lubke

On 3/31/09 5:50 AM, webtier@javadesktop.org wrote:
> 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. 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.
>
How recent of a build are you using to try this out?
> thanks in advance :-)
> [Message sent by forum member 'genlbm' (genlbm)]
>
> http://forums.java.net/jive/thread.jspa?messageID=339830
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

Lincoln Baxter, III

When you get this working, could you please post the solution? I'm
interested in doing this as well.
It would be nice if there were some provided classes for doing this
nicely... this is a BIG potential selling point and could be a BIG
distinguishing factor between JSF2 and the rest of Wicket, Struts, and
other frameworks.

--Thanks
Lincoln

On Fri, 2009-04-03 at 10:13 -0700, Ryan Lubke wrote:

> On 3/31/09 5:50 AM, webtier@javadesktop.org wrote:
> > 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. 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.
> >
> How recent of a build are you using to try this out?
> > thanks in advance :-)
> > [Message sent by forum member 'genlbm' (genlbm)]
> >
> > http://forums.java.net/jive/thread.jspa?messageID=339830
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> > For additional commands, e-mail: webtier-help@glassfish.dev.java.net
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
[att1.html]