您所在的位置: 首页>>开发>>JAVA>>JAVA专区>>热门框架>>Struts>>

我的struts分页算法的实现(3)

  • 摘要:本文作者用struts框架来完成分页算法。具体的程序实现包括:1、根据总的页数,(假设当前页为1);2、根据总的页数,当前页。这两种方法都是取需要显示的数据显示,这样,在数据库庞大的情况下,比一次性把所有数据都取出来的效率要高。个人比较推荐第二种方法。
  • 标签:Java  struts  分页  算法

二、根据总的页数,当前页

这样的话构造函数应该写成:

public PageBean(int totalRows,int currentPage) {
this.totalRows = totalRows;
this.currentPage = currentPage;
if(currentPage < 2)
hasPreviousPage = false;
else
hasPreviousPage = true;
if ((totalRows % pageRecorders) == 0) {
totalPages = totalRows / pageRecorders;
} else {
totalPages = totalRows / pageRecorders + 1;
}
if (currentPage < totalPages) {
hasNextPage = true;
nextPage = currentPage + 1;
pageStartRow = (currentPage - 1)*pageRecorders;
this.pageEndRow = pageStartRow + pageRecorders;
} else if(currentPage == totalPages){
pageStartRow = (currentPage - 1)*pageRecorders;
this.pageEndRow = totalRows;
hasNextPage = false;
nextPage = currentPage;
}
if(currentPage < 2){
previousPage = currentPage;
hasPreviousPage = false;
}else if(currentPage > 1){
previousPage = currentPage-1;
hasPreviousPage = true;
}
}

在action中应该写成:

if(currentPage == null){
pb = new PageBean(totalRows);
session.removeAttribute("page");
queryString = employeeForm.getQueryString();
queryCon = employeeForm.getQueryCon();
session.setAttribute("queryString",queryString);
session.setAttribute("queryCon",queryCon);  
list = employeeDao.getAllEmployee(queryString, queryCon,
String.valueOf(pb.getPageStartRow()),
String.valueOf(pb.getPageRecorders()));
}
else{
pb = new PageBean(totalRows,Integer.parseInt(currentPage));
queryString = employeeForm.getQueryString();
queryCon = employeeForm.getQueryCon();
session.setAttribute("queryString",queryString);
session.setAttribute("queryCon",queryCon);  
list = employeeDao.getAllEmployee(queryString, queryCon,
String.valueOf(pb.getPageStartRow()),
String.valueOf(pb.getPageRecorders()));
}
session.setAttribute("page",pb);       
request.setAttribute("admin", "admin");
request.setAttribute("employee", list);
return mapping.findForward("showlist");

在jsp中应该写成:

<logic:equal name="page" property="hasNextPage" value="true">
<a href="List.do?currentPage=<bean:write name="page" property="nextPage"/>">
nextPage
</a>
</logic:equal>
<logic:equal name="page" property="hasPreviousPage" value="true">
|
<a href="/test/List.do?currentPage=<bean:write name="page" property="previousPage"/>">
PreviousPage
</a>
</logic:equal>

数据库查询部分依然适用。

尽管洋洋洒洒贴了一部分代码,不过好像不太想看,包括我,也比较讨厌看一些烦琐的代码,所以如果你想要源代码进行探讨研究的话,欢迎随时找我。那就总结一下这两种方法吧!

首先这两种方法都是取需要显示的数据显示,这样,在数据库庞大的情况下,比一次性把所有数据都取出来的效率要高。

第一种方法是把PageBean存在了一个HttpSession中,在进入到显示列表的时候就进行了初始化,在jsp页面传递的参数action是固定的三个值:null,nextPage,previousPage.这样虽然比较容易理解,但是我发现一个BUG,就是如果你按刷新,他也会翻页,因为他的url就是 .do?aciton=nextPage,这样的话你传进去的action还是有一个值,这样就会导致翻页。

第二种方法是考虑了第一种方法的BUG,在Jsp页面传递的参数currentPage的值是bean中的nextPage的值或者previousPage里的值,用了struts标签库嵌套,把值赋予currentPage,这样的话currentPage的值是:1,2,3,4...totalPages之间。这样的话你即使按刷新按钮,他也是当前页,因为他的url就是.do?currentPage=someNumber(someNumber是1到totalPages中的一个值)。但是这样的话Pagebean在每次访问的时候都要重新生成一个对象,该对象也是根据totalRows(总数据数),currentPage(当前页数)进行构造,从而设置其他的一些属性。个人比较推荐第二种方法。

(责任编辑 火凤凰 sunsj@51cto.com  TEL:(010)68476636-8007)



共3页: 上一页 [1] [2] 3
【内容导航】
Java实用开发全集
Java类的基础教程专题
Java发展动态专题
Struts框架应用专题
Java编程开发手册
 
 验证码: (点击刷新验证码)   匿名发表
  • Visual C++ 完全自学宝典

  • 作者:强锋科技,朱洪波
  • Visual C++ 6.0是微软公司为程序人员提供的Visual Studio 6.0工具套件中的重要组成部分。本书由浅入深地介绍使用Visual C++ 6.0..
Copyright©2005-2008 51CTO.COM 版权所有