用网页展示表格时,如果行数太多,有时候需要把它们分成很多页.而且各行之间使用不同的背景色来方便用户阅读.或者可能还需要排序。虽然实现上面的功能都不难,但是如果使用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文档.
闽公网安备 35060202000074号