📄 tut_sources.apt
字号:
------------------------------
Data sources
------------------------------
Fabrizio Giustina
------------------------------
11-11-2005
------------------------------
Data sources
* Expressions
Up until this point, we have simply had a List object available to
us under the name "list" 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.
Actually there are two "flavors" of displaytag: an EL version, which
requires j2ee 1.3 and a jsp 1.1 (j2ee 1.2 compatible) version
In the EL version you can obviously use an EL expression, like
name="${pageScope.name.property}"
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}" />
(or
<display:table name="pageScope.results.rows" />
if not using the EL version)
+-------------------------------------------------------------------+
2) Using dynabeans
see {{{http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/RowSetDynaClass.html}}}
+-------------------------------------------------------------------+
<%
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" />
+-------------------------------------------------------------------+
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -