欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

displaytag分页优化,处理大批量数据

程序员文章站 2022-06-15 15:00:31
...
Display Tag Lib是一个标签库,用来处理jsp网页上的Table,功能非常强,可以对的Table进行分页、数据导出、分组、对列排序等等。使用它依赖的包有:
commons-beanunits;
commons-lang
commons- logging
commons- collections
displaytag;
dislaytag分页原理:displaytag分页是将数据库中的所有数据先读入内存,然后在根据分页条件,将不需要显示的数据删除掉,这样我们就很容易想到:如果数据量非常大的时候,可能会引起系统内容崩溃;因上述原因本人参考网络,优化了一下displaytag的分页,希望对同行的朋友,有帮助。优化后的分页原理:只从数据库读取需要显示的条数(比如每页显示10条记录,则每次从数据库读取10条,不会出现内容崩溃的情况),具体炒作如下:
[size=large]第一步[size=large][/size][/size]
编写一个类,实现了displaytag的标签提供的接口 PaginatedList,该类的功能是提供displaytag依赖的分页条件,比如当前页,每页显示数目等。类的代码如下:
public class PaginatedListHelper implements PaginatedList {
@SuppressWarnings("unchecked")
private List list;
private int pageNumber = 1;
private int objectsPerPage = 10;
private int fullListSize = 0;
private String sortCriterion;
private SortOrderEnum sortDirection;
private String searchId;

@SuppressWarnings("unchecked")
public List getList() {
return list;
}

@SuppressWarnings("unchecked")
public void setList(List list) {
this.list = list;
}

public int getPageNumber() {
return pageNumber;
}

public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}

public int getObjectsPerPage() {
return objectsPerPage;
}

public void setObjectsPerPage(int objectsPerPage) {
this.objectsPerPage = objectsPerPage;
}

public int getFullListSize() {
return fullListSize;
}

public void setFullListSize(int fullListSize) {
this.fullListSize = fullListSize;
}

public String getSortCriterion() {
return sortCriterion;
}

public void setSortCriterion(String fieldN) {
this.sortCriterion = (fieldN == null || "null".equals(fieldN)) ? "1"
: fieldN;
}

public void setSortDirection(String dir) {
if (dir == null || "null".equals(dir) || "asc".equalsIgnoreCase(dir)) {
sortDirection = SortOrderEnum.ASCENDING;
} else {
sortDirection = SortOrderEnum.DESCENDING;
}
}

public String getSortDirectionStr() {
if (sortDirection == SortOrderEnum.ASCENDING)
return "ASC";
else
return "DESC";
}

public SortOrderEnum getSortDirection() {
return sortDirection;
}

public void setSortDirection(SortOrderEnum sortDirection) {
this.sortDirection = sortDirection;
}

public String getSearchId() {
return searchId;
}

public void setSearchId(String searchId) {
this.searchId = searchId;
}
}

[size=large]第二步:[/size]
实现一个基础类。这个是一个action,如果哪个类要实现分页,需要继承这个类,对显示的数据,及格式做初始化。代码如下:
public class SearchBaseAction extends ActionSupport {
protected static final long serialVersionUID = -7269846658053183360L;
//页显示数目
protected int pageSize = 10;//手动设置需要显示的数目
//当前页数
protected int page;
//排序格式
protected String dir;
protected PaginatedListHelper paginaredList;
//排序字段 了
protected String sort;

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
if (pageSize < 0)
pageSize = 10;
this.pageSize = pageSize;
}

public int getPage() {
return page;
}

public void setPage(int page) {
if (page < 0)
page = 1;
this.page = page;
}

public String getSort() {
return sort;
}

public void setSort(String sort) {
this.sort = sort;
}

public String getDir() {
return dir;
}

public void setDir(String dir) {
this.dir = dir;
}

@SuppressWarnings("unchecked")
public PaginatedListHelper createPaginaredList(List l, int totalCount) {
paginaredList = new PaginatedListHelper();
paginaredList.setPageNumber(this.page);
paginaredList.setObjectsPerPage(pageSize);
paginaredList.setSearchId(sort);
paginaredList.setSortCriterion(sort);
paginaredList.setSortDirection(dir);
paginaredList.setFullListSize(totalCount);
paginaredList.setList(l);
return paginaredList;
}

@SuppressWarnings("unchecked")
public PaginatedListHelper createPaginaredList(List l, int totalCount,int pageSize) {
paginaredList = new PaginatedListHelper();
paginaredList.setPageNumber(this.page);
paginaredList.setObjectsPerPage(pageSize);
paginaredList.setSearchId(sort);
paginaredList.setSortCriterion(sort);
paginaredList.setSortDirection(dir);
paginaredList.setFullListSize(totalCount);
paginaredList.setList(l);
return paginaredList;
}

public int getFromIndex() {
return (this.page - 1) * pageSize;

}

public int getLimit() {
return pageSize;
}

public PaginatedListHelper getPaginaredList() {
return paginaredList;
}

public void setPaginaredList(PaginatedListHelper paginaredList) {
this.paginaredList = paginaredList;
}

}

第三步:
编写自己的action,得到要显示的数据,具体代码如下:
public class ListUserAction extends SearchBaseAction {

/**
*
*/
private static final long serialVersionUID = 1L;
private UserService userService;//自己的数据库连接类,处理数据库
private String name;
private String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public UserService getUserService() {
return userService;
}

public void setUserService(UserService userService) {
this.userService = userService;
}

public String getSort() {
return sort;
}

public void setSort(String sort) {
this.sort = sort;
}

public String getDir() {
return dir;
}

public void setDir(String dir) {
this.dir = dir;
}

@SuppressWarnings( { "static-access" })
@Override
public String execute() throws Exception {
// 如果当前页为零,则默认为1
if (this.page == 0) {
this.page = 1;
}
// 得到起始索引
int fromIndex = this.getFromIndex();
// 得到每页显示数量
int limit = this.getLimit();
//修改display:tag标签的属性,并返回当前页需要显示的数据
List displayDate = userService.getDisplaydate(fromIndex,limit);//具体方法实现内容,根据自己的业务逻辑实现,使用时,只要得到一个每页显示的数目就ok,返回list,具体实现sql语句根据数据库不同而不同(比如mysql,select × from table limit form,end。mssql采用top,orcale使用rownum,本人采用的是hibernate,)
int count = userService.getCount();//返回所有的数据数量,
//这是关键,也就是唯一使用到分页的重要一步,这个方法只接受两个参数,你也可以扩展上边的那个action,实现自己的一些方法,这个方法只接受两个参数,一个是要显示的数据,第二个是总共数据量。
paginaredList = this.createPaginaredList(displayDate,
count);
return this.SUCCESS;
}
}
第四步jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>All user</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>
<center>
<display:table name="${paginaredList}" export="true"
defaultorder="ascending" sort="list" requestURI="listUser.action"
cellspacing="1" cellpadding="1">
<display:setProperty name="sort.amount" value="list" />
<display:setProperty name="export.amount" value="list" />
<display:setProperty name="paging.banner.placement" value="bottom" />
<display:column property="id" title="序号" sortable="true" paramId="page" paramProperty="page"/>
<display:column property="name" title="姓名" sortable="true" />
<display:column property="password" title="密码" />
<display:column value="修改" href="updateUser.action"
paramId="user.id" paramProperty="id"></display:column>
<display:column value="删除" href="deleteUser.action"
paramId="user.id" paramProperty="id"></display:column>
</display:table>
<BR />
</center>
</body>

</html>