`
zhou568xiao
  • 浏览: 96236 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

displaytag 分页的简单例子(附源代码)

阅读更多

一.display tag

    DisplayTag是一个非常好用的表格显示标签,适合MVC模式。可以对的Table进行分页、数据导出、分组、对列排序等.下面将用Struts2+display tag做个最简单的分页程序.

    1.首先要下它的jar包,将jar包放到WEB-INF的lib文件夹下它的核心jar包是jstl-1.2.jar,另外需要一些辅助jar包,这些辅助包都有不同的功能,具体的功能可以访问http://displaytag.sourceforge.net/10/dependencies.html ,根据需要下载不同的jar包。例子中用到jar包如下:

commons-logging-1.0.4.jar
displaytag-1.1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.jar
xwork-2.0.4.jar
commons-lang-2.3.jar
standard-1.1.2.jar
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
jstl-1.2.jar
itext-1.3.jar
commons-digester-1.7.jar

    2.然后在web.xml下添加一个filter
    <filter>
        <filter-name>exportFilter</filter-name>
        <filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class>
    </filter>

在jsp页面做一个引用:
<%@ taglib uri="http://displaytag.sf.net/el" prefix="display" %>

   3.在src下面com.hua.example先建一个bean

package com.hua.example;

public class StudentInfo {
 private String name;
 private String age;
 private String mark;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getAge() {
  return age;
 }
 public void setAge(String age) {
  this.age = age;
 }
 public String getMark() {
  return mark;
 }
 public void setMark(String mark) {
  this.mark = mark;
 }
 
}

建action

package com.hua.example;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class StudentAction extends ActionSupport{
 private List<StudentInfo>studentInfos;
 public List<StudentInfo> getStudentInfos() {
  return studentInfos;
 }

 public void setStudentInfos(List<StudentInfo> studentInfos) {
  this.studentInfos = studentInfos;
 }

 public String dispayAllStudents()
 {

//定义一个list存放要显示的记录
  studentInfos=new ArrayList<StudentInfo>();
  for(int i=0;i<50;i++)
  {
   StudentInfo studentInfo=new StudentInfo();
   studentInfo.setName("jian"+i);
   studentInfo.setAge("jian"+i);
   studentInfo.setMark("jian"+i);
   studentInfos.add(studentInfo);
  }  return SUCCESS;
 }
}

    4.配置struts.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd ">
<web-app>
 <display-name>Struts 2.0 Hello World</display-name>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>
 <!-- display tag 配置的过滤器 -->
 <filter>
  <filter-name>exportFilter</filter-name>
  <filter-class>
   org.displaytag.filter.ResponseOverrideFilter
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

web.xml

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "
http://java.sun.com/dtd/web-app_2_3.dtd ">
<web-app>
 <display-name>Struts 2.0 Hello World</display-name>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>
 <!-- display tag 配置的过滤器 -->
 <filter>
  <filter-name>exportFilter</filter-name>
  <filter-class>
   org.displaytag.filter.ResponseOverrideFilter
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

   5.相应的页面如下index.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="
http://displaytag.sf.net " prefix="display" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>Student Message</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 
  </head>
 
  <body>
     <a href="/displaytagexample/studentInfo.action">点击查看display tag分页的简单例子</a>
  </body>
</html>

studentinfo.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://displaytag.sf.net/el " prefix="display"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">
  <title>Student Message</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
 
 </head>

 <body>
  <!-- name 是action的list 如果你把他放在session中了就不用写requestURI 否则就一定要写。requestURI值action的路径 -->
  <display:table name="studentInfos" pagesize="10"
   requestURI="studentInfo.action">
   <display:column property="name" title="姓名" />
   <display:column property="age" title="年龄" />
   <display:column property="mark" title="分数" />
  </display:table>

 </body>
</html>
   运行后的效果如下:

<!---->50 items found, displaying 1 to 10. [First/Prev] 1 , 2 , 3 , 4 , 5 [Next /Last ]

姓名年龄分数
jian0 jian0 jian0
jian1 jian1 jian1
jian2 jian2 jian2
jian3 jian3 jian3
jian4 jian4 jian4
jian5 jian5 jian5
jian6 jian6 jian6
jian7 jian7 jian7
jian8 jian8 jian8
jian9 jian9 jian9

显示的格式自己可以定义CSS在studentinfo.jsp加上

<style type="text/css">
.table {
 border: 1px solid #74B3DC;
 color: #000;
 background: #fff;
 width: 99.7% !important;
 width: 99.5%;
}

.table td,.table th{
 border: 1px solid #e0e0e0;
 border-left: 0px;
 border-top: 0px;
 padding: 0.2em;
}
.table thead th{
 border: 1px solid #e0e0e0;
 border-left: 0px;
 border-top: 0px;
 text-align: left;
 font-size: 1em;
 font-weight: bold;
 background: #d7e9f5;
}
}
</style>

在<display:table name="studentInfos" pagesize="10"
   requestURI="studentInfo.action" class="table" >加上引用

最后显示的效果如下

<!---->

 

 下面是display tag 其他的一些用法和功能

http://hi.baidu.com/netnotes/blog/item/d6527d121bef1850f919b878.html

http://kewb.iteye.com/blog/128744

  • 描述: 加上css后显示的效果。
  • 大小: 40.9 KB
  • libs.rar (2.8 MB)
  • 描述: display tag 分页例子中用到的有关display tag lib的jar包,把这些jar包拷贝到WEB-INF/lib中例子就可以运行了。
  • 下载次数: 2546
12
4
分享到:
评论
5 楼 wkshippou 2008-08-19  
是啊,只查询显示当前页面的记录就可以了.
4 楼 wen870105 2008-08-07  
风之狐 2008-05-13
页是分了不错,可是每次都是取得全部数据,数据多了,服务器还不崩溃啦

改他源码,每次只查询当前数据就ok了
3 楼 foryou33 2008-08-03  
其实也不太难解决的,遇到记录上万的数据的时候,只查询显示当前页面的记录就可以了。
2 楼 zhou568xiao 2008-05-22  
这也是display tag美中不足的地方,数据量特别大的话是不适合用的。用的时候要根据自己的情况选择。
1 楼 风之狐 2008-05-13  
页是分了不错,可是每次都是取得全部数据,数据多了,服务器还不崩溃啦

相关推荐

Global site tag (gtag.js) - Google Analytics