Repeater taglib ver. 1.8

    Custom JSP tag Repeater displays data items in a repeating list. This tag is similar to the Repeater control in ASP.NET. Similar to DataList, the content and layout of list items in Repeater is defined using templates. At a minimum, you must define an itemTemplate; however, the following optional templates may be used to customize the appearance of the list:

Template Name Description
itemTemplate Defines the content and layout of items within the list.
alternatingItemTemplate If defined, the alternatingItemTemplate determines the content and layout of alternating items. If not defined, itemTemplate is used.
separatorTemplate If defined, the separatorTemplate is rendered between items (and alternating items). If not defined, a separator is not rendered.
headerTemplate If defined, the headerTemplate determines the content and layout of the list header. If not defined, header is not rendered.
footerTemplate If defined, the footerTemplate determines the content and layout of the list footer. If not defined, footer is not rendered.

Unlike DataList, Repeater has no built-in layout or styles. You must explicitly declare all HTML layout, formatting, and style tags within the templates of the control. For example, to create a list within an HTML table, you might declare the <table> tag in the headerTemplate, a table row (<tr> tags, <td> tags) in the itemTemplate, and the </table> tag in the footerTemplate. It is similar to DataList tag with the following values: repeatColumns="1" repeatLayout="flow".

For example:
 


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

<list:Repeater source="<%=s%>">
<list:itemTemplate><%=CURRENT_VALUE%></list:itemTemplate>
<list:separatorTemplate>,</list:separatorTemplate>
<list:headerTemplate><center><b>HEADER</b></center></list:headerTemplate>
<list:footerTemplate><center><b>FOOTER</b></center></list:footerTemplate>
</list:Repeater>
</html>

And you will get a commas separated list:

HEADER

apple,orange,pear
FOOTER

So this tag outputs itemTemplate (or alternatingItemTemplate) for each element in your datasource. Tag outputs separatorTemplate between elements.

You may add paging. Tag Repeater 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:Repeater source="<%=v%>">
  ...
</list:Repeater>

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:Repeater source="<%=v%>" type="com.acme.bean">
<list:itemTemplate> <%=CURRENT_OBJECT.getName() %> </list:itemTemplate>
...
</list:Repeater>

Let us see more examples (integration with databases):
 


<%@ 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:Repeater 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:Repeater>

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

and you will get some like this:

  1Java
  2C++
  3Basic
  4Prolog

You may integrate Repeater with DB tags. In this case parameter source is some string describes query result and parameter type must be com.cj.repeater.dbtag. Class com.cj.repeater.dbtag supports methods getColumn(int index_for_column), getColumnName(int index_for_column), getColumnType(int index_for_column), getColumnCount() that lets you read data. 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 Repeater -->
<list:Repeater source="<%=\"A\"%>" type="com.cj.repeater.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:Repeater>

Tags are:

Repeater

Tag displays data items in a repeating list. Parameters are:

1) source Describes a 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 a 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.repeater.dbtag
3) 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).
4) 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: repeater.jar    Description: taglib.tld

© Coldbeans      Comments?

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

Also in Coldtags: