`
allen413
  • 浏览: 95686 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

真分页

阅读更多
Java代码
  1. package  com.pms.util;  
  2.   
  3. import  java.util.List;  
  4.   
  5. /**  
  6.  * Pagination.java  
  7.  * utils class  
  8.  * @author fanfq 2009-6-7  
  9.  *   
  10.  * */   
  11. public   class  Pagination<T> { //这里我使用的范型   
  12.       
  13.     private   int  currentPage =  1 // 当前页数   
  14.     private   int  pageCount =  20 // 每页数据的条数   
  15.     private   int  pageSize;  // 总页数   
  16.     private   int  valueCount;  // 总数据的条数   
  17.     private  List<T> pageList; // 分页集合   
  18.     private   int  previousPageCount; // 上一页的页数   
  19.     private   int  nextPagecount;  // 下一页的页数   
  20.       
  21.     public   void  setCurrentPage( int  currentPage) {  
  22.         this .currentPage = currentPage;  
  23.         // 上一页   
  24.         previousPageCount = currentPage - 1 ;  
  25.         // 下一页   
  26.         nextPagecount = currentPage + 1 ;  
  27.     }  
  28.       
  29.     public   void  setPageList(List<T> pageList) {  
  30.         this .pageList = pageList;  
  31.         pageSize = valueCount % pageCount == 0  ? valueCount / pageCount  
  32.                 : valueCount / pageCount + 1 ;  
  33.     }     
  34.       
  35.     public   int  getCurrentPage() {  
  36.         return  currentPage;  
  37.     }  
  38.       
  39.     public   int  getPageCount() {  
  40.         return  pageCount;  
  41.     }  
  42.     public   void  setPageCount( int  pageCount) {  
  43.         this .pageCount = pageCount;  
  44.     }  
  45.     public   int  getPageSize() {  
  46.         return  pageSize;  
  47.     }  
  48.     public   void  setPageSize( int  pageSize) {  
  49.         this .pageSize = pageSize;  
  50.     }  
  51.     public   int  getValueCount() {  
  52.         return  valueCount;  
  53.     }  
  54.     public   void  setValueCount( int  valueCount) {  
  55.         this .valueCount = valueCount;  
  56.     }  
  57.     public  List<T> getPageList() {  
  58.         return  pageList;  
  59.     }  
  60.       
  61.     public   int  getPreviousPageCount() {  
  62.         return  previousPageCount;  
  63.     }  
  64.     public   void  setPreviousPageCount( int  previousPageCount) {  
  65.         this .previousPageCount = previousPageCount;  
  66.     }  
  67.     public   int  getNextPagecount() {  
  68.         return  nextPagecount;  
  69.     }  
  70.     public   void  setNextPagecount( int  nextPagecount) {  
  71.         this .nextPagecount = nextPagecount;  
  72.     }  
  73.       
  74.       
  75.   
  76. }  
package com.pms.util;

import java.util.List;

/**
 * Pagination.java
 * utils class
 * @author fanfq 2009-6-7
 * 
 * */
public class Pagination<T> {//这里我使用的范型
	
	private int currentPage = 1; // 当前页数
	private int pageCount = 20; // 每页数据的条数
	private int pageSize; // 总页数
	private int valueCount; // 总数据的条数
	private List<T> pageList;// 分页集合
	private int previousPageCount;// 上一页的页数
	private int nextPagecount; // 下一页的页数
	
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
		// 上一页
		previousPageCount = currentPage - 1;
		// 下一页
		nextPagecount = currentPage + 1;
	}
	
	public void setPageList(List<T> pageList) {
		this.pageList = pageList;
		pageSize = valueCount % pageCount == 0 ? valueCount / pageCount
				: valueCount / pageCount + 1;
	}	
	
	public int getCurrentPage() {
		return currentPage;
	}
	
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getValueCount() {
		return valueCount;
	}
	public void setValueCount(int valueCount) {
		this.valueCount = valueCount;
	}
	public List<T> getPageList() {
		return pageList;
	}
	
	public int getPreviousPageCount() {
		return previousPageCount;
	}
	public void setPreviousPageCount(int previousPageCount) {
		this.previousPageCount = previousPageCount;
	}
	public int getNextPagecount() {
		return nextPagecount;
	}
	public void setNextPagecount(int nextPagecount) {
		this.nextPagecount = nextPagecount;
	}
	
	

}

 

Java代码
  1. public   void  doGet(HttpServletRequest request, HttpServletResponse response)  
  2.         throws  ServletException, IOException {  
  3.     doPost(request, response);  
  4.   
  5. }  
  6.   
  7. public   void  doPost(HttpServletRequest request, HttpServletResponse response)  
  8.         throws  ServletException, IOException {  
  9.               
  10.       
  11.     //分页查询操作   
  12.     String currentpage = request.getParameter("pageindex" );   
  13.     if (currentpage ==  null ){   
  14.         currentpage = "1" ;   
  15.     }   
  16.     int  pageindex = Integer.parseInt(currentpage);   
  17.     Pagination<Dept> pc =  new  Pagination<Dept>();  //这里我使用的范型   
  18.     int  count = pc.getPageCount();  
  19.     int  cursor = count * (pageindex- 1 );  
  20.     pc.setValueCount(new  DeptDao().getDeptCount());  
  21.     List<Dept> allList = new  DeptDao().getAllDeptByPagenation(cursor,count); //关键之处定位查询   
  22.     pc.setValueCount(new  DeptDao().getDeptCount());  
  23.     pc.setPageList(allList);   
  24.     pc.setCurrentPage(pageindex);   
  25.     request.setAttribute("pc" , pc);  
  26.     this .getServletContext().getRequestDispatcher( "/page/xx_list.jsp" ).forward(request, response);   
  27.       
  28. }  
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
				
		
		//分页查询操作
		String currentpage = request.getParameter("pageindex"); 
		if(currentpage == null){ 
			currentpage = "1"; 
		} 
		int pageindex = Integer.parseInt(currentpage); 
		Pagination<Dept> pc =  new Pagination<Dept>(); //这里我使用的范型
		int count = pc.getPageCount();
		int cursor = count * (pageindex-1);
		pc.setValueCount(new DeptDao().getDeptCount());
		List<Dept> allList = new DeptDao().getAllDeptByPagenation(cursor,count);//关键之处定位查询
		pc.setValueCount(new DeptDao().getDeptCount());
		pc.setPageList(allList); 
		pc.setCurrentPage(pageindex); 
		request.setAttribute("pc", pc);
		this.getServletContext().getRequestDispatcher("/page/xx_list.jsp").forward(request, response); 
		
	}

 

Java代码
  1. /**定位查询*/   
  2. public  List<Dept> getAllDeptByPagenation( int  cursor, int  rows) {  
  3.     String sql = "SELECT * FROM dept limit "  + cursor +  ","  + rows;  
  4.     ResultSet rs = DBPool.exeQuery(sql);  
  5.     List<Dept> list = new  ArrayList<Dept>();  
  6.     try  {  
  7.         while  (rs.next()) {  
  8.             Dept fDept = new  Dept();  
  9.             fDept.setDeptid(rs.getInt(1 ));  
  10.             fDept.setDeptname(rs.getString(2 ));  
  11.             fDept.setDeptbesc(rs.getString(3 ));  
  12.             fDept.setDeptmanager(rs.getInt(4 ));  
  13.             list.add(fDept);  
  14.         }  
  15.     } catch  (SQLException e) {  
  16.         e.printStackTrace();  
  17.     }  
  18.     DBPool.closeConnection();  
  19.     return  list;  
  20. }  
  21.   
  22. /**获得部门数*/   
  23. public   int  getDeptCount(){  
  24.     String sql = "select count(*) from dept" ;  
  25.     ResultSet rs = DBPool.exeQuery(sql);  
  26.     int  count =  0  ;  
  27.     try  {  
  28.         if (rs.next()){  
  29.             count = rs.getInt(1 );  
  30.         }  
  31.     } catch  (SQLException e) {  
  32.         e.printStackTrace();  
  33.     }  
  34.     DBPool.closeConnection();  
  35.     return  count;  
  36. }  
	/**定位查询*/
	public List<Dept> getAllDeptByPagenation(int cursor,int rows) {
		String sql = "SELECT * FROM dept limit " + cursor + "," + rows;
		ResultSet rs = DBPool.exeQuery(sql);
		List<Dept> list = new ArrayList<Dept>();
		try {
			while (rs.next()) {
				Dept fDept = new Dept();
				fDept.setDeptid(rs.getInt(1));
				fDept.setDeptname(rs.getString(2));
				fDept.setDeptbesc(rs.getString(3));
				fDept.setDeptmanager(rs.getInt(4));
				list.add(fDept);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBPool.closeConnection();
		return list;
	}
	
	/**获得部门数*/
	public int getDeptCount(){
		String sql = "select count(*) from dept";
		ResultSet rs = DBPool.exeQuery(sql);
		int count = 0 ;
		try {
			if(rs.next()){
				count = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBPool.closeConnection();
		return count;
	}

 

Html代码
  1. < %@ page  language = "java"   import = "java.util.*"   pageEncoding = "UTF-8" % >   
  2. < %@ taglib  uri = "http://java.sun.com/jsp/jstl/core"   prefix = "c" % >   
  3.   < body >   
  4.     < div   class = "title" >   
  5.     < h1 > 所有部门信息 </ h1 >   
  6.     < table > < tr > < td >   
  7.         相关操作:  
  8.     </ td > </ tr > </ table >   
  9.     </ div >   
  10.       
  11. < table >   
  12.   < thead >   
  13.     < th   width = "10%" > 部门编号 </ th >   
  14.     < th   width = "20%" > 部门名称 </ th >   
  15.     < th   width = "20%" > 部门概述 </ th >   
  16.     < th   width = "20%" > 部门经理 </ th >   
  17.     < th   width = "10%" > 操作 </ th >   
  18.   </ thead >   
  19.     
  20.   < c:forEach   var = "pc"   items = "${pc.pageList}" >   
  21.   
  22.       
  23.       
  24.     < tr   class = "a1"   align = "center"   onmousemove = "color=this.style.backgroundColor;this.style.backgroundColor='rgb(214,229,249)'"   style = "width: 529px"   onmouseout = "this.style.backgroundColor='white'" >         
  25.      < td > ${pc.deptid} </ td >   
  26.      < td > ${pc.deptname} </ td >   
  27.      < td > ${pc.deptbesc} </ td >   
  28.      < td > < a   href = "#"   target = "" > ${pc.deptmanager} </ a > </ td >   
  29.      < td > < a   target = ""   onClick = "Myopen(User,${pc.deptid})" > 修改 </ a > </ td >   
  30.     </ tr >   
  31.       
  32.         
  33.   </ c:forEach >   
  34.   
  35. </ table >   
  36.   
  37. < div   class = "title" >   
  38.       
  39.     < table > < tr   align = "right" > < td >   
  40.         第${pc.currentPage}/${pc.pageSize}页&nbsp;&nbsp;  
  41.         < a   href = "DeptServlet?pageindex=1" > 首页 </ a >    
  42.         < c:if   test = "${pc.previousPageCount > 0}"   var = "true" >    
  43.         < a   href = "DeptServlet?pageindex=${pc.previousPageCount}" > 上一页 </ a >    
  44.         </ c:if >    
  45.         < c:if   test = "${pc.nextPagecount <= pc.pageSize}"   var = "true" >    
  46.         < a   href = "DeptServlet?pageindex=${pc.nextPagecount}" > 下一页 </ a >    
  47.         </ c:if >    
  48.         < a   href = "DeptServlet?pageindex=${pc.pageSize}" > 尾页 </ a >    
  49.     </ td > </ tr > </ table >   
  50.     < h1 > **fanfq.iteye.com** </ h1 >   
  51. </ div >   
  52.   
  53. </ body >   
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  <body>
    <div class="title">
	<h1>所有部门信息</h1>
	<table><tr><td>
		相关操作:
	</td></tr></table>
	</div>
	
<table>
  <thead>
    <th width="10%">部门编号</th>
    <th width="20%">部门名称</th>
    <th width="20%">部门概述</th>
    <th width="20%">部门经理</th>
    <th width="10%">操作</th>
  </thead>
  
  <c:forEach var="pc" items="${pc.pageList}">

	
	
	<tr class="a1" align="center" onmousemove="color=this.style.backgroundColor;this.style.backgroundColor='rgb(214,229,249)'" style="width: 529px" onmouseout="this.style.backgroundColor='white'">	  	
	 <td>${pc.deptid}</td>
	 <td>${pc.deptname}</td>
	 <td>${pc.deptbesc}</td>
	 <td><a href="#" target="">${pc.deptmanager}</a></td>
	 <td><a target="" onClick="Myopen(User,${pc.deptid})">修改</a></td>
	</tr>
	
	  
  </c:forEach>

</table>

<div class="title">
	
	<table><tr align="right"><td>
		第${pc.currentPage}/${pc.pageSize}页&nbsp;&nbsp;
		<a href="DeptServlet?pageindex=1">首页</a> 
		<c:if test="${pc.previousPageCount > 0}" var="true"> 
		<a href="DeptServlet?pageindex=${pc.previousPageCount}">上一页</a> 
		</c:if> 
		<c:if test="${pc.nextPagecount <= pc.pageSize}" var="true"> 
		<a href="DeptServlet?pageindex=${pc.nextPagecount}">下一页</a> 
		</c:if> 
		<a href="DeptServlet?pageindex=${pc.pageSize}">尾页</a> 
	</td></tr></table>
	<h1>**fanfq.iteye.com**</h1>
</div>

</body>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics