第一步:我们首先确定每页要放多少数据,一共多少数据,要分多少页,对上一页和下一页、首页、尾页进行页数分析,由此依据创建工具类page,page接受网页返回的当前页数(若无页数数据则默认为一),返回正确的上一页下一页...的page数,servlet通过调用page工具类实现正确的页数查询。最后返回数据到jsp页面显示。
那么,工具类:
package com.person.util; public class PageUtil { private Integer currentPage;//当前页 private Integer firstPage;//第一页 private Integer lastPage;//尾页 private Integer nextPage;//下一页 private Integer proPage;//上一页 private Integer totalPage;//总页数 private Integer totalCount;//总数据量 private Integer index;//分页查询的索引 private PageUtil(){} public PageUtil(String currentPage,Integer totalCount){ this.currentPage=currentPage==null? 1: Integer.parseInt(currentPage); this.totalPage=(totalCount+4)/5; this.firstPage = 1; this.lastPage = this.totalPage; this.nextPage = this.currentPage==this.lastPage ? this.lastPage : this.currentPage+1; this.proPage= this.currentPage==1?1:this.currentPage-1; this.totalCount=totalCount; this.index=(this.currentPage-1)*5; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getFirstPage() { return firstPage; } public void setFirstPage(Integer firstPage) { this.firstPage = firstPage; } public Integer getLastPage() { return lastPage; } public void setLastPage(Integer lastPage) { this.lastPage = lastPage; } public Integer getNextPage() { return nextPage; } public void setNextPage(Integer nextPage) { this.nextPage = nextPage; } public Integer getProPage() { return proPage; } public void setProPage(Integer proPage) { this.proPage = proPage; } public Integer getTotalPage() { return totalPage; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public Integer getTotalCount() { return totalCount; } public void setTotalCount(Integer totalCount) { this.totalCount = totalCount; } public Integer getIndex() { return index; } public void setIndex(Integer index) { this.index = index; } @Override public String toString() { return "PageUtil{" + "currentPage=" + currentPage + ", firstPage=" + firstPage + ", lastPage=" + lastPage + ", nextPage=" + nextPage + ", proPage=" + proPage + ", totalPage=" + totalPage + ", totalCount=" + totalCount + ", index=" + index + '}'; } }
接下来进行DAO的编写:
接口:
/** * 根据具体的page(页数)进行固定个数的信息查询 * @param index * @return */ List<Map<String,Object>> getAdminByIndex(Integer index);
impl:
@Override public List<Map<String, Object>> getAdminByIndex(Integer index) { String sql="select * from administrators limit ?,5"; return DBUtil.executeQuery(sql,index); }
准备工作完成后进行servlet的编写:
package com.person.servlet; import com.person.dao.IAdminDAO; import com.person.dao.impl.AdminDAOImpl; import com.person.util.PageUtil; 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 java.io.IOException; import java.util.List; import java.util.Map; @WebServlet("/dromAdminHome.isLogin") public class DromAdminHomeServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //将此page页面的信息传递过来 String page=req.getParameter("page"); //设置页面信息进工具类, PageUtil pageUtil = new PageUtil(page,7); //查询本页面的信息: IAdminDAO adminDAO = new AdminDAOImpl(); List<Map<String,Object>> allAdmin=adminDAO.getAdminByIndex(pageUtil.getIndex()); //将查询出来的信息设置入req中同时将工具类中计算出来的其他可供点击页面的信息也存入req中 //这样若点击到时则执行本servlet,则又会计算出点击页面其外的其他页面的page,本页面的信息则进行查询显示 req.setAttribute("allAdmin",allAdmin); req.setAttribute("pageData",pageUtil); req.getRequestDispatcher("/views/dromAdminHome.jsp").forward(req,resp); } }
最后进行jsp页面的编写:
<table cellspacing="0px"> <tr> <td>编号</td> <td>姓名</td> <td>性别</td> <td>电话</td> <td>宿舍楼</td> <td>用户名</td> <td>操作</td> </tr> <c:forEach var="a" items="${allAdmin}"> <tr> <td>${a.id}</td> <td>${a.name}</td> <td>${a.gender}</td> <td>${a.telephone}</td> <td>${a.dormitory}</td> <td>${a.username}</td> <td><a href="/yuer/views/updateAdmin.jsp">修改</a> <a href="/yuer/deleteAdmin?id=${a.id}">删除</a></td> </tr> </c:forEach> </table> <div style="text-align: center"> <a href="/yuer/dromAdminHome.isLogin?page=${pageData.firstPage}" >首页</a> <a href="/yuer/dromAdminHome.isLogin?page=${pageData.proPage}" >上一页</a> <a href="/yuer/dromAdminHome.isLogin?page=${pageData.nextPage}" >下一页</a> <a href="/yuer/dromAdminHome.isLogin?page=${pageData.lastPage}" >尾页</a> 共【${pageData.currentPage}/${pageData.totalPage}】页 每页显示【5】条 </div>
over~~~~~~~~~~~