IBM® Faces components are part of the JavaServer™ Faces Widget Library (JWL). They allow you to build a Web application with rich components to display complex arrangements of data. The resulting application has no footprint, because it uses only HTML, CSS, and JavaScript™. The IBM Faces components are an extension of JavaServer Faces (JSR 127). The IBM® Rational® Application Developer Version 7.0 tooling allows rapid application development with these components by using drag-and-drop operations. The dataTable component allows the user to display tabular data of any type, including text, images, combination boxes, and more. This article explains how to create an application with a JWL dataTable and how to use the new features introduced for the dataTable in Rational Application Developer V7.0.
The following steps explain how to create a Web application with a JSF dataTable
Create a new dynamic Web project in Rational Application Developer
- In the File menu, click New > Dynamic Web Project.
- Enter a project name, such as
MyProjectin the Project Name box. - Choose a Target Runtime of IBM® WebSphere® Application Server V6.1.
- In the Configurations context menu, choose Faces Project.
- Click Add Project to an EAR and enter an EAR file name that is the same as the project name that you just entered, but with EAR appended. For example:
MyProjectEAR. - Click Next.
- Ensure that Base Faces Support, Enhanced Faces Components, and JSTL are active.
- Click Finish to create the dynamic Web project.
- A WebDiagram.gph file opens by default.
- Close this file, because you are not going to use this feature now.
- In the new project, right-click the Web Content folder and select New Web Page.
- Enter a
name for the pagein the File Name box. - Click Finish to add the JavaServer Page™ (JSP) to the dynamic Web project.
Import the Java bean to use as your data source
- Download the SampleModel.zip file at the end of this article (see Resources).
- This .zip file contains sample JavaBeans™ with data that you will use to display in the dataTable.
- In the Import box, enter
Archivein the Select an Import Source box. This should filter the list below that box to contain only Archive file. - Click Archive file, and then click Next.
- Click the top Browse button to locate the SampleModel.zip file that you just downloaded.
- Make sure that all of the folders that appear in the left pane are selected.
- Click the bottom Browse button to choose the folder that you want to import the files into.
- Select the src folder in your project.
- Click Finish to import the JavaBeans into your project.
- The classes available to your application are structured as Figure 1 shows.
Figure 1. The JavaBeans view
You can now create page data to use in your project from the JavaBeans that you just imported. You will create page data using Root.java from the samplemodel.zip file as the root class of your page data.
- In the Page Data view, right-click Page Bean and select New > JavaBean.
- Type a name for the bean in the Name box, in this case:
mybean. - Click the button beside the Class box and type in the name of the root class. For this example, type
Root. - Click Finish to create the page data.
- In the Page Data view, expand the mybean root node.
- Select the Stocks array, and drag it onto the JSP. This will create a dataTable on the page that will display the content of the Stocks array.
- A dialog box will give you the option to configure which columns are displayed.
- Click Finish to accept the default dataTable settings.
- The Rational Application Developer tooling will create a dataTable on the JSP that displays the columns of the Stock array.
- Switch to the Source view of your JSP.
The dataTable source should look like code Listing 1.
Listing 1. dataTable source code
<hx:dataTableEx id="tableEx1" value="#{pc_File1.mybean.stocks}"
var="varstocks" styleClass="dataTableEx" headerClass="headerClass"
footerClass="footerClass" rowClasses="rowClass1, rowClass2"
columnClasses="columnClass1" border="0" cellpadding="2"
cellspacing="0">
<hx:columnEx id="columnEx1">
<f:facet name="header">
<h:outputText styleClass="outputText" value="Change"
id="text1"></h:outputText>
</f:facet>
<h:outputText id="textChange1" value="#{varstocks.change}"
styleClass="outputText">
</h:outputText>
</hx:columnEx>
<hx:columnEx id="columnEx2">
<f:facet name="header">
<h:outputText styleClass="outputText" value="CurrentPrice"
id="text2"></h:outputText>
</f:facet>
<h:outputText id="textCurrentPrice1"
value="#{varstocks.currentPrice}" styleClass="outputText">
</h:outputText>
</hx:columnEx>
<hx:columnEx id="columnEx3">
<f:facet name="header">
<h:outputText styleClass="outputText" value="Symbol"
id="text3"></h:outputText>
</f:facet>
<h:outputText id="textSymbol1" value="#{varstocks.symbol}"
styleClass="outputText">
</h:outputText>
</hx:columnEx>
<hx:columnEx id="columnEx4">
<f:facet name="header">
<h:outputText styleClass="outputText" value="Volume"
id="text4"></h:outputText>
</f:facet>
<h:outputText id="textVolume1" value="#{varstocks.volume}"
styleClass="outputText">
</h:outputText>
</hx:columnEx>
<hx:columnEx id="columnEx5">
<f:facet name="header">
<h:outputText styleClass="outputText" value="Percentage"
id="text5"></h:outputText>
</f:facet>
<h:outputText id="textPercentage1" value="#{varstocks.percentage}"
styleClass="outputText">
</h:outputText>
</hx:columnEx>
</hx:dataTableEx>
|
Run your dataTable on the server
- Save all of the changes that you have made to your project.
- Choose Clean from the Project menu in the toolbar.
- Within the Servers tab, start the IBM® WebSphere® Application Server V6.1 application by clicking the green Start the Server icon.
- In the Project Explorer view, right-click the JSP file that you just developed in the WebContent folder, and then choose Run As > Run on Server.
- In the context manu, select WebSphere Application Server V6.1, and then click Finish.
- The JSP will run in the internal browser in the Rational Application Developer tool, as Figure 2 shows.
Figure 2. dataTable running on server
Add a Deluxe Pager with new features
The Deluxe Pager is not a new component for the dataTable, but more features have been introduced in Rational Application Developer V7.0.
- Go back to the Design view for your JSP.
- Click your dataTable to select it.
- Choose the Rational Application Developer Properties view to see the dataTable's properties.
- Click the Display options tab, which is a sub-tab of the hx:dataTableEx tab.
- In the Rows per page text field, enter
2, so that you will have a need for a pager. - Next, click Add a deluxe pager.
- This will create a footer for the dataTable and add the Deluxe Pager in the dataTable's footer.
The Deluxe Pager tag and footer added to the dataTable will look like Listing 2.
Listing 2. Deluxe Pager tag and footer added to the dataTable
<f:facet name="footer"> <hx:panelBox id="box1" styleClass="panelBox"> <hx:pagerDeluxe id="deluxe1" styleClass="pagerDeluxe" /> </hx:panelBox> </f:facet> |
Now, when you run the JSP page on server, the dataTable will use the default Deluxe Pager, as Figure 3 shows. Also see Table 1 for a list of the new attributes of the Deluxe Pager tag.
Figure 3. dataTable running on the server with the Deluxe Pager component
Table 1. New attributes of the <hx:deluxePager> tag
| Attribute Name | Values | Description |
|---|---|---|
| showOnlyNextPrevious | true/false | Setting this attribute to true will hide the first and last buttons of the Deluxe Pager. |
| hideDisabledButtons | true/false | Setting this attribute to true will hide the disabled buttons. |
You can set these new attributes manually in the JSP source view..
- Go back to the Design view for your JSP.
- Select the Deluxe Pager.
- Choose the Rational Application Developer Properties view to see the Deluxe Pager's properties.
- On the hx:pagerDeluxe tab in the Properties view, there are two check boxes to select to turn on these new features:
- Check the box beside Hide 'First' and 'Last' buttons to set showOnlyNextPrevious to Hide 'First' and 'Last' buttons to true.
- Check the box beside Hide disabled buttons to set hideDisabledButtons to true.
Note: Alternatively, you can set the new features in the tooling provided with Rational Application Developer V7.0 by following these steps:
- Go to the Source view for your JSP.
- Locate the <hx:pagerDeluxe> tag.
- Add an attribute of
showOnlyNextPrevious="true"to the tag. - Add an attribute of
hideDisabledButtons="true"to the tag.
When you run your dataTable on the server, the resulting dataTable looks like the one shown in Figure 4. The "first" and "last" buttons are not shown, because you specified that you want to show only the next and previous buttons. However, the previous button doesn't show either, because you are on the first page of the table, where the previous button is normally disabled, and you chose to hide disabled buttons.
Figure 4. Deluxe Pager with new attributes
Add the new features in Output Statistics
Output Statistics is not a new component for the dataTable, but it offers more features starting with Rational Application Developer V7.0.
Add a <hx:outputStatistics> tag
- Go back to the Design view for your JSP.
- Click on your dataTable to select it.
- Choose the Rational Application Developer Properties view to see the dataTable's properties.
- Click the Display options tab, which is a sub-tab of the hx:dataTableEx tab.
- Select Add page information. This adds the Output Statistics beside the Deluxe Pager in the dataTable's footer.
The code that adds the Output Statistics tag to the dataTable footer looks like Listing 3.
Listing 3. The Output Statistics tag added to the dataTable footer
<hx:outputStatistics id="statistics1" styleClass="outputStatistics"> </hx:outputStatistics> |
When you run the JSP page on server now, the dataTable uses the default Output Statistics. By default , the Output Statistics provides only paging information and information about the number of rows selected. Because row selection is not turned on, the example in Figure 5 displays only paging information.
Figure 5. Example of the dataTable running with Output Statistics
Before continuing with the new features of the Output Statistics component, add the ability to select columns to the dataTable.
- Go back to the Design view for your JSP.
- Click your dataTable to select it.
- Select the Rational Application Developer Properties view to see the dataTable's properties.
- Click the Row actions tab, which is a sub-tab of the hx:dataTableEx tab.
- Select Add selection column to the table.
- Run on the server again.
When you run the JSP page on the server now, the default Output Stastics component displays both selection and paging information, as Figure 6 shows.
Figure 6. dataTable running on server with Output Statistics and a Selection column
The behavior of earlier versions of the Output Statistics component is still the default. Paging information displays by default, and selection information displays if there is a Selection column. Table 2 shows the new attributes of the Output Statistics feature.
Table 2. New attributes of the <hx:outputStatistics> tag
| Attribute Name | Values | Description |
|---|---|---|
| paging | true/false | If set to true and paging is enabled in the dataTable, then Page 1 of 4 will appear, for example. Paging information appears by default, so you must reset it to false to prevent it from appearing. |
| selected | true/false | If set to true, the number of selected rows appears in the status bar. For example: Selected: 3. True is the defult setting, so you must change it to false to prevent it from appearing. |
| total | true/false | If set to true, the total number of rows in the dataTable appears in the status bar. For example. Total: 20. It does not appear by default. |
| shown | true/false | If set to true, the number of rows visible will be shown in the status bar. For example: It does not appear by default.. It does not appear by default. |
| filtered | true/false | If you set up dataTable filtering, you can use this attribute to display the number of filtered rows. If you set the filter to equal 4, for example, then Filtered: 4 appears in the status bar. Note: You can also use this attribute to set the number of filtered rows in the component if you are using the statusText attribute. |
| statusText | true/false | If you set statusText, then 4 of the other 5 hx:outputStatistics are ignored: paging, selected, shown, and total. This attribute should be set to any string that you want displayed in the status bar. The values of {0}, {1}, {2}, {3}, {4} and {5} represent this information, respectively: paging info 1, paging info 2, selected rows, total rows, rows shown, and filtered rows. |
You can set these new attributes manually in the JSP source view.
- Go back to the Design view for your JSP.
- Select Output Statistics.
- Choose the Properties view in Rational Application Developer to see the Output Statistics properties.
- On the hx:outputStatistics tab in the Properties view, there are four check boxes and two text fields to check to turn on these new features.
- Check Show paging information to turn on paging information.
- Check Show number of selected rows to turn on selection information.
- Check Show total number of rows to turn on total information.
- Check Show number of visible rows to turn on row information.
Note: You can also set these options by entering your preferences in the tooling provided with Rational Application Developer V7.0:
- Go to the Source view for your JSP.
- Locate the <hx:pagerDeluxe> tag.
- Add an attribute of
paging="true"to the tag. - Add an attribute of
selected="true"to the tag. - Add an attribute of
shown="true"to the tag. - Add an attribute of
total="true"to the tag.
The Output Statistics tag added to the dataTable's footer will now look like what you see in Listing 4.
Listing 4. The Output Statistics tag added to the dataTable footer
<hx:outputStatistics id="statistics1" styleClass="outputStatistics" paging="true" selection="true" total="true" shown="true"> </hx:outputStatistics> |
Figure 7 shows the dataTable that results when you run your dataTable on the server. Four pieces of information are provided in statistical output in the status bar.
Figure 7. Output Statistics with new attributes
Add the Filtered attribute option of Output Statistics
- Go back to the Design view for your JSP.
- Select Output Statistics.
- Choose the Properties view in Rational Application Developer to see the Output Statistics properties.
- On the hx:outputStatistics tab in the Properties view, there is a text field called Number of filtered rows. You can either enter a number here to have a fixed number of filtered rows or bind it to a method to get the current number of filtered rows. You must write this code to return the filtered rows in the pagecode file for the JSP.
- In the src folder for your project, open file1.java in the pagecode package.
- Add the method shown in Listing 5 to the page code, as an example:
Listing 5. Source code to return the number of filtered rows
protected String filteredRows = "10";
public String getFilteredRows() {
return filteredRows;
}
public void setFilteredRows(String filteredRows) {
this.filteredRows = filteredRows;
}
|
- Go back to the Properties view in Rational Application Developer to see the Output Statistics properties.
- Click the button beside the text field called Number of filtered rows.
- The filteredRows variable that you just created will be listed in the window that displays.
- Select that variable, and then click OK.
- Run the JSP page on the server again, as previously described, the dataTable now displays the filtered rows (see Figure 8). It will display 10, because that is that value that you set for this example, but you can write code to change this value in the pagecode file.
Figure 8. Output Statistics with filtered rows
Add the statusText attribute of Output Statistics
The new statusText should be set to a string. For example: "We are on page {0} of {1}. There are {2} rows selected from a total of {3} rows. We can see {4} rows, and there are {5} filtered rows:
- {0} and {1} will be replaced by paging information, visible page, and total number of pages.
- {2} will be replaced by selected row information.
- {3} will be replaced by total row information.
- {4} will be replaced by number of rows shown
- {5} will be replaced by the number of filtered rows (only if the user has set the filtered attribute of this tag equal to the number of filtered rows, as descirbed in the previous section).
If statusText is not set, the other five new attributes are used instead. If none of these are set, then only paging and selection information will appear in the status bar, because the rest of the attributes are set to false by default. See Listing 6 for an example of the code to set the statusText attribute and Figure 9 for an example of the display.
Listing 6. Example of the code to set the statusText attribute:
<hx:outputStatistics id="statistics1"
styleClass="outputStatistics" filtered="#{pc_File1.filteredRows}"
statusText="We are on page {0} of {1}, there are {2} rows selected
from a total of {3} rows. We can see {4} rows and there are {5}
filtered rows."></hx:outputStatistics>
|
Figure 9. Output Statistics with statusText attribute
Scrolling is a new feature for the dataTable introduced in Rational Application Developer V7.0.
Add scrolling to the <h:dataTable> or <hx:dataTableEx> tags
dataTables imported in Rational Application Developer 7.0 from previous releases will have the <h:dataTable> tag; whereas, dataTables created using Rational Application Developer 7.0 will use the <hx:dataTableEx> tag. The area that scrolls is the area of the dataTable between the column headers and column footers. Scrolling can work in conjunction with paging, and each page of the dataTable will be scrollable. To specify the height of the scrollable area in Rational Application Developer 7.0 and add the new Scrolling feature, follow these steps:
- Go back to the Design view for your JSP.
- Select the dataTable.
- Switch to the Source view tab for your JSP.
- The page should now show the code for the h:dataTable tag, because you selected it before switching to the source view.
- Add
%lt;f:attribute name="scrollSize" value="20px"> tagas a child tag just below the opening h:dataTable tag. - You can set the value to anything with a unit of height in any of the CSS units of measurement: em, ex, px, pt, pc, cm, in, mm, or %.
The added to the <h:dataTable> will look like Listing 7.
Listing 7. Code added to the <h:dataTable>
<h:dataTable id="tableEx1" value="#{pc_File1.mybean.stocks}"
var="varstocks" styleClass="dataTableEx" headerClass="headerClass"
footerClass="footerClass" rowClasses="rowClass1, rowClass2"
columnClasses="columnClass1" border="0" cellpadding="2"
cellspacing="0">
<f:attribute name="scrollSize" value="20px"/>
|
Add Scrolling to the <hx:dataTableEx> tag
- Choose the Properties view in Rational Application Developer to see the dataTable's properties.
- Click the Display options tab, which is a sub-tab of the hx:dataTableEx tab.
- In the Height of scrollable area text field, enter a
numerical valueand choose a unit of height from the context menu. This unit of height can be any of the CSS units of measurement: em, ex, px, pt, pc, cm, in, mm, or %. - The value you choose must be smaller than the height of the dataTable, or Scrolling will not function.
The code added to the <hx:dataTableEx> tag will look like Listing 8.
Listing 8. Code added to the <hx:dataTableEx> tag
<hx:dataTableEx id="tableEx1" value="#{pc_File1.mybean.stocks}"
var="varstocks" styleClass="dataTableEx" headerClass="headerClass"
footerClass="footerClass" rowClasses="rowClass1, rowClass2"
columnClasses="columnClass1" border="0" cellpadding="2"
cellspacing="0" scrollSize="20px"%gt;
|
When you run the JSP page on the server, as described previously, the dataTable will now scroll, as Figure 10 shows.
Figure 10. dataTable running on the server with the Scrolling feature
| Description | Name | Size | Download method |
|---|---|---|---|
| Sample JavaBeans for this article | sampleModel1.zip | 5KB | HTTP |
Information about download methods
Learn
- Visit the
Rational Application Developer product page to expand your product skills.
- Stay current with developerWorks technical events and Webcasts.
- Check out this JavaServer Faces tutorial from Sun Microsystems.
Get products and technologies
- Download a free trial version of Rational Application Developer V7.0
- Build your next development project with IBM trial software, available for download directly from developerWorks.
Discuss
- Rational Software Architect, Data Architect, Software Modeler, Application Developer and Web Developer forum: Ask questions about Rational Application Developer.
- WebSphere JavaServer Faces (JSF) forum: Ask questions about WebSphere JavaServer Faces (JSF).
Susan Gallagher works at the IBM Dublin Software Lab. She graduated with a BSc. in Computer Applications from Dublin City University in 2002, and is a Sun-Certified Java Developer. She is currently a member of the JWL (JSF Widget Library) team, developing the DataGrid component. You can reach Susan at susan.gallagher@ie.ibm.com




