Dynamically adding components to a custom composite JSF component
December 13, 2005 4 Comments
When writing a custom JSF component, you might want to include other components in yours. JSF provides different ways to achieve this, the hard way is to encode-decode the other component you want to include. The easy way is to create and add the component programatically you want to include, to your composite component. For example you want to add a input text component dynamically.
private void encodeInputText(FacesContext context) {
HtmlInputText inputText = new HtmlInputText();
inputText.setParent(this);
inputText.setId(this.getId() + “:_text1”);
inputText.encodeBegin(context);
inputText.encodeEnd(context);
}
This code will render an input text to the page, in this example you can also manipulate several properties of the inputText before calling encode, you can set converters, validators, style classes, and other component specific attributes to inputText here without coding html. Calling encode will give this job to the renderer of the inputText.
Updated: After adding the child component dynamically, like the first way, the encoding methods must be called in order to render the children.
Hi,
I’m looking into including your code guidelines into a project, but cannot seem to make it work. Could you give me aclue please?
I’m trying to dynamically add a children element, a standard commandLink component from jsf framework, but the action binding refuses to work …
I’ve created a custom jsf component, that does nothing at all.
For example, one would use it like:
So my UI component class would have the following methods:
The tld file is created, the tag class is working. All the component is working properly.
Now I want to add a link as a child component, but I wat to do it programatically, so I changed the encodeBegin method of my custom ui class:
This seems to work, but not quite … the link appears as expected, the value references for value and style are correctly passed, but the action doesn’t work at all.
For example if I change to:
And I have the following method created on my managed bean:
Is successfully calls and retrieves the value from the method. Same happens to the style property.
Now for the action, if I change to:
And I have the following method created on my managed bean:
The result is nothing … I mean the method is not called at all. The “backup” is properly defined on my “faces-config.xml”:
If I create the link manually on the web page:
It does work (the backup.jsp page is shown), so the methods are properly configured, only the action binding does not work.
Wether I use a string “backup” or a reference “#{mybean.doMyLinkAction}” I cannot make it work.
On the console I get the following results, for each value I test (string “backup” or reference “#{mybean.doMyLinkAction}”):
So the “if (CommandLinkTag.isValueReference( myLinkAction )) {” is working properly … that just leaves me with the action method binding instructions …
Why don’t they work?
Any Help Appreciated … Thanks in Advance!
I dunno how you did it, I tried the getChildren().add() approach and nothing at all gets rendered. It’s extremely frustrating to read all over the place that it’s really easy to implement your own JSF composite components when in fact it plainly isn’t.
I’ve no idea what I’m doing wrong!
How about the first approach? Also it is known that dynamically added components will not “behave” as you may expect.
defrert