Summary

This article will show you how to build components at run time as the component is rendered, using NetAdvantage for JSF dynamically. The components will be created in the Java server-side code of the backing bean, instead of using tags to create the components in the JSP page. This is useful if either the contents or the format of the component is not known until run time.

Additional Information

For this example, we are going to create a Sidebar and dynamically add Sidebar groups and links to the Sidebar.

Although the example shows how to dynamically create Sidebar groups in a Sidebar component, the same technique should work for any JSF component.

Step-By-Step Example

The first step is to create the tags in the JSP page that will create a place holder for the Sidebar and bind it to the backing bean as shown in the code below:

In JSP:
<ig:sidebar
     id="dynGroups"
     binding="#{webbar_dynamic.sidebar}" >
</ig:sidebar>
What this does to create a Sidebar component and bind it to the backing bean. When the page gets rendered the binding="#{webbar_dynamic.sidebar}" tag will cause the setSidebar(Sidebar bar) method in the backing bean to be called.

In the backing bean, the setSidebar(Sidebar bar) needs to dynamically construct the Sidebar groups and add them to the Sidebar using the FacesContext. This process is similar to creating an XML document though code with a DOM Object: you create a set of objects and then arrange them in a hierarchical order, in the same manner as the tags would be arranged on the JSP page.

To create a Sidebar group you would first construct one in the FacesContext and set its properties as follows:

In Java:
Application application = FacesContext.getCurrentInstance().getApplication();
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();

// Create SidebarGroup, set the Text value, and expand the group
HtmlSidebarGroup aSidebarGroup = (HtmlSidebarGroup) application.createComponent(HtmlSidebarGroup.COMPONENT_TYPE);
aSidebarGroup.setText(sidebarGroupNameStr);
aSidebarGroup.setExpanded(true);
aSidebarGroup.setId(viewRoot.createUniqueId());
The next step is to create some items to add to the Sidebar group. In this example, we are going add a hyperlink.

In Java:
/* Create some Links to display in the group */
HtmlLink aLink1 = (HtmlLink)application.createComponent(HtmlLink.COMPONENT_TYPE);
aLink1.setId(viewRoot.createUniqueId());
aLink1.setUrl("http://www.infragistics.com/Corporate/press/PressReleasesViewer.aspx?Id=123");
aLink1.setValue("Infragistics NetAdvantage JSF Press Release");
aSidebarGroup.getChildren().add(aLink1);
The final line in the last set of code aSidebarGroup.getChildren().add(aLink1); adds to the link as a child member of the Sidebar group.

The last step is to add the Sidebar group with its link to the Sidebar.

In Java:
// Add the newly created SidebarGroup to the Sidebar's children list
bar.getChildren().add(aSidebarGroup);
Setting up the sample:
After downloading the sample using the link below, put the contents in the \documentation\tutorials folder underneath the installation directory for NetAdvantage for JSF. The sample works the same as all the other tutorials, and has a build.xml file that will allow you to build and run the sample.

Samples

dynamicsidebargroup_tutorial.zip
 Demonstrates how to dynamically add groups to a JSF Sidebar component

The information in this article applies to:
JSF WebBar (v6.1.20061)


Screenshot: