Skip to main content

JSF 2 adding a composite components to the view from a managed bean?

2 replies [Last post]
Joined: 2009-03-11
Points: 0

Is it possible to programatically add a composite component (defined in a xhtml file) to the view ?

So I have a custom component defined in resources/components/mycomponent.xhtml

<br />
<p>...<br />

And I would like to add it to a view programatically from a managed bean using something like:

<br />
...<br />
UIComponent composite = app.createComponent(context,"components/mycomponent.xhtml");<br />
composite.getAttributes().put("foo","bar");<br />
context.findComponent("form").getChildren().add(composite);<br />
...<br />

Is this possible?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-04-09
Points: 0

Here's my research on the topic:

Might help you (I've got it working with children and facets as well).

Joined: 2009-03-11
Points: 0

The following snippet kind of does what I want.

UIComponent viewPanel = getContext()


Resource componentResource = getApplication()

FacesContext context = getContext();
UIComponent composite = context.getApplication().createComponent(context, componentResource);

UIComponent compositeRoot = getApplication().createComponent(UIPanel.COMPONENT_TYPE);
composite.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);

try {
FaceletFactory factory = (FaceletFactory)
RequestStateManager.get(context, RequestStateManager.FACELET_FACTORY);
Facelet f = factory.getFacelet(componentResource.getURL());
f.apply(context, compositeRoot);
catch (Exception e) {
// ...



Now I I just need to figure out how to populate the and elements in the facelet.