//1.导入pager-taglib.jar包;
//2.引入pager-taglib标签:
<%@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager" %>
//3.建立PagerModel类:
public class PagerModel {
/**
* 总记录数
*/
private int total;
/**
* 当前页结果集
*/
private List datas;
public List getDatas() {
return datas;
}
public void setDatas(List datas) {
this.datas = datas;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
//4.pager业务逻辑部分:
public class AbstractManager extends HibernateDaoSupport {
public PagerModel searchHql(String hql){
return searchHql(hql,null,SystemContext.getOffset(),SystemContext.getPageSize());
}
public PagerModel searchHql(String hql,Object datas){
return searchHql(hql,new Object[]{datas},SystemContext.getOffset(),SystemContext.getPageSize());
}
public PagerModel searchHql(String hql,Object[] datas){
return searchHql(hql,datas,SystemContext.getOffset(),SystemContext.getPageSize());
}
public PagerModel searchHql(String hql,int offset,int pageSize){
return searchHql(hql,null,offset,pageSize);
}
public PagerModel searchHql(String hql,Object datas,int offset,int pageSize){
return searchHql(hql,new Object[]{datas},offset,pageSize);
}
public PagerModel searchHql(String hql,Object[] datas,int offset,int pageSize){
String countHql = getQuery(hql);
Query query = getSession().createQuery(countHql);
if(datas !=null && datas.length > 0){
for(int i = 0 ; i<datas.length;i++)
query.setParameter(i, datas[i]);
}
int total = ((Long)query.uniqueResult()).intValue();
Query query1=getSession().createQuery(hql);
if(datas !=null && datas.length > 0){
for(int i = 0 ; i<datas.length;i++)
query1.setParameter(i, datas[i]);
}
List list =query1.setFirstResult(offset).setMaxResults(pageSize).list();
PagerModel pm = new PagerModel();
pm.setDatas(list);
pm.setTotal(total);
return pm;
}
public String getQuery(String hql){
int index =hql.indexOf("from");
if(index != -1){
return "select count(*)"+hql.substring(index);
}
throw new SystemException("查询语句出错!");
}
}
//5.定义offset、pagesize类:
public class SystemContext {
private static ThreadLocal offset = new ThreadLocal();
private static ThreadLocal pageSize = new ThreadLocal();
public static int getOffset(){
Integer os = (Integer)offset.get();
if(os == null){
return 0;
}
return os;
}
public static void setOffset(int offsetvalue){
offset.set(offsetvalue);
}
public static void removeOffset(){
offset.remove();
}
public static int getPageSize(){
Integer ps = (Integer)pageSize.get();
if(ps == null){
return Integer.MAX_VALUE;
}
return ps;
}
public static void setPageSize(int pageSizevalue){
pageSize.set(pageSizevalue);
}
public static void removePageSize(){
pageSize.remove();
}
}
//6.过滤器中初始化pagesize、offset:
public class PagerFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest =(HttpServletRequest)request;
SystemContext.setOffset(getOffset(httpRequest));
SystemContext.setPageSize(getPageSize(httpRequest));
try{
chain.doFilter(request, response);
}finally{
//清空ThreadLocal中的值
SystemContext.removeOffset();
SystemContext.removePageSize();
}
}
public void init(FilterConfig arg0) throws ServletException {
}
public int getOffset(HttpServletRequest request){
int offset = 0;
try {
offset = Integer.parseInt(request.getParameter("pager.offset"));
} catch (NumberFormatException ignore) {
}
return offset;
}
//设置每页显示多少条记录
public int getPageSize(HttpServletRequest request){
return 3;
}
}
//7.配置过滤器web.xml中:
<filter>
<filter-name>pager-taglib</filter-name>
<filter-class>com.oa.util.filter.PagerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>pager-taglib</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
//8.显示效果:
<pg:pager url="org.do" items="${pm.total }" export="currentPageNumber=pageNumber">
<pg:param name="parentId"/>
<pg:first>
<a href="${pageUrl}" mce_href="${pageUrl}">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl }" mce_href="${pageUrl }">前页</a>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber }">
<font color="red">${pageNumber }</font>
</c:when>
<c:otherwise>
<a href="${pageUrl }" mce_href="${pageUrl }">${pageNumber }</a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<a href="${pageUrl }" mce_href="${pageUrl }">后页</a>
</pg:next>
<pg:last>
<a href="${pageUrl }" mce_href="${pageUrl }">尾页</a>
</pg:last>
</pg:pager>