1、接口分页类
package com.broadengate.business.service.iface;
import java.util.List;
@SuppressWarnings("rawtypes")
public interface PageClass {
/**
* 初始化分页信息
*/
public abstract void init();
/**
* 以下判断页的信息,只需getter方法(is方法)即可
* @return
*/
public abstract boolean isFirstPage();
public abstract boolean isLastPage();
public abstract boolean isHasPreviousPage();
public abstract boolean isHasNextPage();
public abstract List getList();
public abstract void setList(List list);
public abstract int getTotalPage();
public abstract void setTotalPage(int totalPage);
public abstract int getCurrentPage();
public abstract void setCurrentPage(int currentPage);
public abstract int getPageSize();
public abstract void setPageSize(int pageSize);
public abstract void setFirstPage(boolean isFirstPage);
public abstract void setLastPage(boolean isLastPage);
public abstract void setHasPreviousPage(boolean hasPreviousPage);
public abstract void setHasNextPage(boolean hasNextPage);
public abstract int getAllCount();
public abstract void setAllCount(int allCount);
public abstract int getStartPage();
public abstract void setStartPage(int startPage);
public abstract int getEndPage();
public abstract void setEndPage(int endPage);
public abstract int countTotalPage(final int pageSize,final int allRow);
public abstract int countOffset(final int pageSize,final int currentPage);
public abstract int countCurrentPage(int page);
}
2、实现分页类
package com.broadengate.business.service.impl;
import java.util.List;
import org.springframework.stereotype.Service;
import com.broadengate.business.service.iface.PageClass;
/**
* @ClassName: PageClass
* @Description: 分页类
* @author 邓伟
* @date 2010-11-10 上午13:12:49
*/
@Service
@SuppressWarnings("rawtypes")
public class PageClassImpl implements PageClass {
private List list; //集合
private int allCount;
private int totalPage; //总页数
private int currentPage=0; //当前页
private int pageSize; //每页显示多少条数
private int endPageIndex;
public int getEndPageIndex() {
return endPageIndex;
}
public void setEndPageIndex(int endPageIndex) {
this.endPageIndex = endPageIndex;
}
private boolean isFirstPage; //是否为第一页
private boolean isLastPage; //是否为最后一页
private boolean hasPreviousPage; //是否有上一页
private boolean hasNextPage; //是否有下一页
//2010-12-1 添加 起始行 终止行
private int startPage;//起始行
private int endPage; //终止行
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#init()
*/
public void init() {
this.totalPage = totalPage;
this.isFirstPage = isFirstPage;
this.isLastPage = isLastPage;
this.hasPreviousPage = hasPreviousPage;
this.hasNextPage = hasNextPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#isFirstPage()
*/
public boolean isFirstPage() {
return currentPage==1;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#isLastPage()
*/
public boolean isLastPage() {
return currentPage==totalPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#isHasPreviousPage()
*/
public boolean isHasPreviousPage() {
return currentPage!=1; //只要当前页不是第1页
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#isHasNextPage()
*/
public boolean isHasNextPage() {
return currentPage!=totalPage; //只要当前页不是最后1页
}
/**
* 计算总页数
* ,静态方法,供外部直接通过类名调用
* @param pageSize 每页记录数
* @param allRow 总记录数
* @return 总页数
*/
public int countTotalPage(final int pageSize,final int allRow)
{
int totalPage=allRow%pageSize==0 ? allRow/pageSize :allRow/pageSize+1;
return totalPage;
}
/**
* 计算当前页开始记录
* @param pageSize 每页记录数
* @param currentPage 当前第几页
* @return 当前页开始记录号
*/
public int countOffset(final int pageSize,final int currentPage)
{
final int offset=pageSize*(currentPage-1);
return offset;
}
/**
* 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替
* @param page 传入的参数(可能为空,即0,则返回1)
* @return 当前页
*/
public int countCurrentPage(int page)
{
final int curPage=(page==0?1:page);
return curPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#getList()
*/
public List getList() {
return list;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#setList(java.util.List)
*/
public void setList(List list) {
this.list = list;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#getTotalPage()
*/
public int getTotalPage() {
return totalPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#setTotalPage(int)
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#getCurrentPage()
*/
public int getCurrentPage() {
return currentPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#setCurrentPage(int)
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#getPageSize()
*/
public int getPageSize() {
return pageSize;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#setPageSize(int)
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#setFirstPage(boolean)
*/
public void setFirstPage(boolean isFirstPage) {
this.isFirstPage = isFirstPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#setLastPage(boolean)
*/
public void setLastPage(boolean isLastPage) {
this.isLastPage = isLastPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#setHasPreviousPage(boolean)
*/
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#setHasNextPage(boolean)
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#getAllCount()
*/
public int getAllCount() {
return allCount;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#setAllCount(int)
*/
public void setAllCount(int allCount) {
this.allCount = allCount;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#getStartPage()
*/
public int getStartPage() {
return startPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#setStartPage(int)
*/
public void setStartPage(int startPage) {
this.startPage = startPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#getEndPage()
*/
public int getEndPage() {
return endPage;
}
/* (non-Javadoc)
* @see com.broadengate.business.service.impl.PageClass#setEndPage(int)
*/
public void setEndPage(int endPage) {
this.endPage = endPage;
}
}
3、例子调用方法
(dao或者service类)
public PageClassImpl getPageClassBySearch(int curentPageIndex,int pageSize,Labinfo labinfo,User user,String subjects){
String hql="from Labinfo e where e.user.grade like '%"+user.getGrade()+"%' " +" and e.subject like '%"+subjects+"%'";
String temp="";
if (labinfo!=null) {
if (labinfo.getLabname()!=null&&!"".equals(labinfo.getLabname().trim())) {
temp+=" and e.labname like '%"+labinfo.getLabname()+"%'";
}
if (labinfo.getUser()!=null&&labinfo.getUser().getUserName()!=null&&!"".equals(labinfo.getUser().getUserName().trim())) {
temp+=" and e.user.userName like '%"+labinfo.getUser().getUserName()+"%'";
}
if (labinfo.getTempTime()!=null&&!"".equals(labinfo.getTempTime().trim())) {
temp+=" and e.loadtime <='"+labinfo.getTempTime()+" 23:59:59'";
//temp+=" and e.time ='"+Labinfo.getTempTime()+"'";
}
if (labinfo.getUser()!=null&&labinfo.getUser().getClasses()!=null&&!"".equals(labinfo.getUser().getClasses().trim())) {
temp+=" and e.user.classes like '%"+labinfo.getUser().getClasses()+"%'";
}
}
hql+=temp;
//hql+=" order by e.zhang asc";
String countSql="select count(*) from Labinfo e where e.user.grade like '%"+user.getGrade()+"%' " +
" and e.subject like '%"+subjects+"%'";
countSql+=temp;
Query query=super.getSessionFactory().getCurrentSession().createQuery(countSql);
int count=Integer.valueOf(query.uniqueResult().toString());
PageClassImpl pageClassImpl=new PageClassImpl();
pageClassImpl.setAllCount(count);
pageClassImpl.setCurrentPage(curentPageIndex);
pageClassImpl.setPageSize(pageSize);
int pageCount=pageClassImpl.getAllCount()/pageClassImpl.getPageSize();
if (pageClassImpl.getAllCount()%pageClassImpl.getPageSize()!=0) {
pageCount=pageCount+1;
}
pageClassImpl.setEndPageIndex(pageCount);
query=super.getSessionFactory().getCurrentSession().createQuery(hql);
query.setFirstResult((curentPageIndex-1)*pageSize);
query.setMaxResults(pageSize);
pageClassImpl.setList(query.list());
return pageClassImpl;
}
4、action存放在作用域中
public String toDefaultLab(){
ActionContext.getContext().put("labList", labinfoService.getPageClassBySearch(start, 10, labinfo, user,subjects));
return "labindex";
}
5、jsp页面展示
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="tag" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/My97DatePicker/WdatePicker.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="style/style.css" rel="stylesheet" type="text/css" />
<title></title>
</head>
<body style="padding:5px;">
<tag:if test="${labList.allCount==0 }">
<div style="text-align: center;">没有你要的数据!</div>
</tag:if>
<tag:if test="${labList.allCount!=0 }">
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" class="content_tab">
<tr>
<td align="center" class="table_bg tc" >编号</td>
<td class="table_bg">名称 </td>
<td class="table_bg">章 </td>
<td class="table_bg">节 </td>
<td class="table_bg">上传时间</td>
<td align="center" class="table_bg tc">操作</td>
</tr>
<tag:forEach var="extr" items="${labList.list }">
<tr>
<td align="center" class="fb tc">${extr.id}</td>
<td class="tl">${extr.labname }</td>
<td class="tl">第${extr.chapter}章</td>
<td class="tl">第${extr.section}节</td>
<td align="center" class="tl">
<f:formatDate value="${extr.loadtime}" pattern="yyyy-MM-dd hh:mm:ss"/>
</td>
<td align="center">
<!--学生登陆,用户类型是2,所选择的科目:1是语文,2是数学,3是英语-->
<tag:if test="${sessionScope.session_userinfo.userType!='3' }">
<a href="${pageContext.request.contextPath }/labinfo!toUpdateLab.action?id=${extr.id}">修改</a>
<a href="${pageContext.request.contextPath }/labinfo!doDeleteLab.action?id=${extr.id}" onclick="return confirm('确定要删除吗?')">删除</a>
</tag:if>
<!--<a href="${pageContext.request.contextPath }/labinfo!doDown.action?fnamepath=${extr.filepath}">下载</a>
-->
<a href="${pageContext.request.contextPath }/labinfo!downFile.action?id=${extr.id}">打开</a>
</td>
</tr>
</tag:forEach>
</table>
<div class="scott">
当前第${labList.currentPage }页 总共${labList.allCount }条
<a href="${pageContext.request.contextPath }/labinfo!toDefaultLab.action?start=1">首页</a>
<tag:if test="${labList.currentPage==1 }" var="falg1">
上一页
</tag:if>
<tag:if test="${labList.currentPage!=1 }">
<a href="${pageContext.request.contextPath }/labinfo!toDefaultLab.action?start=${labList.currentPage-1}">上一页</a>
</tag:if>
<tag:if test="${labList.currentPage==extraList.endPageIndex }" var="falg2">
下一页
</tag:if>
<tag:if test="${labList.currentPage!=labList.endPageIndex }">
<a href="${pageContext.request.contextPath }/labinfo!toDefaultLab.action?start=${labList.currentPage+1}">下一页</a>
</tag:if>
<a href="${pageContext.request.contextPath }/labinfo!toDefaultLab.action?start=${labList.endPageIndex}">末页</a>
</div>
</tag:if>
</body>
</html>
注明:例子中与分页无关的代码可以删除!