Database Reference
In-Depth Information
List
values
=
new
ArrayList
();
// column must be an ENUM or SET
Pattern
pc
=
Pattern
.
compile
(
"(enum|set)\\((.*)\\)"
,
Pattern
.
CASE_INSENSITIVE
);
Matcher
m
=
pc
.
matcher
(
typeDef
);
// matches() fails unless it matches entire string
if
(
m
.
matches
())
{
// split value list on commas, trim quotes from end of each word
String
[]
v
=
m
.
group
(
2
).
split
(
","
);
for
(
int
i
=
0
;
i
<
v
.
length
;
i
++)
values
.
add
(
v
[
i
].
substring
(
1
,
v
[
i
].
length
()
-
1
));
}
ctx
.
setAttribute
(
valListAttr
,
values
);
}
%>
The function takes three arguments:
ctx
The page context object.
typeDefAttr
The name of the page attribute that contains the column definition. This is the
function “input.”
valListAttr
The name of the page attribute into which to store the resulting array of legal column
values. This is the function “output.”
To generate a list element from the
size
column, begin by fetching the column metadata.
Extract the column value list into a JSTL variable named
values
and the default value
into a variable named
default
as follows:
<sql:query
dataSource=
"${conn}"
var=
"rs"
>
SELECT COLUMN_TYPE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'cookbook' AND TABLE_NAME = 'cow_order'
AND COLUMN_NAME = 'size'
</sql:query>
<c:set
var=
"typeDef"
scope=
"page"
value=
"${rs.rowsByIndex[0][0]}"
/>
<%
getEnumOrSetValues
(
pageContext
,
"typeDef"
,
"values"
);
%>
<c:set
var=
"defaultVal"
scope=
"page"
value=
"${rs.rowsByIndex[0][1]}"
/>
Then use the value list and default value to construct a form element. For example,
produce a set of radio buttons like this:
<c:forEach
items=
"${values}"
var=
"val"
>
<input
type=
"radio"
name=
"size"
value=
"<c:out value="
${val}"
/>
"