分页程序

分页程序实际很简单,下面我介绍一下自己写的,可能很烂 :cry:
1.构造传递DTO。代码如下:
import java.util.List;
public class PaginationDto {
private int totalCount;
private int currentPage;
private int totalPages;
private int indexPage = 1;
private int prePage;
private List nums;
private int nextPage;
private int lastPage;
private List data;
//生成getter/setter方法
}
2.构造分页逻辑。代码如下:
public class PaginationBean {
public PaginationBean() {}
//构造导航信息
public PaginationDto buildNavgation(int currentPage,boolean byType,String typeId) {
PaginationDto dto = new PaginationDto();
List nums = new ArrayList();
//查找数据总条数
int totalCount = findTotalCount();
//每页20条记录
int totalPages = totalCount / 20;
//计算总页数
int tp = totalCount % 20;
if (tp != 0) {
totalPages += 1;
}
int indexPage = 1;
int prePage = -1;
if (currentPage == 1) {
prePage = -1;
} else {
prePage = currentPage - 1;
}
int lastPage = totalPages;
int nextPage = -1;
if (currentPage < lastPage) {
nextPage = currentPage + 1;
} else {
nextPage = -1;
}
if (currentPage == 1) {
if (totalPages <= 20) {
nums.clear();
for (int i = 0; i < totalPages; i++) {
nums.add(String.valueOf(i + 1));
}
} else {
nums.clear();
for (int i = 0; i < 20; i++) {
nums.add(String.valueOf(i + 1));
}
}
} else {
if (currentPage >= 20) {
int moveNum = 2;// currentPage 20;
int begin = currentPage - moveNum;
int end = currentPage + 20 - moveNum;
if (end > totalPages) {
end = totalPages;
}
nums.clear();
for (int i = begin; i <= end; i++) {
nums.add(i + "");
}
} else {
nums.clear();
if (currentPage <= totalPages) {
nums.clear();
for (int i = 0; i < totalPages; i++) {
nums.add(String.valueOf(i + 1));
}
} else {
nums.clear();
for (int i = 0; i < 20; i++) {
nums.add(String.valueOf(i + 1));
}
}
}
}
//计算取数据的开始和结束位置
int currentCountE = currentPage * 20 - 1;
int currentCountB = currentCountE - 19;
if (currentCountE > totalCount) {
currentCountE = totalCount;
}
//根据当前页码计算要取得数据
List result = findLimit(currentCountB, currentCountE);
dto.setCurrentPage(currentPage);
dto.setTotalPages(totalPages);
dto.setData(result);
dto.setIndexPage(indexPage);
dto.setLastPage(lastPage);
dto.setNextPage(nextPage);
dto.setNums(nums);
dto.setPrePage(prePage);
dto.setTotalCount(totalCount);
dto.setTotalPages(totalPages);
dto.setData(result);
return dto;
}

public int findTotalCount() {
//查询总记录数
}

public List findLimit(int down, int up) {
//这里给出的是ORACLE的专用分页代码
//你的SQL就是SELECT name,password from yourtable
String sql = "select * from ( select row_.*, rownum rownum_ from ( ";
sql+=" SELECT name,password from yourtable";
sql+=" ) row_ where rownum <=?) where rownum_ >?";
}}
3.jsp代码

<div id="nav" align="center">
共计${dto.totalCount}条记录 ;当前位置${dto.currentPage}/${dto.totalPages}
<a href="yourManage.do?method=page&cp=1&id=${id}">【首页】</a>
<c:if test="${dto.prePage!=-1}">
<a href="yourManage.do?method=page&cp=${dto.prePage}&id=${id}">【上一页】</a>
</c:if>
<c:if test="${dto.prePage<0}">
【上一页】
</c:if>
<c:forEach items="${dto.nums}" var="obj">
<a href="yourManage.do?method=page&cp=${obj}&id=${id}">${obj}</a>
</c:forEach>
<c:if test="${dto.nextPage==-1}">
【下一页】
</c:if>

<c:if test="${dto.nextPage!=-1}">
<a href="yourManage.do?method=page&cp=${dto.currentPage}&id=${id}">【下一页】</a>
</c:if>

<a href="yourManage.do?method=page&cp=${dto.lastPage}&id=${id}">【末页】</a>

</div>
4.action代码
int currentPage = Integer.parseInt(request.getParameter("cp"));
String id = request.getParameter("id");
if(id==null||id.equals("")){
PaginationBean pb = new PaginationBean();
List types = logic.groupType("-1",false);
request.setAttribute("types", types);
PaginationDto dto = pb.buildNavgation(currentPage,false,"");
request.setAttribute("dto",dto);
}else{
PaginationBean pb = new PaginationBean();
List types = logic.groupType("-1",false);
request.setAttribute("types", types);
request.setAttribute("id", id);
PaginationDto dto = pb.buildNavgation(currentPage,true,id);
request.setAttribute("dto",dto);
}
return mapping.findForward("index");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值