主要是帮助大家理解分页功能,脱离了很多分页控件我们如果实现分页效果。
首先 分页我们知道要有上一页 下一页 总页数 当前页 这些信息,那么我们定义分页类如下:
package com.deom.commons;
import java.util.List;
/**
* 分页功能
*
* @author spdai
*
*/
public class Page {
// 总的记录数
private List records;
// 当前页面
private int currentPageNum;
// 总页数
private int totalPage;
// 页的大小
private int pageSize = 10;
// 总的记录条数
private int totalRecords;
// 开始分页的位置
private int startIndex;
// 上一页
private int prePageNum;
// 下一页
private int nextPageNum;
// 开始页码
private int startPage;
// 结束页码
private int endPage;
// 设置跳转页的url
private String uri;
/**
* 初始化页面 只需要 转入当前页 总记录条数
*
* @param currentPageNum
* @param totalRecords
*/
public Page(int currentPageNum, int totalRecords) {
this.currentPageNum = currentPageNum;
this.totalRecords = totalRecords;
this.totalPage = totalRecords % pageSize == 0 ? totalRecords / pageSize
: totalRecords / pageSize + 1;
this.startIndex = (this.currentPageNum - 1) * pageSize;
if (totalPage > 5) {
startPage = currentPageNum - 2;
endPage = currentPageNum + 2;
if (startPage < 1) {
startPage = 1;
endPage = 5;
}
if (endPage > totalPage) {
endPage = totalPage;
startPage = totalPage - 4;
}
} else {
startPage = 1;
endPage = totalPage;
}
}
/**
* @return the records
*/
public List getRecords() {
return records;
}
/**
* @param records
* the records to set
*/
public void setRecords(List records) {
this.records = records;
}
/**
* @return the totalPage
*/
public int getTotalPage() {
return totalPage;
}
/**
* @param totalPage
* the totalPage to set
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
/**
* @return the pageSize
*/
public int getPageSize() {
return pageSize;
}
/**
* @param pageSize
* the pageSize to set
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* @return the totalRecords
*/
public int getTotalRecords() {
return totalRecords;
}
/**
* @param totalRecords
* the totalRecords to set
*/
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
/**
* @return the startIndex
*/
public int getStartIndex() {
return startIndex;
}
/**
* @param startIndex
* the startIndex to set
*/
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
/**
* @return the prePageNum
*/
public int getPrePageNum() {
this.prePageNum = this.currentPageNum - 1;
if (this.prePageNum < 1) {
this.prePageNum = 1;
}
return this.prePageNum;
}
/**
* @param prePageNum
* the prePageNum to set
*/
public void setPrePageNum(int prePageNum) {
this.prePageNum = prePageNum;
}
/**
* @return the nextPageNum
*/
public int getNextPageNum() {
nextPageNum = currentPageNum + 1;
if (nextPageNum > totalPage)
nextPageNum = totalPage;
return nextPageNum;
}
/**
* @param nextPageNum
* the nextPageNum to set
*/
public void setNextPageNum(int nextPageNum) {
this.nextPageNum = nextPageNum;
}
/**
* @return the startPage
*/
public int getStartPage() {
return startPage;
}
/**
* @param startPage
* the startPage to set
*/
public void setStartPage(int startPage) {
this.startPage = startPage;
}
/**
* @return the endPage
*/
public int getEndPage() {
return endPage;
}
/**
* @param endPage
* the endPage to set
*/
public void setEndPage(int endPage) {
this.endPage = endPage;
}
/**
* @return the uri
*/
public String getUri() {
return uri;
}
/**
* @param uri
* the uri to set
*/
public void setUri(String uri) {
this.uri = uri;
}
/**
* @return the currentPageNum
*/
public int getCurrentPageNum() {
return currentPageNum;
}
/**
* @param currentPageNum
* the currentPageNum to set
*/
public void setCurrentPageNum(int currentPageNum) {
this.currentPageNum = currentPageNum;
}
}
dao层调用方法:
@Override
public int getTotalRecords() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
stmt = conn.prepareStatement("select count(*) from customers");
rs = stmt.executeQuery();
if(rs.next()){
return rs.getInt(1);
}
return 0;
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}
@Override
public List findPageRecords(int startIndex, int pageSize) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
stmt = conn.prepareStatement("select * from customers limit ?,?");
stmt.setInt(1, startIndex);
stmt.setInt(2, pageSize);
rs = stmt.executeQuery();
List<Customer> cs = new ArrayList<Customer>();
while(rs.next()){
Customer c = new Customer();
c.setId(rs.getInt("id"));
c.setName(rs.getString("name"));
c.setGender(rs.getString("gender"));
c.setBirthday(rs.getDate("birthday"));
c.setCellphone(rs.getString("cellphone"));
c.setEmail(rs.getString("email"));
c.setHobby(rs.getString("hobby"));
c.setType(rs.getString("type"));
c.setDescription(rs.getString("description"));
cs.add(c);
}
return cs;
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}
jsp页面使用:
第${page.currentPageNum}页/共${page.totalPage}页
<a href="${pageContext.request.contextPath}">首页</a>
<a href="${pageContext.request.contextPath}${page.uri}&num=${page.prePageNum}">上一页</a>
<c:forEach begin="${page.startPage}" end="${page.endPage}" var="num">
<a href="${pageContext.request.contextPath}${page.uri}&num=${num}">${num}</a>
</c:forEach>
<a href="${pageContext.request.contextPath}${page.uri}&num=${page.nextPageNum}">下一页</a>
<a href="${pageContext.request.contextPath}${page.uri}&num=${page.totalPage}">尾页</a>
<select id="pagenum" οnchange="jump(this)">
<c:forEach begin="1" end="${page.totalPage}" var="num">
<option value="${num}" ${num==page.currentPageNum?'selected="selected"':'' }>${num}</option>
</c:forEach>
</select>
<input type="text" id="newnum" size="2"/>
<input type="button" value="跳转" οnclick="jump1()"/>
<script type="text/javascript">
function jump(selectObj){
window.location.href="${pageContext.request.contextPath}${page.uri}&num="+selectObj.value;
}
function jump1(){
var newnum = document.getElementById("newnum").value;
//验证
//验证必须是一个自然整数
if(!/^[1-9][0-9]*$/.test(newnum)){
alert("请输入正确的页码");
return;
}
//范围不能超过1~最大页数
if(newnum<1||newnum>${page.totalPage}){
alert("页码已经超出了范围");
return;
}
window.location.href="${pageContext.request.contextPath}${page.uri}&num="+newnum;
}
</script>
效果如下: