1.首先创建一个page.java的文件,然后实现get和set的方法
package com.systop.rwgl.page.model;
import java.util.List;
public class Page<T> {
private int currentPage = 1; // 当前页, 默认显示第一页
private int pageCount = 4; // 每页显示的行数(查询返回的行数), 默认每页显示4行
private int totalCount; // 总记录数
private int totalPage; // 总页数 = 总记录数 / 每页显示的行数 (+ 1)
private List<T> pageData; // 分页查询到的数据
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
if (totalCount % pageCount == 0) {
totalPage = totalCount / pageCount;
} else {
totalPage = totalCount / pageCount + 1;
}
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getPageData() {
return pageData;
}
public void setPageData(List<T> pageData) {
this.pageData = pageData;
}
}
2.在servlet的文件查询所有的方法中,写入分页的内容,在查询所有的基础上分页
if(action != null && action.equals("sel")) {
//首先实例化一个Dao文件
staff_up_managerDao staff_up_managerdao = new staff_up_managerDao();
List list = null;//创加一个集合
try {
//在staff_up_managerDao中调用getApply的方法,放到集合中
list = staff_up_managerdao.getApply("");
} catch (Exception e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
//定义一个currPage,得到当前页的值,赋给currPage
String currPage = request.getParameter("currentPage");
// 第一次访问,设置当前页为1;
if (currPage == null || "".equals(currPage.trim())){
currPage = "1";
}
// 转换
int currentPage = Integer.parseInt(currPage);
//2. 创建PageBean对象,设置当前页参数; 传入service方法参数
Page<staff_up_manager> page = new Page<staff_up_manager>();
page.setCurrentPage(currentPage);
//使用session,得到当前的id
HttpSession session = request.getSession();
Employee e = (Employee) session.getAttribute("employee");
int id = e.getId();
try {
//在staff_up_manangerDao中调用getApplys的方法
staff_up_managerdao.getApplys(page);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String context = request.getContextPath();
String pageContent = "";
if(currentPage == 1) {
pageContent =
"<UL class=\"am-pagination\">" +
" <LI><A\r\n" + "href=\""+context+"/staff_up_manager?action=sel¤tPage="+(currentPage-1)+"\">«</A></LI>\r\n" +
" <LI class=\"am-active\"><A\r\n" + "href=\""+context+"/staff_up_manager?action=sel¤tPage="+(currentPage)+"\">"+currentPage+"</A></LI>\r\n" +
" <LI class=\"\"><A\r\n" + "href=\""+context+"/staff_up_manager?action=sel¤tPage="+(currentPage+1)+"\">"+(currentPage+1)+"</A></LI>\r\n" +
" <LI class=\"\"><A\r\n" + "href=\""+context+"/staff_up_manager?action=sel¤tPage="+(currentPage+2)+"\">"+(currentPage+2)+"</A></LI>\r\n" +
" <LI class=\"\"><A\r\n" + "href=\""+context+"/staff_up_manager?action=sel¤tPage="+(currentPage+3)+"\">"+(currentPage+3)+"</A></LI>\r\n" +
" <LI class=\"\"><A\r\n" + "href=\""+context+"/staff_up_manager?action=sel¤tPage="+(currentPage+4)+"\">"+(currentPage+4)+"</A></LI>\r\n" +
" <LI><A\r\n" + "href=\""+context+"/staff_up_manager?action=sel¤tPage="+(currentPage+1)+"\">»</A></LI>\r\n" +
"</UL>";
}
3.在Dao文件中写入分页的代码,一个方法是在得到所有值的情况下分页,第二个方法是的到所有的条数
public void getApplys(Page<staff_up_manager> tr) throws Exception{
//得到总的页数
int totalCount = this.getTotalCount();
tr.setTotalCount(totalCount);
//判断总的页数是否大于0
if (tr.getCurrentPage() <=0) {
// 把当前页设置为1
tr.setCurrentPage(1);
} else if (tr.getCurrentPage() > tr.getTotalPage()){
// 把当前页设置为最大页数
tr.setCurrentPage(tr.getTotalPage());
}
int currentPage = tr.getCurrentPage();
// 查询的起始行
int index = (currentPage -1 ) * tr.getPageCount();
// 查询返回的行数
int count = tr.getPageCount();
//分页查询数据 把查询到的数据设置到tr对象中
String sql = "select ID,TASK_EMPLOYEE_ID,UP_XM,UP_GH,UP_SF ,UP_GRNL from task_up_apply limit ?,?";
Connection conn = null;
PreparedStatement stmt = null;
conn = getConn();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, index);
stmt.setInt(2, count);
ResultSet rs = stmt.executeQuery();
//创建一个集合staff_up_manager
List staff_up_manager = new ArrayList<staff_up_manager>();
while(rs.next()) {
staff_up_manager e = new staff_up_manager();
//从结果集中获取到每个字段的值,然后存入到staff_up_manager
e.setId(rs.getInt(1));
e.setApemid(rs.getInt(2));
e.setXm(rs.getString(3));
e.setGh(rs.getString(4));
e.setSf(rs.getString(5));
e.setGrnl(rs.getString(6));
staff_up_manager.add(e);
//将staff_up_manager中的值,放到pageDate中
List<staff_up_manager> pageDate = staff_up_manager;
tr.setPageData(pageDate);
}
}
//查询总条数
public int getTotalCount() {
Connection conn = null;
PreparedStatement stmt = null;
//首先设置总条数为0
int count = 0 ;
//在task_up_apply中查询一共有多少条的信息
String sql = "select count(*) FROM task_up_apply ";
try {
conn = getConn();
stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
if(rs.next()) {
count = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}
return count; //返回总条数
}
4.最后在前台中完成分页
共 ${counts}条记录//总共有多少条记录
<DIV class="am-fr">${pageContent}</DIV>//显示出来分页