Exercise 1.6: Filtering a relational record list
Before you begin, complete Exercise 1.5: Creating an update page.
Now you have seen how to use a relational record list to display all the records in a table and how to apply a filter condition on a relational record to single out a specific record. Next, you will learn how to display only those records that match a specific criterion, allowing users to view the Classified ads by category.
As with the update page, you will filter on a parameter containing the desired category, passed to the page from a hyperlink. Since there may be multiple records that match the category, the filter will be applied to a relational record list, rather than a relational record.
Create a filtered relational record list
Creating a filtered relational record list includes the extra step of filtering the list so only certain records are shown. Similar to when you created the update_record.jsp page, you will specify a parameter in the all_records.jsp page and use a link to send that parameter to the filtered_records.jsp page.
- Open the filtered_records.jsp file by double-clicking it in the Project Explorer view.
- Remove the default text.
- Create a relational record list by dragging a Relational Record List from the Palette onto the page. The Add Relational Record List window opens.
- Name this Record List filtered_recordlist.
- Make sure Add Data Controls is selected and click Next.
- Click the W5SAMPLE.ADS table.
- Click Next.
- Under Tasks, click Filter results. The Filters window opens.
- To create the filter condition, click the Add
button on the Conditions tab. The Conditions window opens.
- Select the MAINCATEGORY column from the Column drop-down list.
- Type #{param.CAT} in the Value field. This code will be evaluated at runtime to be the value of the request parameter CAT.
The Conditions window should look like this:
- Click OK to apply the filter condition to your relational record list. This condition is listed on the Conditions tab.
- Click Close.
- Click Next.
- Again, you have the choice of different columns to display in the data table. Choose only these columns:
- TITLE
- DESCRIPTION
- MAINCATEGORY
- PRICE
- PHONE columns.
- Arrange the columns in the following order:
The Add Relational Record List window should look like this:
- TITLE
- DESCRIPTION
- MAINCATEGORY
- PRICE
- PHONE
- Click Finish. The relational record list is added to the page. It looks just like the record list on the all_records.jsp page, but because it is filtered by category, it will show only the records in one category at a time.
- Save the page.
The page should look like this:
Linking from all_records.jsp
As with the update page, now you need to create a hyperlink in the all_records.jsp file that will not only navigate to the filtered_records.jsp file but will also pass the CAT parameter to filter the relational record list.
- Open the all_records.jsp file.
- In the Palette view, expand the Faces Components drawer.
- Drag a Link component from the Palette directly onto the {MAINCATEGORY} output control on your data table. The Configure URL window opens.
- In the URL field, type filtered_records.jsp as the target page, and leave the Label field empty.
Dropping the link directly onto an output control and leaving the Label field empty causes the hyperlink to use the {MAINCATEGORY} text as the hyperlink label.
- Click OK.
- Click the link icon
and then click the Parameter tab in the Properties view.
- Click Add Parameter to create a new parameter.
- Type CAT in the Name field for the new parameter.
- To bind the CAT parameter to the MAINCATEGORY column in the input form, select the Value field and then click the Select Page Data Object
button in that field. The Select Page Data Object window opens.
- In the Data Objects field, click the MAINCATEGORY column from the all_recordlist(ADS) relational record list.
- Click OK.
- Save the page and test it. When you click a category name, you will be sent to the filtered_records.jsp page, which shows only the classified ads in that category.
Congratulations!
You have completed Module 1: Creating Web pages with data connections. You have learned how to:
- Display information from databases on Web pages
- Work with relational records, relational record lists, and data tables
- Display, edit, create, and delete database records from a Web page
- Link to a record or set of records using the hyperlink and combo box components
Adding advanced features
This module has introduced you to JavaServer Faces technology and tooling. There is much more you can do to make your site attractive, usable, and efficient. In the next module, Module 2: Adding advanced features, you will learn how to take this simple classified advertising site and turn it into a more visually pleasing and complex application. Continue to the next module if you want to learn to make your site look like this: