因为现在框架很多都用hibernate,所以写了一个比较通用的hibernate分页的实例
3 page.jsp 需要用到jstl的c标签
1。首先是 Page 类
2. SearchResult 类
- public class Page {
- private int pageSize = 10;
- private int totalCount;
- private int currentPage;
- private int startIndex = 0;
- private int[] indexes = new int[0];
- private int nextIndex;
- private int previousIndex;
- private int pageCount;
- private int lastIndex = 0;
- public Page(int pageSize,
- int startIndex) {
- this.pageSize=pageSize;
- this.startIndex=startIndex;
- }
- public Page(List list, int totalCount) {
- setPageSize(pageSize);
- setTotalCount(totalCount);
- setStartIndex(0);
- }
- public Page(List list, int totalCount, int startIndex) {
- setPageSize(pageSize);
- setTotalCount(totalCount);
- setStartIndex(startIndex);
- }
- public Page(List list, int totalCount, int pageSize,
- int startIndex) {
- setPageSize(pageSize);
- if(totalCount==0)
- {
- setTotalCount(1);
- }else{
- setTotalCount(totalCount);
- }
- setStartIndex(startIndex);
- }
- public void setTotalCount(int totalCount) {
- if (totalCount > 0) {
- this.totalCount = totalCount;
- int count = totalCount / pageSize;
- if (totalCount % pageSize > 0)
- count++;
- indexes = new int[count];
- for (int i = 0; i < count; i++) {
- indexes[i] = pageSize * i;
- }
- } else {
- this.totalCount = 0;
- }
- }
- public int getTotalCount() {
- return totalCount;
- }
- public void setIndexes(int[] indexes) {
- this.indexes = indexes;
- }
- public int[] getIndexes() {
- return indexes;
- }
- public void setStartIndex(int startIndex) {
- if (totalCount <= 0)
- this.startIndex = 0;
- else if (startIndex >= totalCount)
- this.startIndex = indexes[indexes.length - 1];
- else if (startIndex < 0)
- this.startIndex = 0;
- else {
- this.startIndex = indexes[startIndex / pageSize];
- }
- }
- public int getStartIndex() {
- return startIndex;
- }
- public void setNextIndex(int nextIndex) {
- this.nextIndex = nextIndex;
- }
- public int getNextIndex() {
- int nextIndex = getStartIndex() + pageSize;
- if (nextIndex >= totalCount)
- return getStartIndex();
- else
- return nextIndex;
- }
- public void setPreviousIndex(int previousIndex) {
- this.previousIndex = previousIndex;
- }
- public int getPreviousIndex() {
- int previousIndex = this.getStartIndex()-this.pageSize;
- if (previousIndex <=0)
- return 0;
- else
- return previousIndex;
- }
- public void setPageCount(int pageCount) {
- this.pageCount = pageCount;
- }
- public int getPageCount() {
- int count = totalCount / pageSize;
- if (totalCount % pageSize > 0)
- count++;
- return count;
- }
- public int getCurrentPage() {
- if(0== this.getPageCount())
- {
- return 0;
- }else{
- return getStartIndex() / pageSize + 1;
- }
- }
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- public void setLastIndex(int lastIndex) {
- this.lastIndex =lastIndex ;
- }
- public int getLastIndex() {
- if(indexes.length<=0)
- {
- return 0;
- }else{
- return indexes[indexes.length-1];
- }
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- }
- public class SearchResult {
- private List list = new ArrayList();
- private Page page;
- public List getList() {
- return list;
- }
- public void setList(List list) {
- this.list = list;
- }
- public Page getPage() {
- return page;
- }
- public void setPage(Page page) {
- this.page = page;
- }
- }
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <script type="text/javascript">
- function changeRecord(num){
- document.getElementById('startIndex').value=num
- document.forms[0].submit();
- }
- function gopage(lastnum)
- {
- var page=document.getElementById('page').value;
- var num=parseInt(page*10)-parseInt(10);
- //if(num>lastnum)num=lastnum;
- if(num>lastnum)
- {
- alert("错误页数");
- return false;
- }
- if(num<0)num=0;
- document.getElementById('startIndex').value=num
- document.forms[0].submit();
- }
- </script>
- <c:if test="${!empty Page}">
- <table align="right">
- <tr>
- <td>
- 共${Page.totalCount}记录
- <input type="hidden" name="startIndex" id="startIndex" value="0">
- </td>
- <td>
- <c:choose>
- <c:when test="${Page.startIndex ne '0'}">
- <a href="#" onclick="changeRecord('0')">首页</a>
- </c:when>
- <c:otherwise>
- 首页
- </c:otherwise>
- </c:choose>
- </td>
- <td>
- <c:choose>
- <c:when test="${Page.startIndex ne '0'}">
- <a href="#" onclick="changeRecord('${Page.previousIndex}')">上一页</a>
- </c:when>
- <c:otherwise>
- 上一页
- </c:otherwise>
- </c:choose>
- </td>
- <td>
- <c:choose>
- <c:when test="${Page.nextIndex>Page.startIndex}">
- <a href="#" onclick="changeRecord('${Page.nextIndex}')">下一页</a>
- </c:when>
- <c:otherwise>
- 下一页
- </c:otherwise>
- </c:choose>
- </td>
- <td>
- <c:choose>
- <c:when test="${Page.lastIndex eq Page.startIndex}">
- 末页
- </c:when>
- <c:otherwise>
- <a href="#" onclick="changeRecord('${Page.lastIndex}')"> 末页</a>
- </c:otherwise>
- </c:choose>
- </td>
- <td>
- <input type="text" name="page" id="page" size="3" onkeyup="this.value=this.value.replace(/[^/d]/g,'')" >
- <input type="button" value="GO" onclick="gopage('${Page.lastIndex}')">
- 当前第${Page.currentPage}/${Page.pageCount}页
- </td>
- </tr>
- </table>
- </c:if>
用法:
在hibetnate DAO
- public SearchResult findPageByQuery(String hql ,Page page,int totalCount) {
- Query query = getSession().createQuery(hql);
- query.setFirstResult(page.getStartIndex());
- query.setMaxResults(page.getPageSize());
- List list = query.list();
- page.setTotalCount(totalCount);
- SearchResult sr= new SearchResult();
- sr.setList(list);
- sr.setPage(page);
- return sr;
- }
ex:
在action里面
- Page page=null;
- String startIndex = request.getParameter("startIndex");
- if(StringUtils.isNotBlank(startIndex)){
- page=new Page(10,Integer.valueOf(startIndex));
- }
- else{
- page= new Page(10, 0);
- }
- SearchResult sr = null;
- try{
- sr = sysLogService.getAllSysLogs(page);
- }catch(Exception e){
- e.printStackTrace();
- }
- request.setAttribute("searchSysLog", sr.getList());
- request.setAttribute("Page", sr.getPage());
在dao里面
- public SearchResult getAllSysLogs(Page page) {
- String hql = "From Lovesyslog ";
- String numHql = "select count(*) From Lovesyslog";
- SearchResult sr =null;
- try{
- List list = this.getHibernateTemplate().find(numHql);
- Long number = (Long)list.get(0);
- sr = this.findPageByQuery(hql, page, number.intValue());
- }catch (Exception e) {
- e.printStackTrace();
- }
- return sr;
- }