List分页代码-JAVA
之前项目里用的Hibernate分页,结果出现了BUG,最后优化代码就将数据库结果查询出来装载到list中,对list进行操作分页。
下面代码部分,分为ListHelper.java/Pagination.java/剩下的是Action中方法的实现和页面的Pager呈现。
ListHelper.java
package com.cn.***.util;
import java.util.List;
import javax.servlet.http.*;
public class ListHelper {
public static Pagination getPager(HttpServletRequest request,List list) {
int currentPage = 1;
if(request.getParameter("currentPage")!=null){
currentPage = Integer.parseInt(request.getParameter("currentPage"));
}
Pagination pager = new Pagination(currentPage,10,list);
if (currentPage < 0) {
pager.setUseDefaultRows(); //如果当前行为空,则设置默认的行数
}
String pagerMethod = request.getParameter("pageC");
if (pagerMethod != null) {
if (pagerMethod.equals("first")) {
pager.first();
} else if (pagerMethod.equals("previous")) {
pager.previous();
} else if (pagerMethod.equals("next")) {
pager.next();
} else if (pagerMethod.equals("last")) {
pager.last();
}
}
return pager;
}
}
Pagination.java
package com.cn.***.util;
import java.util.List;
import java.util.ArrayList;
public class Pagination{
private int currentPage = 1; //当前页
public int next = 0; //下一页
public int previous = 0; //前一页
private int rows = 0; //显示条数
private List list = null; //集合
public final int defaultRows = 10; //默认每页显示条数
private int totalPages = 0; //总共的页数
public Pagination(int currentPage,int rows,List list){
this.currentPage = currentPage;
this.rows = rows;
this.list = list;
this.totalPages = getPages(list.size());
}
public void first(){
this.currentPage=1;
}
public void previous(){
if(this.currentPage==1){
return ;
}
this.currentPage -- ;
}
public void next(){
if (this.currentPage < this.totalPages) {
this.currentPage++;
}
}
public void last(){
this.currentPage = this.totalPages;
}
public int getTotalPages(){
return this.totalPages;
}
public void setCurrentPage(int currentPage){
this.currentPage = currentPage;
}
public int getCurrentPage(){
return this.currentPage;
}
public int getNext(){
return this.next;
}
public int getPrevious(){
return this.previous;
}
public void setRows(int rows){
this.rows = rows;
}
private int getRows(){
return this.rows;
}
public void setList(List list){
this.list = list;
}
public void setUseDefaultRows(){
this.rows = defaultRows;
}
public Pagination(){
this.rows = defaultRows;
}
public List doPagination(){ //分页
if(list==null){
System.out.println("未传入必须的List对象.");
throw new NullPointerException("未传入必须的List对象.");
}
List temp = new ArrayList();
int size = list.size();
int pages = this.getPages(size); //计算总页数
int start = 0;
int end = 0;
this.totalPages = pages;
if(this.currentPage>=pages){
this.currentPage = pages;
start = (pages-1)*this.rows;
end = size;
this.previous = pages-1;
this.next = pages;
}else if(this.currentPage
Action中方法
request.getSession().removeAttribute("newspagerlist");
Pagination pager = (Pagination)request.getAttribute("pager");
List newsList = new ArrayList();
pager = ListHelper.getPager(request, newsList); //初始化List分页
newsList=***** //对需要处理的List进行装载
if (pager == null) {
pager = new Pagination(pager.getCurrentPage(),1,newsList);
}
newsList = new ArrayList();
newsList= pager.doPagination();
request.getSession().setAttribute("newspagerlist", newsList);
request.getSession().setAttribute("pager", pager); return;
Pager呈现
第 页
共 页
首页
上一页
下一页
尾页