Adding a Null Entry to a Data-Bound Drop Down List in JSP

Written By Duncan Mills, Oracle Corporation
May 2004

Introduction

Oracle JDeveloper 10g, using the Oracle Application Development Framework (ADF) provides the JSP page developer with convenient way of creating drop down lists which are populated from a record collection of some sort (for instance a Business Components View Object) and which set the value for a bound field. An Example of this might be to replace a department number field on an input form with a drop down list naming the department.

There is an extensive sample showing you how to create these bound list items available on OTN:

DataBound Lists using ADF

There is also a HowTo on the same subject

Creating a Databound Drop Down List in Oracle JDeveloper 10g

This tip shows you how you can extend the technique of creating bound list by adding an entry to represent a null value, which may be required if the bound column is allowed to be null by the database schema and you need to provide users with a way of selecting that as a value in the drop down.

Adding a Null Item

There are two approaches you could take to add a null value:

  1. Create the View Object in your model layer which selects the required lookup rows from the lookup table and then UNIONs this list of rows with a select from DUAL to generate an extra blank row. This approach is reasonable, but it means that the view object has to be specially constructed and uses expert mode SQL.
  2. A simpler approach is to add an extra tag into the <html:select> statement in the page, and that is what is discussed here.

When you data bind a drop down list onto a page you will see the following code generated for the list tag:

       <html:select property="DepartmentId">
                              
<html:optionsCollection label="prompt" value="index" property="DepartmentId.displayData"/>
</html:select>

We can add a null value to this very simply (although the null value can only be defined to appear at the start or end of the list). To do this you just have to add an extra <html:option> tag within the <html:select> tag, which has the magic value of -1. The ADF model will interpret this as meaning set the bound column to null.

So here is the drop down list with a null value added as the first value in the list.

      <html:select property="DepartmentId">
                              
<html:option value="-1">(No Department Assigned)</html:option>
<html:optionsCollection label="prompt" value="index" property="DepartmentId.displayData"/>
</html:select>

The extra null entry will appear at the top of the list with the label (No Department Assigned)

You can extend this same techniques to radio groups which also use the list binding.

drmills v1.0 07/May/2004

false ,,,,,,,,,,,,,,,