J2EE中使用Display標記庫來展示表格
來源:互聯網
上載者:User
j2ee 用網頁展示表格時,如果行數太多,有時候需要把它們分成很多頁.而且各行之間使用不同的背景色來方便使用者閱讀.或者可能還需要排序。雖然實現上面的功能都不難,但是如果使用Display標記庫將能夠大大簡化開發.它模仿google,baidu頁面的風格,把許多行的表格分成各個頁面,並提供了常用的功能。
資料模型是很簡單的美國總統JavaBean.它有3個簡單的String屬性。
Java代碼如下:
PagedData.java
import java.util.ArrayList;
import java.util.List;
public class PagedData {
private List list;
public PagedData( ) {
list = new ArrayList( );
list.add( new President( "Garfield", "James", "1881") );
list.add( new President( "Arthur", "Chester", "1881-85") );
list.add( new President( "Cleveland", "Grover", "1885-89") );
list.add( new President( "Harrison", "Benjamin", "1889-93") );
list.add( new President( "Cleveland", "Grover", "1893-97") );
list.add( new President( "McKinley", "William", "1897-1901") );
list.add( new President( "Roosevelt", "Theodore", "1901-09") );
list.add( new President( "Taft", "William H.", "1909-13") );
list.add( new President( "Wilson", "Woodrow", "1913-21") );
list.add( new President( "Jackson", "Andrew", "1829-37") );
list.add( new President( "Harding", "Warren", "1921-23") );
list.add( new President( "Coolidge", "Calvin", "1923-29") );
list.add( new President( "Hoover", "Herbert", "1929-33") );
list.add( new President( "Roosevelt", "Franklin D.", "1933-45") );
list.add( new President( "Truman", "Harry", "1945-53") );
list.add( new President( "Eisenhower", "Dwight", "1953-61") );
list.add( new President( "Kennedy", "John F.", "1961-63") );
list.add( new President( "Johnson", "Lyndon", "1963-69") );
list.add( new President( "Nixon", "Richard", "1969-74") );
list.add( new President( "Ford", "Gerald", "1974-77") );
list.add( new President( "Carter", "Jimmy", "1977-81") );
list.add( new President( "Reagan", "Ronald", "1981-89") );
list.add( new President( "Bush", "George H.W.", "1989-93") );
list.add( new President( "Clinton", "William J.", "1993-2001") );
list.add( new President( "Bush", "George W.", "2001-present") );
list.add( new President( "Washington", "George", "1789-97") );
list.add( new President( "Adams", "John", "1797-1801") );
list.add( new President( "Jefferson", "Thomas", "1801-09") );
list.add( new President( "Madison", "James", "1809-17") );
list.add( new President( "Monroe", "James", "1817-25") );
list.add( new President( "Jackson", "Andrew", "1829-37") );
list.add( new President( "Van Buren", "Martin", "1837-41") );
list.add( new President( "Harrison", "William Henry", "1841") );
list.add( new President( "Tyler", "John", "1841-45") );
list.add( new President( "Polk", "James", "1845-49") );
list.add( new President( "Taylor", "Zachary", "1849-50") );
list.add( new President( "Fillmore", "Millard", "1850-53") );
list.add( new President( "Pierce", "Franklin", "1853-57") );
list.add( new President( "Buchanan", "James", "1857") );
list.add( new President( "Lincoln", "Abraham", "1861-65") );
list.add( new President( "Johnson", "Andrew", "1865-69") );
list.add( new President( "Grant", "Ulysses S.", "1869-77") );
list.add( new President( "Hayes", "Rutherford B.", "1877-81") );
}
public List getData( ) {
return list;
}
}
President.java
public class President {
public President(String lname, String fname, String term) {
lastName = lname;
firstName = fname;
this.term = term;
}
public String getFirstName( ) {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName( ) {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getTerm( ) {
return term;
}
public void setTerm(String term) {
this.term = term;
}
private String lastName;
private String firstName;
private String term;
}
下面的Jsp頁面是展示表格的,也體現了Display庫最常見的用法:
index.jsp
<%@ 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="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>
在瀏覽器裡開啟頁面:
看見了吧,效果確實不錯:)
要使用display標記庫,需要在這裡下載:
http://displaytag.sourceforge.net
把display.jar檔案放到WEB-INF/lib中.
注意:
這裡用到了EL,所以 jstl.jar和standard.jar這兩個庫需要在lib中.
Display.jar依賴2.0或以上的Jakarta Commons Lang庫,commons-lang-2.0.jar和Jakarta Commons Collections庫,commons-collections.jar.
它們分別在:
http://jakarta.apache.org/commons和http://jakarta.apache.org/commons/collections/
下載,然後把對應的jar檔案copy到WEB-INF/lib中.
簡單介紹用法,其實也不用我多說,看看jsp檔案也就基本懂了.
<display:table id="pres" name="${pagedData.data}"
sort="list" pagesize="10" defaultsort="3">
id是以後用到時的變數.name是要展現的集合資料.list表示整個list被排序.pagesize表示每頁所要展示的數.defaultsort表示最開始是按第幾列排序的,注意這裡是以1開始計數的.
<display:caption>United States Presidents</display:caption>
isplay:caption標記中間的字串是用來放到表格上面的標題.
<display:column property="firstName" title="First Name"
sortable="true"/>
display:column標記指定了每列的屬性.
還要更多的使用方法,見Display標記庫的Doc文檔.