例如我做的书城,其中的分页方式
首先需要一个domain类,用来记录总条数,每页规定显示数,总页数,当前页
package domain;
import java.util.List;
public class PageResult {
List list;
long totalCount;//总条数
int pageSize = 4;//每页显示条数
int totalPage;//总页数
int currentPage;//当前页
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public long getTotalCount() {
return totalCount;
}
public void setTotalCount(long totalCount) {
this.totalCount = totalCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
@Override
public String toString() {
return “PageResult [totalCount=” + totalCount + “, pageSize=” + pageSize + “, totalPage=” + totalPage
+ “, currentPage=” + currentPage + “]”;
}
}
这是我的domain类
创建一个service类
在service类中定义总页数的规则,及设置当前页数
package service;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.ldap.PagedResultsControl;
import dao.ProductDao;
import domain.PageResult;
import domain.Product;
public class ProductService {
ProductDao productDao=new ProductDao();
public PageResult<Product> findBooks(String category,int page){
try {
//创建模型
PageResult<Product> pr=new PageResult<Product>();
//设置总记录数
long totalCount=new ProductDao().count(category);
pr.setTotalCount(totalCount);
System.out.println(category);
System.out.println(totalCount);
//设置总页数
int totalPage=(int) Math.ceil(totalCount*1.0/pr.getPageSize());
pr.setTotalPage(totalPage);
//设置当前页数
pr.setCurrentPage(page);
//设置数据list
List<Product> al=productDao.findBooks(category, page, pr.getPageSize());
pr.setList(al);
return pr;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
其中在设置总页数总使用了Math.ceil方法
Math.ceil() 函数返回大于或等于一个给定数字的最小整数。
totalCount*1.0/pr.getPageSize()
我们定义总页数的规则,totalCount是总条数,在PageResult类中我们设置了pageSize为4,也就是一页显示四条信息,用总条数,除去4,例如现在有17条数据,17除去4还余了1,Math.ceil()方法的作用就体现出来了
servlet
package web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import domain.PageResult;
import domain.Product;
import service.ProductService;
@WebServlet("/showProductByPage")
public class ShowProductByPageServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String category =req.getParameter("category");
byte[] buf = category.getBytes("iso8859-1");
//第二步: 使用utf-8码表进行解码
category = new String(buf,"utf-8");
System.out.println(category);
String pageStr=req.getParameter("page");
int page=1;
if(pageStr!=null && !"".equals(pageStr)){
page=Integer.parseInt(pageStr);
}
//调用service
ProductService ps=new ProductService();
PageResult<Product> pageResult=ps.findBooks(category, page);
//存入request
req.setAttribute("pageResult", pageResult);
req.setAttribute("category", category);
//跳转页面
req.getRequestDispatcher("/product_list.jsp").forward(req, resp);
}
}
在页面中,使用c:forEach来显示页数,及当前页数