Data sources
Expressions
Up until this point, we have simply had a List object available to us in the request scope that has driven the display of the tables shown. We have been setting up that bean with the following scriptlet, but presumably you would be doing something similar in your Action class rather then raw on this jsp page.
<% request.setAttribute( "test", new TestList( 10 ) ); %>
This table is called with the following attributes:
<display:table name="test">
You can also acquire a handle to the list you want to display by specifying not only a bean name, but also a bean property (a getter method), and the table tag will call that property to fetch the list to display.
In the non-EL version you can define the “name” attribute using a really similar sintax, just without the $:
You can define the scope of the bean adding one of the following suffix:
- pageScope
- requestScope (default)
- sessionScope
- applicationScope
You can also access javabean style properties, mapped properties or indexed properties in the bean, also nested. The syntax for accessing a javabean property is .property . You can read a mapped property specifying it between () and an indexed property using [].
So the following:
sessionScope.list.value.attribute(name).item[1]
is equivalent to:
session.getAttribute("list").getValue().getAttribute("name").getItem(1)
Supported data
The table tag actually supports the following kind of objects:
- a Collection
- an Enumeration
- a Map (values are displayed in row)
- a Dictionary (values are displayed in row)
- an array
- an Iterator
- any Object with an iterator() method
- … any other object will be displayed on a single row
From a db?
Displaytag will never support retrieving data from a db directly. Displaytag is here to help you in displaying data, not to retrieve them.
Anyway, there are a couple of easy methods to get records from a db and display them using displaytag:
1) Using jstl:
just use the sql:query tag and pass the result to the table tag in this way
<sql:query var="results">
select * from table
</sql:query>
<display:table name="${results.rows}" />
2) Using dynabeans
<%
Connection con = ...; // just open a connection
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * from table");
RowSetDynaClass resultSet = new RowSetDynaClass(rs, false);
stmt.close();
con.close();
request.setAttribute("results", resultSet);
%>
<display:table name="requestScope.results.rows" />