页面分页

来源:互联网 发布:数据挖掘导论 pdf 编辑:程序博客网 时间:2024/05/18 02:40

对于页面分页,总的来讲分为两种,一种是前台页面的分页,一种是数据库的分页,下面主要是介绍分页的思想。

前端分页:

所谓前端分页,顾名思义,分页的效果是前端页面显示。这种分页,通常是一次性查出需要展示分页的所有内容,前端页面需要将页码和每页展示条数传递给控制器,控制器通过每页展示条数将查处的集合进行截取,从而达到分页效果。

后端分页,是数据库只查询出当前页需要展示的数据。

sql查询关键字

sqlserver:top     mysql: limit


后台对应工具类

public class PageUtil {private Integer pageId = 1; // 当前页private Integer rowCount = 0; // 总行数private Integer pageSize = 10; // 页大小private Integer pageCount = 0; // 总页数private Integer pageOffset = 0;// 当前页起始记录private Integer pageTail = 0; // 当前页到达的记录private String queryCondition; // 自定义条件private String andCondition; // 条件private String orderByCondition; // 排序private boolean paging = false; // 默认分页private boolean like = false; // 默认模糊查询public boolean getLike() {return like;}public void setLike(boolean like) {this.like = like;}public boolean getPaging() {return paging;}public void setPaging(boolean paging) {this.paging = paging;}public void splitPageInstance() {if (pageSize < 1 || null == pageSize) {pageSize = 10;}// 总页数=(总记录数+每页行数-1)/每页行数pageCount = (rowCount + pageSize - 1) / pageSize;// 当前页大于总页数if (pageId > pageCount) {pageId = pageCount;}// 防止 pageOffset 小于 0pageOffset = ((pageId - 1) * pageSize);if (pageOffset < 0)pageOffset = 0;}public String getLimit() {return " limit " + pageOffset + "," + pageSize;}public String getAndCondition() {return andCondition == null ? "" : " AND " + andCondition;}public String getOrderByCondition() {return orderByCondition == null ? "" : " order by " + orderByCondition;}public String getAllConditionAndLimit() {return getQueryCondition() + getAndCondition() + getOrderByCondition()+ getLimit();}// GET AND SETpublic Integer getPageId() {return pageId;}public String getQueryCondition() {return queryCondition;}public void setQueryCondition(String queryCondition) {this.queryCondition = queryCondition;}public void setAndCondition(String andCondition) {this.andCondition = andCondition;}public void setOrderByCondition(String orderByCondition) {this.orderByCondition = orderByCondition;}public void setPageId(Integer pageId) {this.pageId = pageId;}public Integer getRowCount() {return rowCount;}public void setRowCount(Integer rowCount) {this.rowCount = rowCount;splitPageInstance();}public Integer getPageSize() {return pageSize;}public void setPageSize(Integer pageSize) {this.pageSize = pageSize;}public Integer getPageCount() {return pageCount;}public void setPageCount(Integer pageCount) {this.pageCount = pageCount;}public Integer getPageOffset() {return pageOffset;}public void setPageOffset(Integer pageOffset) {this.pageOffset = pageOffset;}public Integer getPageTail() {return pageTail;}public void setPageTail(Integer pageTail) {this.pageTail = pageTail;}public static void main(String[] args) {PageUtil pageUtil = new PageUtil();pageUtil.setPageId(2);pageUtil.setPageSize(2);pageUtil.setRowCount(10);System.out.println(pageUtil.getLimit());System.out.println(pageUtil.getPageCount());}}

前台显示

<page:createPager pageSize="${pageSize}" totalPage="${totalPage}"totalCount="${totalCount}" curPage="${curPage}" />


<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"version="2.0"><description>Pager</description><tlib-version>1.0</tlib-version><short-name>page</short-name><uri></uri><tag><name>createPager</name><tag-class>com.common.util.tag.Pager</tag-class><body-content>JSP</body-content><attribute><name>curPage</name><required>true</required><rtexprvalue>true</rtexprvalue><type>java.lang.Integer</type></attribute><attribute><name>totalPage</name><required>true</required><rtexprvalue>true</rtexprvalue><type>java.lang.Integer</type></attribute><attribute><name>pageSize</name><required>true</required><rtexprvalue>true</rtexprvalue><type>java.lang.Integer</type></attribute><attribute><name>totalCount</name><required>true</required><rtexprvalue>true</rtexprvalue><type>java.lang.Integer</type></attribute></tag></taglib>


package com.common.util.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.TagSupport;/** * @类说明:页面分页tag实体类 */public class Pager extends TagSupport {private static final long serialVersionUID = -2613705016796991725L;private Integer curPage;private Integer totalPage;private Integer pageSize = 10;// 后续系统参数配置private Integer totalCount = 0;public void setCurPage(Integer curPage) {this.curPage = curPage;}public void setPageSize(Integer pageSize) {this.pageSize = pageSize;}public void setTotalPage(Integer totalPage) {this.totalPage = totalPage;}public Integer getTotalCount() {return totalCount;}public void setTotalCount(Integer totalCount) {this.totalCount = totalCount;}public int doStartTag() throws JspException {JspWriter out = pageContext.getOut();int pageNumber = 0;if (totalPage % pageSize == 0) {pageNumber = totalPage / pageSize;} else {pageNumber = (totalPage / pageSize) + 1;}if (curPage < 1) {curPage = 1;}try {if (pageNumber > 0) {out.print("<div class=\"pagination\"><span class=\"page_total\">共"+totalCount+"条记录</span><ul class=\"page_right\">");int start = 1;int end = totalPage;for (int i = 4; i >= 1; i--) {if ((curPage - i) >= 1) {start = curPage - i;break;}}for (int i = 4; i >= 1; i--) {if ((curPage + i) <= totalPage) {end = curPage + i;break;}}// 如果小于9则右侧补齐if (end - start + 1 <= 9) {Integer padLen = 9 - (end - start + 1);for (int i = padLen; i >= 1; i--) {if ((end + i) <= totalPage) {end = end + i;break;}}}// 如果还小于9左侧补齐if (end - start + 1 <= 9) {Integer padLen = 9 - (end - start + 1);for (int i = padLen; i >= 1; i--) {if ((start - i) >= 1) {start = start - i;break;}}}if (curPage > 1) {if (start >= 1) {out.print("<a href='javascript:goPage(1)' class=\"pagebtn\">首页</a>");}out.print("<a href='javascript:goPage(" + (curPage - 1) + ")' class=\"pagebtn\">上一页</a>");}if (end > start) {out.print("<div class=\"page_num\">");}for (int i = start; i <= end; i++) {if (i == curPage) {if (curPage != end) {out.print("<a href='javascript:void(0);' class='page_cur'>" + i + "</a>");}if (i != end) {out.print("<span>|</span>");} else if (i == end && i != 1) {out.print("<a href='javascript:void(0);' class='page_cur'>" + i + "</a>");}} else {if (i == end) {out.print("<a href='javascript:goPage(" + i + ")'>" + i + "</a>");} else {out.print("<a href='javascript:goPage(" + i + ")'>" + i + "</a>");out.print("<span>|</span>");}}}if (end > start) {out.print("</div>");}if (curPage < totalPage) {out.print("<a href='javascript:goPage(" + (curPage + 1) + ")' class='pagebtn'>下一页</a>");if (end <= totalPage) {out.print("<a href='javascript:goPage(" + totalPage + ")' class='pagebtn'>尾页</a>");}}}} catch (IOException e) {e.printStackTrace();}return super.doStartTag();}public static Integer getStartIndex(Integer pageNum, Integer pageSize) {Integer res = 0;if (pageNum > 0) {res = (pageNum - 1) * pageSize;}return res;}}



原创粉丝点击