4.6. Using the Display Tag Library

Problem

You want an easier way of displaying tabular data that supports paging and sorting.

Solution

Use the Display tag JSP tag library.

Example 4-13 shows a JSP page that displays the list of U.S. presidents using the model data from Recipe 4.5. This JSP page displays a table rendered using the display tag library. The displayed page has alternating table row colors, allows for paging, and offers column sorting, all without requiring any custom Java coding.

Example 4-13. Display tag example

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://displaytag.sf.net/el" prefix="display" %>
<html>
<head>
  <title>Struts Cookbook - Chapter 4 : Display Tag Example</title>
  <style>
  .even {background-color:orange;}
  .odd {background-color:yellow;}
  </style>
</head>
<body>
  <h2>Display Tag Examples</h2>
  <jsp:useBean id="pagedData" class="com.oreilly.strutsckbk.ch04.
    PagedData"/>
  <display:table id="pres" name="${pagedData.data}"
                 sort="list" pagesize="10" defaultsort="3">
    <display:caption>United States Presidents</display:caption>
    <display:setProperty name="basic.show.header" value="true"/>
    <display:column property="firstName" title="First Name"
                    sortable="true"/>
    <display:column property="lastName" title="Last Name"
                    sortable="true"/>
    <display:column property="term" title="Term of Office"
                    sortable="true"/>
  </display:table>  
</body>
</html>

Discussion

The display tag library is an open source tag library that can be used to render highly functional displays of tabular data. The Solution shown above creates the page shown in Figure 4-8.

Table created using the display tags

Figure 4-8. Table created using the display tags

The web page shown in Figure 4-8 packs a lot of functionality for a table created using a handful of custom tags. If you compare this Solution to the previous recipes that were used to provide alternate row colors, paging, and sorting, you can see why this tag library has become quite popular.

Warning

This solution provides presentation-level paging. It doesn't restrict the amount of data initially received from the underlying persistent store.

To use the display tag library, you will need to download it from http://displaytag.sourceforge.net. Once downloaded, copy the displaytag.jar file into your web application's WEB-INF/lib folder. You will need to copy a tag library descriptor (.tld) file into this folder as well. The display tags provide a couple of choices here. The Solution uses the displaytags-el.tld file. These tags support JSTL expressions for attribute values.

The EL-version of the display tags requires that the jstl.jar and standard.jar JAR files be present in the WEB-INF/lib folder.

The display tag library depends on Version 2.0 or later of the Jakarta Commons Lang library, commons-lang-2.0.jar.

Tip

At the time of this writing, the Struts 1.1 distribution shipped with an earlier version of Commons Lang, and Struts 1.2 didn't include any version of Commons Lang. You can download the commons-lang-2.0.jar from http://jakarta.apache.org/commons. Replace commons-lang.jar with commons-lang-2.0.jar in your web application's WEB-INF/lib folder. From here on, you should not have any other incompatibility or dependency problems.

The display tags provide a lot of functionality and are easy to use. First, the display:table tag specifies information about the entire table:

<display:table id="pres" name="${pagedData.data}"
             sort="list" pagesize="10" defaultsort="3">

The id attribute creates a scoped variable that can be used to refer to the current row. The name attribute identifies the collection to be rendered. The sort attribute indicates how the data should be sorted: "list" indicates that the entire list is sorted, and "page" indicates that only the visible data on the current page is to be sorted. The value of the pagesize attribute is the number of rows to display per page.

Tip

Setting the pagesize attribute automatically enables paging.

The defaultsort attribute specifies the column (starting with 1) on which the data is initially sorted. In the Solution, this value is set to 3, which sorts the data by the "Term of Office" column.

The display:caption tag renders the table caption displayed above the column headers:

<display:caption>United States Presidents</display:caption>

The display:column tag specifies information about each column to be displayed:

<display:column property="firstName" title="First Name"
                sortable="true"/>

The property attribute specifies the JavaBean property that holds the data for the column. The title attribute specifies the text to display in the column header. The sortable attribute specifies if the data can be sorted by this column. If this value is set to true, then clicking the column header will sort the data by that column.

The display tag library provides functionality for exporting displayed tabular data to XML, Excel spreadsheets, and comma-separated value (.csv) files. This functionality can be enabled by registering some servlet filters provided with the library. Then you set the export attribute of the display:table tag to true. The documentation provided with display tags has all the details on setting up the export capability.

See Also

Complete details on the display tag library can be found at its web site: http://displaytag.sourceforge.net. Recipe 4.3, Recipe 4.4, and Recipe 4.5 show how to provide similar functionality. While these "roll your own" recipes may not be necessary if you're using the display tags, they will help you understand how the functionality is implemented.

Get Jakarta Struts Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.