第一步分析我们做分页需要什么数据:
我们从两个方向来分析:1 页面方向,2 servlet方向
一 ,页面方向
1 当前页 currPageCode
2 总 页数 totalPage
3一页中的记录数据 datas
二,servlet方向
1 当前页 currPageCode
2 总页数 totalPage
3 每页记录数 pagesize
4 一共多少条记录 totalRecord
5 一页中的记录数据 datas
6当前页第一条记录的行数 currPageBeginIndex
7 url
[html] view plain copy
- public class PageBean<T> {
- private int cp;//当前页
- private int pc;//可以通过每页记录数和共多少条记录得到 (dt%md ==0 )?dt/md : dt/md+1 一共多少页
- private int md;//每页记录数
- private List<T> pd;//页中的记录数据
- private int dt;//一共多少条记录
- private int cd;//可以通过每页记录是*(当前页数-1) + 1得到,当前页第一条记录的行数
- private String url;
- //为了清楚起见,我就没有粘上我的setget方法
- }
嗯,有了javabean了,那摩就先让我们写一下servlet吧!
[java] view plain copy
- public String findAll(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- /*
- * 1 通过页面回传的当前页信息设置当前页
- * 2 设置每页所显示的数据
- * 3 调用servlce的findAll方法获得一共多少条记录及当前页的数据
- *
- */
- PageBean<Customer> pb = new PageBean<Customer>();
- if(request.getParameter("cp") != null && !request.getParameter("cp").trim().isEmpty()) {
- int cp = Integer.parseInt(request.getParameter("cp"));
- pb.setCp(cp);
- }
- else {
- pb.setCp(1);
- }
- pb.setMd(10);
- pb.setPd(service.findAll(pb.getCd(),pb.getMd()));
- pb.setDt(service.count());
- String url = getUrl(request);
- pb.setUrl(url);
- request.setAttribute("pb",pb);
- return "f:list.jsp";
- }
嗯,有了数据了,这个方法会转发找list.jsp页面中,我们来看该页的技术点
[html] view plain copy
- <body>
- <h3 align="center">客户列表</h3>
- <table border="1" width="70%" align="center">
- <tr>
- <th>客户姓名</th>
- <th>性别</th>
- <th>生日</th>
- <th>手机</th>
- <th>邮箱</th>
- <th>描述</th>
- <th>操作</th>
- </tr>
- <!-- 第几页 current page cp
- 共几页 page count pc
- 一页显示多少数据 many date md
- 每页所显示的数据 page date pd
- 一共多少条记录 date total dt
- 当前页的第一条记录的行数 current date cd -->
- <!-- 1,这一点就不用多说了吧,我们把我们查找的数据进行遍历 -->
- <c:forEach var="customer" items="${pb.pd}">
- <tr>
- <!-- private String cname;
- private String gender;
- private String birthday;
- private String cellphone;
- private String email;
- private String description;
- -->
- <td>${customer.cname }</td>
- <td>${customer.gender }</td>
- <td>${customer.birthday}</td>
- <td>${customer.cellphone }</td>
- <td>${customer.email }</td>
- <td>${customer.description }</td>
- <td>
- <a href="<c:url value='/customer2?method=forEdit&cid=${customer.cid }'/>">编辑</a>
- <a href="<c:url value='/customer2?method=delete&cid=${customer.cid }'/>">删除</a>
- </td>
- </tr>
- </c:forEach>
- </table>
- <!-- 这是我们分页技术页面部分的实现; -->
- <center>
- 第${requestScope.pb.cp }页/共 ${pb.pc }页
- <a href="<c:url value="${pb.url }"/>">首页</a> <!-- 因为首页也是第一页,所以我们没有加当前页码参数,servlet会自动认为是第一页 -->
- <c:if test="${pb.cp > 1 }"> <!-- 如果当前页面是第一页德华,那摩就禁用上一页 -->
- <a href="<c:url value='${pb.url }&cp=${pb.cp - 1 }'/>">上一页</a>
- </c:if>
- <%--
- 用来进行中间页码的显示及使用超链接所带来的的问题的解决方案
- 如何显示页码呢,我们其实只需要两个数据,begin和end就哦了,
- 我们常见的百度页面有什么特点呢,你可以去看看我就不多说了;下面我给出他的计算公式:
- 如果总页数<=10(列表长度),那么begin=1,end=总页数
- 否则
- 使用公式计算;begin=pc-5, end=pc + 4;
- 两种特殊情况:
- 头溢出:当begin<1时,让begin=1
- 尾溢出:当end>${tp}时,让end=${tp}
- --%>
- <c:choose>
- <c:when test="${pb.pc <= 10 }"> <!-- 判断总页数小于页面大小吗 -->
- <c:set var="begin" value="1"/>
- <c:set var="end" value="${pb.pc }"/>
- </c:when>
- <c:otherwise>
- <c:set var="begin" value="${pb.cp-4 }"/>
- <c:set var="end" value="${pb.cp+5 }"/>
- <c:choose>
- <c:when test="${begin < 1 }">
- <c:set var="begin" value="1"/>
- <c:set var="end" value="10"/>
- </c:when>
- <c:when test="${end > pb.pc }">
- <c:set var="begin" value="${pb.pc-9 }"/>
- <c:set var="end" value="${pb.pc }"/>
- </c:when>
- </c:choose>
- </c:otherwise>
- </c:choose>
- <!-- 通过上面的操作我们可以得到正确的begin和end了下面我们就对其进行遍历 -->
- <c:forEach begin="${begin }" end="${end }" var="i">
- <c:choose>
- <!-- 当前页面显示为普通文本 -->
- <c:when test="${i == pb.cp }">${i }</c:when>
- <!-- 其他页面显示为超链接 -->
- <c:otherwise><a href="<c:url value='${pb.url }&cp=${i }'/>"> [${i }] </a></c:otherwise>
- </c:choose>
- </c:forEach>
- <c:if test="${pb.cp < pb.pc }"> <!-- 如果当前页面时最后一页的话,我们就禁用下一页 -->
- <a href="<c:url value='${pb.url }&cp=${pb.cp + 1 }'/>">下一页</a>
- </c:if>
- <a href="<c:url value='${pb.url }&cp=${pb.pc }'/>">尾页</a>
- </center>
- </body>
哦了多余的话我就不解释了,我想你看过这段代码之后会明白的吧。
那摩接下来让我们来写一个service和dao就算完事了,service我就不说了我们直接来看dao吧
[java] view plain copy
- public List<Customer> findAll(int index,int length) {
- String sql = "select * from t_customer limit ?,?";
- List<Customer> l = null;
- Object[] p = {index,length};
- try {
- l = queryRunner.query(sql, new BeanListHandler<Customer>(Customer.class),p);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return l;
- }
[java] view plain copy
- public int count() {
- <span style="white-space:pre;"> </span>String sql = "select count(*) from t_customer";
- <span style="white-space:pre;"> </span>Number n = null;
- <span style="white-space:pre;"> </span>try {
- <span style="white-space:pre;"> </span>n = (Number)queryRunner.query(sql, new ScalarHandler());
- <span style="white-space:pre;"> </span>
- <span style="white-space:pre;"> </span>} catch (SQLException e) {
- <span style="white-space:pre;"> </span>// TODO Auto-generated catch block
- <span style="white-space:pre;"> </span>e.printStackTrace();
- <span style="white-space:pre;"> </span>}
- <span style="white-space:pre;"> </span>return n.intValue();
- <span style="white-space:pre;"> </span>}
是滴,全部查找就是这摸简单,只要还是得利用当前行数,和每页记录数
哦了
有人会问我getUrl()方法是什么鬼,还有encoding()方法又是你妈什么?
应为我们是使用get提交的所以会造成乱码,嗯对,我们获取的customer对象他爹都不认识他,我们的encoding方法就是用来进行解码的
[java] view plain copy
- //这是其中的一个,其他的属性也如同
- if(c.getCname() != null && !c.getCname().trim().isEmpty()) {
- byte[] b = null;
- try {
- b = c.getCname().getBytes("iso-8859-1");
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- String name=null;
- try {
- name = new String(b,"utf-8");
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- c.setCname(name);
- }
getUrl是用来获取参数路径并去除&cp参数的:
[java] view plain copy
- private String getUrl(HttpServletRequest request) {
- //String c = request.getContextPath();
- String q = request.getQueryString();
- String s = request.getServletPath();
- if(q != null && !q.trim().isEmpty()) {
- if( q.contains("&cp=")) {
- int i = q.indexOf("&cp=");
- q = q.substring(0, i);
- }
- }
- String url = s + "?" + q;
- return url;
- }
哦了,大致的技术点我们就鸡巴说没了,让我们提升点难度,试试多条件查询:
[java] view plain copy
- //多条件查询的servlet方法
- public String query(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- Customer customer = new Customer();
- try {
- BeanUtils.populate(customer,request.getParameterMap());
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Customer c = encoding(customer);
- PageBean<Customer> pb = new PageBean<Customer>();
- if(request.getParameter("cp") != null && !request.getParameter("cp").trim().isEmpty()) {
- int cp = Integer.parseInt(request.getParameter("cp"));
- pb.setCp(cp);
- }
- else {
- pb.setCp(1);
- }
- pb.setMd(10);
- pb.setPd(service.query(customer,pb.getCd(),pb.getMd()));
- pb.setDt(service.count());
- String url = getUrl(request);
- pb.setUrl(url);
- request.setAttribute("pb",pb);
- return "f:list.jsp";
- }
多条件查询的dao
[java] view plain copy
- public List<Customer> query(Customer customer ,int index,int length) {
- //我们将其参数放入ArrayList中
- List<Object> l = new ArrayList();
- //我们使用StringBuffer连接我们所需要的条件
- StringBuffer sql = new StringBuffer
- ("select * from t_customer where 1=1 ");
- if(customer.getCname() != null && !customer.getCname().trim().isEmpty()) {
- sql.append("and cname=? ");
- l.add(customer.getCname());
- }
- if(customer.getBirthday() != null && !customer.getBirthday().trim().isEmpty()) {
- sql.append("and birthday=? ");
- l.add(customer.getBirthday());
- }
- if(customer.getCellphone() != null && !customer.getCellphone().trim().isEmpty()) {
- sql.append("and cellphone=? ");
- l.add(customer.getCellphone());
- }
- if(customer.getDescription() != null && !customer.getDescription().trim().isEmpty()) {
- sql.append("and description=? ");
- l.add(customer.getDescription());
- }
- if(customer.getEmail() != null && !customer.getEmail().trim().isEmpty()) {
- sql.append("and email=? ");
- l.add(customer.getEmail());
- }
- if(customer.getGender() != null && !customer.getGender().trim().isEmpty()) {
- sql.append("and gender=? ");
- l.add(customer.getGender());
- }
- sql.append("limit ?,?");
- l.add(index);
- l.add(length);
- List<Customer> lc= null;
- try {
- lc = queryRunner.query(sql.toString(), new BeanListHandler<Customer>(Customer.class), l.toArray());
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return lc;
- }