Custom JSP taglib. Implements data driven templates you may use for pages with some regular structure (e.g. tables, catalogues, reports etc.). DataList tag accepts as parameters some datasource (e.g. array, iterator, enumeration, ResultSet) and a set of templates (for header, footer, items and separators). Developers with background in MS technologies may find that this taglib is an analogue of DataList web control in .NET framework.
For example:
and you will get the following output:
In this example itemTemplate describes one pattern for data. CURRENT_VALUE is a page scope variable (so, it is case sensitive) you may use as a reference for current value (type is java.lang.String).
You may describe a separate template for each other row:
and output is:
element:apple
----
orange
----
element:pear
So this tag outputs itemTemplate (or alternatingItemTemplate) for each element in datasource. Tag outputs separatorTemplate between "rows".
You may add paging. Tag DataList accepts also parameters from and to. From describes an index (or row in case of ResultSet) for first element. Default value is 1 (from the first position or first row). To describes an index for last element (row). Note: indexes always start from 1 (not from 0).
As a source you may use arrays, enumerations, iterators and ResultSets.
You may use this tag together with DB tags
also. For example:
Two parameters repeatColumns and repeatLayout describe how to output data. RepeatColumns describes how many items will be describes in one "line" (between separators). Default value is 1. Default value for RepeatLayout if flow. Items will be outputted "as is". Another possible value is table. In this case each "line" of items will be surrounded by <tr> and </tr> tags and each item will be surrounded by <td> and </td> tags.
Parameter type describes type for elements in your datasource. In itemTemplate and
alternatingItemTemplate you may use predefined variables CURRENT_VALUE and CURRENT_OBJECT.
Type for CURRENT_OBJECT is exactly what you set in parameter type. Type for CURRENT_VALUE
is java.lang.String. For example, suppose you have a vector of beans. You may use some like this:
Let us see more examples (integration with the database):
and you will get some like this:
1 Java 2 C++ 3 Basic 4 Prolog
You may integrate DataList with DB tags. In this case parameter source is some string describes query result and parameter type must be com.cj.datalist.dbtag. Class com.cj.datalist.dbtag supports methods
getColumn(int columnNumber) returns data
getColumnName(int columnNumber) returns a name for column
getColumnType(int columnNumber) returns a type for column
getColumnCount() what's the number of columns in the ResultSet?
For example:
Tags are:
DataList
Body tag describes a data list. Body contains definitions for templates (see tags below). Parameters are:
1) source Describes data source. Could be an array, iterator, enumeration, ResultSet or string.
String describes ID for query results if you are using DB tags.
2) type Optional parameter. Describes type for elements in datasource. Default value is
java.lang.String. For ResultSet it must be java.sql.ResultSet, for DB tag ID it must be
com.cj.datalist.dbtag
3) repeatColumns Optional parameter. Describes how many items should
placed per each row. Default value is 1 (one item per row).
4) repeatLayout Optional parameter. Describes a layout. Possible values are
table or flow. Default value is flow. In case of table layout each row
will be wrapped to <tr> and </tr> tags and each item will be wrapped to <td> and </td> tags.
5) from Optional parameter. Describes an initial element (position) for loop through datasource
(row in ResultSet). Starts with 1. Default value is 1 (first element or first row).
6) to Optional parameter. Describes a last element (position) for loop through datasource
(row in ResultSet). By default tag scans all elements or rows (up to last position or row).
headerTemplate
Body tag describes a template for header. Parameters are: none
footerTemplate
Body tag describes a template for footer. Parameters are: none
separatorTemplate
Body tag describes a template for separator. Parameters are: none
itemTemplate
Body tag describes a template for items. Parameters are: none
alternatingItemTemplate
Body tag describes a template for items in other rows. Parameters are: none
for downloading:
Library: datalist.jar Description: taglib.tld
See also Coldtags suite - the largest collection of custom JSP tags.