Skip to main content

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

2 replies [Last post]
janderssn
Offline
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>
<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.
lexi
Offline
Joined: 2004-04-09
Points: 0

Here's my research on the topic:

http://forums.java.net/jive/thread.jspa?threadID=75740

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

janderssn
Offline
Joined: 2009-03-11
Points: 0

The following snippet kind of does what I want.

[code]
UIComponent viewPanel = getContext()
.getViewRoot()
.findComponent("form:viewPanel");

viewPanel.getChildren().clear();

Resource componentResource = getApplication()
.getResourceHandler()
.createResource("custom.xhtml","components/custom");

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);
compositeRoot.setRendererType("javax.faces.Group");

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

viewPanel.getChildren().add(compositeRoot);

[/code]

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