DisplayTagAndHibernatePagination 分页

本文介绍如何使用Display Tag库和Hibernate实现分页功能。通过三个步骤,包括在JSP文件中设置Display Tag参数、修改Action文件获取页数及调整数据访问层以返回分页列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[url]http://raibledesigns.com/wiki/Wiki.jsp?page=DisplayTagAndHibernatePagination[/url]

our trail:

Display Tag and Hibernate Pagination - How to achieve pagination in display tag library using hibernate in 3 easy steps.

1) Add partialList="true" size="resultSize" to the display tag in your jsp file like so:


<display:table name="collection" cellspacing="0" cellpadding="0" requestURI="" 
defaultsort="1" id="c" pagesize="25" partialList="true" size="resultSize" class="list" export="false">


2) Modify your action file by adding a method which gets the page number from the request


public final class DisplayTagAction extends BaseAction {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("Entering 'execute' method");
}

TestDataManager amgr = (TestDataManager) getBean("testDataManager");

//get the page number from request
int page = getPage(request);
int size = 25;

//pass in page and size
request.setAttribute("collection", amgr.getTestData(page, size);

//get the total size of collection , required for display tag to get the pagination to work
request.setAttribute("resultSize", new Integer(amgr.getTestDataSize()));

return mapping.findForward("displayTagPage");

}


public int getPage(HttpServletRequest request){
int page=0;
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String name = (String)paramNames.nextElement();
if (name != null && name.startsWith("d-") && name.endsWith("-p")) {
String pageValue = request.getParameter(name);
if (pageValue != null) {
page = Integer.parseInt(pageValue)-1;
}
}
}
return page;
}


3) Modify the data access call to return a paginated list.


public List getTestData(int page, int pageSize){

Query query = getSession().createQuery("from Test");
return query.setFirstResult(page * pageSize).setMaxResults(pageSize+1).list();
}

public int getTestDataSize() {

return ((Integer)getSession().createQuery("select count(*) from Test").uniqueResult()).intValue();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值