`
追梦java
  • 浏览: 37237 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类
最新评论

J2EE中使用Display标记库来展示表格

 
阅读更多

用网页展示表格时,如果行数太多,有时候需要把它们分成很多页.而且各行之间使用不同的背景色来方便用户阅读.或者可能还需要排序。虽然实现上面的功能都不难,但是如果使用Display标记库将能够大大简化开发.它模仿google,baidu页面的风格,把许多行的表格分成各个页面,并提供了常用的功能。

  数据模型是很简单的美国总统JavaBean.它有3个简单的String属性。

  Java代码如下:

PagedData.java

importjava.util.ArrayList;
importjava.util.List;

publicclassPagedData{

 privateListlist;

 publicPagedData(){
  list=newArrayList();
  list.add(newPresident("Garfield","James","1881"));
  list.add(newPresident("Arthur","Chester","1881-85"));
  list.add(newPresident("Cleveland","Grover","1885-89"));
  list.add(newPresident("Harrison","Benjamin","1889-93"));
  list.add(newPresident("Cleveland","Grover","1893-97"));
  list.add(newPresident("McKinley","William","1897-1901"));
  list.add(newPresident("Roosevelt","Theodore","1901-09"));
  list.add(newPresident("Taft","WilliamH.","1909-13"));
  list.add(newPresident("Wilson","Woodrow","1913-21"));
  list.add(newPresident("Jackson","Andrew","1829-37"));
  list.add(newPresident("Harding","Warren","1921-23"));
  list.add(newPresident("Coolidge","Calvin","1923-29"));
  list.add(newPresident("Hoover","Herbert","1929-33"));
  list.add(newPresident("Roosevelt","FranklinD.","1933-45"));
  list.add(newPresident("Truman","Harry","1945-53"));
  list.add(newPresident("Eisenhower","Dwight","1953-61"));
  list.add(newPresident("Kennedy","JohnF.","1961-63"));
  list.add(newPresident("Johnson","Lyndon","1963-69"));
  list.add(newPresident("Nixon","Richard","1969-74"));
  list.add(newPresident("Ford","Gerald","1974-77"));
  list.add(newPresident("Carter","Jimmy","1977-81"));
  list.add(newPresident("Reagan","Ronald","1981-89"));
  list.add(newPresident("Bush","GeorgeH.W.","1989-93"));
  list.add(newPresident("Clinton","WilliamJ.","1993-2001"));
  list.add(newPresident("Bush","GeorgeW.","2001-present"));
  list.add(newPresident("Washington","George","1789-97"));
  list.add(newPresident("Adams","John","1797-1801"));
  list.add(newPresident("Jefferson","Thomas","1801-09"));
  list.add(newPresident("Madison","James","1809-17"));
  list.add(newPresident("Monroe","James","1817-25"));
  list.add(newPresident("Jackson","Andrew","1829-37"));
  list.add(newPresident("VanBuren","Martin","1837-41"));
  list.add(newPresident("Harrison","WilliamHenry","1841"));
  list.add(newPresident("Tyler","John","1841-45"));
  list.add(newPresident("Polk","James","1845-49"));
  list.add(newPresident("Taylor","Zachary","1849-50"));
  list.add(newPresident("Fillmore","Millard","1850-53"));
  list.add(newPresident("Pierce","Franklin","1853-57"));
  list.add(newPresident("Buchanan","James","1857"));
  list.add(newPresident("Lincoln","Abraham","1861-65"));
list.add(newPresident("Johnson","Andrew","1865-69"));
  list.add(newPresident("Grant","UlyssesS.","1869-77"));
  list.add(newPresident("Hayes","RutherfordB.","1877-81"));
 }
 publicListgetData(){
  returnlist;
 }
}

President.java

publicclassPresident{
 publicPresident(Stringlname,Stringfname,Stringterm){
  lastName=lname;
  firstName=fname;
  this.term=term;
 }

 publicStringgetFirstName(){
  returnfirstName;
 }
 publicvoidsetFirstName(StringfirstName){
  this.firstName=firstName;
 }
 publicStringgetLastName(){
  returnlastName;
 }
 publicvoidsetLastName(StringlastName){
  this.lastName=lastName;
 }
 publicStringgetTerm(){
  returnterm;
 }
 publicvoidsetTerm(Stringterm){
  this.term=term;
 }

 privateStringlastName;
 privateStringfirstName;
 privateStringterm;
}
  下面的JSP页面是展示表格的,也体现了Display库最常见的用法:

index.jsp

<%@pagecontentType="text/html;charset=UTF-8"language="java"%>
<%@tagliburi="http://displaytag.sf.net/el"prefix="display"%>
<html>
<head>
<title>StrutsCookbook-Chapter4:DisplayTagExample</title>
<style>
.even{background-color:orange;}
.odd{background-color:yellow;}
</style>
</head>
<body>
<h2>DisplayTagExamples</h2>
<jsp:useBeanid="pagedData"class="PagedData"/>
<display:tableid="pres"name="${pagedData.data}"
sort="list"pagesize="10"defaultsort="3">
<display:caption>UnitedStatesPresidents</display:caption>
<display:setPropertyname="basic.show.header"value="true"/>
<display:columnproperty="firstName"title="FirstName"
sortable="true"/>
<display:columnproperty="lastName"title="LastName"
sortable="true"/>
<display:columnproperty="term"title="TermofOffice"
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或以上的JakartaCommonsLang库,commons-lang-2.0.jar和JakartaCommonsCollections库,commons-collections.jar.

  它们分别在:

http://jakarta.apache.org/commons和http://jakarta.apache.org/commons/collections/

  下载,然后把对应的jar文件copy到WEB-INF/lib中.

简单介绍用法,其实也不用我多说,看看jsp文件也就基本懂了.

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

id是以后用到时的变量.name是要展现的集合数据.list表示整个list被排序.pagesize表示每页所要展示的数.defaultsort表示最开始是按第几列排序的,注意这里是以1开始计数的.

<display:caption>UnitedStatesPresidents</display:caption>

<display:caption标记中间的字符串是用来放到表格上面的标题.

<display:columnproperty="firstName"title="FirstName"
sortable="true"/>
display:column标记指定了每列的属性.

还要更多的使用方法,见Display标记库的Doc文档.




分享到:
评论

相关推荐

    J2EE指南(J2EE中文教材)

    J2EE指南对于广大的Java程序员来说是一份不可或缺的资料了。这篇导论对于初次碰到J2EE的程序员来说有着同样的作用。它与Java指南一样都是一例子为中心。 这篇指南是为爱好开发和部署J2EE应用程序的程序员准备的。它...

    j2ee中使用线程的小DEMO

    最近做了个线程,主要是在j2ee的运用的 个人觉得比较基础,仅供大家参考

    j2ee中文帮助文档

    j2ee中文帮助文档j2ee中文帮助文档j2ee中文帮助文档j2ee中文帮助文档j2ee中文帮助文档j2ee中文帮助文档j2ee中文帮助文档j2ee中文帮助文档j2ee中文帮助文档j2ee中文帮助文档

    J2EE 指南 J2EE中文教材

    J2EE 指南对于广大的Java 程序员来说是一份不可或缺的资料了。这篇导论对于初次碰到J2EE 的程序员来说有着同样的作用。它与Java 指南一样都是一例子为中心。

    j2ee6 中文规范

    j2ee6中文规范j2ee6中文规范j2ee6中文规范j2ee6中文规范

    j2ee的题库与练习

    j2ee的题库与练习

    J2EE开发使用手册(PDF)

    J2EE开发使用手册(PDF)

    j2ee Filter使用原理

    j2ee Filter使用原理本代码里面首先是介绍了Filter实现的原理,然后模拟的了j2ee项目中Filter的使用原理,然后由此又延伸了Struts2的Interceptor实现原理,并且给予了详细的注释。

    j2ee.jar架包

    j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar架包j2ee.jar...

    J2EE题库(选择填空)

    有关J2EE的一些选择填空题!ZZZZZZZZZZZZZZZZZZZZZZZZ

    J2EE开发使用手册

    J2EE开发使用手册:J2EE为开发企业级的多层应用程序和复杂的企业系统定义了一套API。针对各种各样的企业需求,本书试图通过J2EE解决所有的企业问题。全书共分六大部分30章,分别从企业面临的问题、企业数据的表示、...

    J2EE架构标签库

    J2EE架构_标签库封装 应用于移动、电信、广电BOSS系统架构源码

    j2ee j2ee j2ee j2ee j2ee j2ee

    j2ee j2ee j2ee j2ee j2ee j2ee j2ee j2ee j2ee

    J2EE 5 中文API

    J2EE 5 中文API

    J2EE开发使用手册-4

    本资料共分六大部分30章,分别从企业面临的问题、企业数据的表示、企业通信、常见企业服务、企业Web支持和企业应用程序支持等几个方面阐述企业问题的J2EE解决方案。还分别介绍了分布式应用程序开发的一些基本概念,...

    J2EE开发使用手册-3

    本资料共分六大部分30章,分别从企业面临的问题、企业数据的表示、企业通信、常见企业服务、企业Web支持和企业应用程序支持等几个方面阐述企业问题的J2EE解决方案。还分别介绍了分布式应用程序开发的一些基本概念,...

    j2ee 笔记 j2ee 笔记 j2ee 笔记

    j2ee 笔记 j2ee 笔记 j2ee 笔记 j2ee 笔记

    J2EE中文问题终极解决之道

    J2EE中文乱码问题终极解决之道 如果看了这个文件之后你还解决不了乱码问题 那就不是我的问题了

    J2EE课件J2EE课件J2EE课件J2EE课件

    J2EE课件 J2EE课件 J2EE课件 J2EE课件 J2EE课件 J2EE课件 J2EE课件 J2EE课件 J2EE课件 J2EE课件 J2EE课件 J2EE课件 J2EE课件 J2EE课件 J2EE课件

Global site tag (gtag.js) - Google Analytics