Database Reference
In-Depth Information
<select
name=
"color"
>
<c:forEach
items=
"${rs.rows}"
var=
"row"
>
<option
value=
"<c:out value="
${row.color}"
/>
">
<c:out
value=
"${row.color}"
/></option>
</c:forEach>
</select>
The pop-up menu can be changed easily to a scrolling list; add a
size
attribute to the
opening
<select>
tag. For example, to make three colors visible at a time, generate the
list like this:
<select
name=
"color"
size=
"3"
>
<c:forEach
items=
"${rs.rows}"
var=
"row"
>
<option
value=
"<c:out value="
${row.color}"
/>
">
<c:out
value=
"${row.color}"
/></option>
</c:forEach>
</select>
Generating a list element for the set of states is similar, except that the labels are not the
same as the values. To make the labels more meaningful to customers, display the full
state names. But the value returned when the form is submitted should be an abbrevi‐
ation because that is what gets stored in the
cow_order
table. To produce a list that way,
select both the abbreviations and the full names and insert them into the proper parts
of each list item. For example, to create a pop-up menu, do this:
<sql:query
dataSource=
"${conn}"
var=
"rs"
>
SELECT abbrev, name FROM states ORDER BY name
</sql:query>
<select
name=
"state"
>
<c:forEach
items=
"${rs.rows}"
var=
"row"
>
<option
value=
"<c:out value="
${row.abbrev}"
/>
">
<c:out
value=
"${row.name}"
/></option>
</c:forEach>
</select>
The preceding JSP examples use an approach that prints each list item individually. List
element generation in CGI.pm-based Perl scripts proceeds on a different basis: extract
the information from the database first, and then pass it all to a function that returns a
string representing the form element. The functions that generate single-pick elements
are
radio_group()
,
popup_menu()
, and
scrolling_list()
. These have several argu‐
ments in common:
name
The list element name.
values
The values for the items in the list. This should be a reference to an array.