-
package
com.pms.util;
-
-
import
java.util.List;
-
-
-
-
-
-
-
-
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;
-
}
-
-
-
-
}
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;
}
}
-
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);
-
-
}
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);
}
-
-
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;
-
}
/**定位查询*/
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;
}
-
<
%@ 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}页
-
<
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
>
<%@ 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}页
<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>