DataList taglib ver. 2.2

    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:
 


<%@ taglib uri="taglib.tld" prefix="list" %>
<html>
<%
   String s[]={"apple","orange","pear"};
%>

<list:DataList source="<%=s%>">
<list:itemTemplate>element:<%=CURRENT_VALUE%></list:itemTemplate>
<list:separatorTemplate><br>----<br></list:separatorTemplate>
<list:headerTemplate><center><b>HEADER</b></center></list:headerTemplate>
<list:footerTemplate><center><b>FOOTER</b></center></list:footerTemplate>
</list:DataList>

</html>

and you will get the following output:

HEADER

element:apple
----
element:orange
----
element:pear
FOOTER

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:
 


<list:DataList source="<%=s%>">
<list:itemTemplate>element:<%=CURRENT_VALUE%></list:itemTemplate>
<list:alternatingItemTemplate>
<font color="lightblue">element:<%=CURRENT_VALUE%></font>
</list:alternatingItemTemplate>
<list:separatorTemplate><br>----<br></list:separatorTemplate>
</list:DataList>

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:
 


<%
  java.util.Vector v=new java.util.Vector();
  v.addElement("first");
  v.addElement("second");
%>

<list:DataList source="<%=v%>">
  ...
</list:DataList>

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:
 


<%
  java.util.Vector=new java.util.Vector();
  v.addElement(new com.acme.bean());
  ...
%>
<list:DataList source="<%=v%>" type="com.acme.bean">
<list:itemTemplate> <%=CURRENT_OBJECT.getName() %> </list:itemTemplate>
...
</list:DataList>

Let us see more examples (integration with the database):
 


<%@ taglib uri="taglib.tld" prefix="list" %>
<%@ page import="java.sql.*" %>
<html>
<%
Object o=Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection conn=DriverManager.getConnection("jdbc:odbc:db1");
PreparedStatement st=conn.prepareStatement("select * from Test");
ResultSet rs=st.executeQuery();
%>

<!-- prints 4 records -->
<list:DataList source="<%=rs%>" type="java.sql.ResultSet" from="2" to="5">
<list:itemTemplate>
 <tr><td><%=CURRENT_OBJECT.getString(1)%></td><td><%=CURRENT_OBJECT.getString(2)%></td></tr>
</list:itemTemplate>
<list:alternatingItemTemplate>
 <tr bgcolor="lightgreen"><td><%=CURRENT_OBJECT.getString(1)%></td><td><%=CURRENT_OBJECT.getString(2)%></td></tr>
</list:alternatingItemTemplate>
<list:headerTemplate><table border="1"></list:headerTemplate>
<list:footerTemplate></table></list:footerTemplate>
</list:DataList>

<%
rs.close();
st.close();
conn.close();
%>
</html>

and you will get some like this:

  1Java
  2C++
  3Basic
  4Prolog

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:
 


<sql:openConnection driver="sun.jdbc.odbc.JdbcOdbcDriver" url="jdbc:odbc:db1" id="conn"/>
<sql:setQuery id="conn" res="A" query="select * from Test"/>

<!-- ID for query is A. We will pass this as a string expression to DataList -->
<list:DataList source="<%=\"A\"%>" type="com.cj.datalist.dbtag" from="2" to="5">
<list:itemTemplate><tr><td><%=CURRENT_OBJECT.getColumn(1)%></td><td><%=CURRENT_OBJECT.getColumn(2)%></td></tr></list:itemTemplate>
<list:headerTemplate><table border="1"></list:headerTemplate>
<list:footerTemplate></table></list:footerTemplate>
</list:DataList>

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

© Coldbeans      Comments?

See also Coldtags suite - the largest collection of custom JSP tags.

Also in Coldtags: