Displaytag1.1支持在外部实现大数据量分页。主要有两种方式实现:
Displaytag 1.1 offers two alternative ways for working with partial lists:
-
the first one uses the valuelist pattern, and requires that the object that you give to displaytag implements the org.displaytag.pagination.PaginatedList interface. You can pass this object to displaytag as an usual list, and it will extract paging and sorting information from it. This way is more recommended if you have to build your backend layer and you can easily follow this pattern.
-
a second way, recommended if you only have to use partial list for few tables that show a performance problem using full lists, is passing all the needed parameters as separate tag attributes (recors to be shown, page number, total number of records...)
其中第二种方式看起来较为简单,只需在<displaytag>中增加两个属性:partialList="true"size="resultSize"即可,但是在使用发现这种方式由于是在原来内存分页的基础上修改的,所以还是存在一些Bug(比如:分页导航的值不会变......)
所以这里介绍Displaytag推荐的第一种方式,即:
实现org.displaytag.pagination.PaginatedListorg.displaytag.pagination.PaginatedList接口。
实现PaginatedList接口:
import
java.util.List;
import
org.displaytag.pagination.PaginatedList;
import
org.displaytag.properties.SortOrderEnum;
/** */
/**
* 分页列表
*
*
@author
John.Zhu
*
*/
public
class
PageList
implements
PaginatedList
{
/** */
/**
* 每页的列表
*/
private
List list;
/** */
/**
* 当前页码
*/
private
int
pageNumber
=
1
;
/** */
/**
* 每页记录数 page size
*/
private
int
objectsPerPage
=
15
;
/** */
/**
* 总记录数
*/
private
int
fullListSize
=
0
;
private
String sortCriterion;
private
SortOrderEnum sortDirection;
private
String searchId;
public
List getList()
{
return
list;
}
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 sortCriterion)
{
this
.sortCriterion
=
sortCriterion;
}
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;
}
}
Contoller:
/** */
/**
* 分页查询
*
*
@param
request
*
@param
response
*
@return
*
@throws
Exception
*/
private
ModelAndView paging(HttpServletRequest request,
HttpServletResponse response, ModelAndView mav)
throws
Exception
{
//
获取当前页数,displaytag通过参数"page"传递这个值
int
pageNumber;
if
(request.getParameter(
"
page
"
)
!=
null
&&
!
""
.equals(request.getParameter(
"
page
"
)))
{
pageNumber
=
Integer.parseInt(request.getParameter(
"
page
"
));
}
else
{
pageNumber
=
1
;
}
PageList pageList
=
new
PageList();
Map map
=
WebUtils.getParametersStartingWith(request,
"
search_
"
);
Page page
=
dao.query(map, pageNumber, PAGE_SIZE);
//
设置当前页数
pageList.setPageNumber(pageNumber);
//
设置当前页列表
pageList.setList(page.getResults());
//
设置page size
pageList.setObjectsPerPage(PAGE_SIZE);
//
设置总页数
pageList.setFullListSize(page.getTotalCount());
mav.addObject(
"
results
"
, pageList);
return
mav;
}
好了,现在直接在JSP上使用 displaytag就行了:
<
display:table
name
="${results}"
id
="row"
class
="simple nocol"
pagesize
="${pageSize}"
export
="true"
decorator
="org.displaytag.decorator.TotalTableDecorator"
requestURI
="${pageContext.request.contextPath}/pageQuery.do?method=paging"
>
<
display:column
property
="date.time"
title
="日期"
format
="{0,date,yyyy-MM-dd}"
sortable
="false"
/>
<
display:column
property
="code"
title
="编码"
group
="1"
sortable
="false"
/>
<
display:column
property
="name"
title
="名称"
group
="1"
sortable
="false"
/>
<
display:column
property
="costTotal"
title
="总额(元)"
format
="{0,number,0,000}"
total
="true"
sortable
="false"
/>
</
display:table
>