Creating custom JSF parent component

You may wish to create a JSF component that can contain other child JSF components.
Words are very nice but how to implement this idea ???
Assume we want to create a component that can hide and show the child components based upon a specific attribute. Let call our component (parentTag) and the attribute (visible).
For example)
<c:parentTag visible="#{myBean.visible}">
<h:commandButton .../>
<h:inputText .../>
<!--Other JSF components here-->
</c:parentTag>
If #{myBean.visible} is true then the child components will be visible else they won't appear.

The main trick is here at the Renderer class using the
getRendersChildren( ) and encodeChildren( ) methods.

Let's see how to make this ...
//The main trick is here at the Renderer class ...
class ParentTagRenderer extends Renderer {
//Must override this method to tell that you are a parent component ...
public boolean getRendersChildren() {
return true;
}

//Here is the method that is responsible for rendering the child components ...
public void encodeChildren(FacesContext context, UIComponent component)
throws IOException {
String visible = (String) component.getAttributes().get("visible");
//If the value is true then render the childrens ...
if ("true".equalsIgnoreCase(visible)) {
super.encodeChildren(context, component);
}
}
/*Other Renderers methods ...*/
}

0 comments: