加入pager-taglib.jar
页面上下文:
package com.shwl.cmshop;public class SystemContext {
private static ThreadLocal<Integer> offset = new ThreadLocal<Integer>();
private static ThreadLocal<Integer> pagesize = new ThreadLocal<Integer>();

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();
}

}写一个页面过滤器:
package com.shwl.cmshop.web;import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;import com.shwl.cmshop.SystemContext;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();
}

}

protected int getOffset(HttpServletRequest request){
int offset = 0;
try {
offset = Integer.parseInt(request.getParameter("pager.offset"));
} catch (NumberFormatException ignore) {
}
return offset;
}

protected int getPagesize(HttpServletRequest request){
return 10;
} public void init(FilterConfig arg0) throws ServletException {
}}
下面是hibernate对分页的实现
package com.shwl.cmshop;import java.util.List;import org.hibernate.Query;
import org.hibernate.Session;public class AbstractDAO {
public PagerModel searchPaginated(String hql) throws Exception {
return searchPaginated(hql, null, 0, Integer.MAX_VALUE);
} public PagerModel searchPaginated(String hql, Object param)
throws Exception {
return searchPaginated(hql, new Object[] { param }, 0,
Integer.MAX_VALUE);
} public PagerModel searchPaginated(String hql, Object[] params)
throws Exception {
return searchPaginated(hql, params, 0, Integer.MAX_VALUE);
} public PagerModel searchPaginated(String hql, int offset, int pagesize)
throws Exception {
return searchPaginated(hql, null, offset, pagesize);
} public PagerModel searchPaginated(String hql, Object obj, int offset,
int pagesize) throws Exception {
return searchPaginated(hql, new Object[] { obj }, offset, pagesize);
} /**
 * 根据HQL语句进行分页查询
 * 
 * @param hql
 *            HQL语句
 * @param params
 *            HQL语句带的多个参数值
 * @param offset
 *            从第几条记录开始查询
 * @param pagesize
 *            每页显示多少行
 * @return
 * @throws Exception
 */
public PagerModel searchPaginated(String hql, Object[] params, int offset,
int pagesize) throws Exception {
Session session = HibernateUtils.getSession();
int total = 0;
List datas = null;
// 获取记录总数
String countHql = getCountQuery(hql);
Query query = session.createQuery(countHql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
}
total = ((Long) query.uniqueResult()).intValue(); // 获取当前页的结果集
query = session.createQuery(hql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
} query.setFirstResult(offset);
query.setMaxResults(pagesize);
datas = query.list();
PagerModel pm = new PagerModel();
pm.setTotal(total);
pm.setPages((total + pagesize - 1) / pagesize);
pm.setOffset(offset);
pm.setPagesize(pagesize);
pm.setDatas(datas);
return pm;
} /**
 * 根据HQL语句,获得查找总记录数的HQL语句 如: select ... from Orgnization o where o.parent is
 * null 经过转换,可以得到: select count(*) from Orgnization o where o.parent is null
 * 
 * @param hql
 * @return
 * @throws Exception
 */
private String getCountQuery(String hql) throws Exception {
int index = hql.indexOf("from");
if (index != -1) {
return "select count(*) " + hql.substring(index);
} throw new RuntimeException("无效的HQL查询语句!");
}
}

解决方案 »

  1.   

    我们的分页标签public class PageTag extends BodyTagSupport { private static final long serialVersionUID = 1L; public static final long PAGESIZE = 20; public static final long PAGECAPTION = 4; protected String pageUrl; protected Long pageNo; protected long totalPageCount; protected long currentPageNo; protected long pageSize = PAGESIZE; protected Integer garment = 0; protected String onclick; protected String ulstyle; protected String listyle; public Integer getGarment() {
    return garment;
    } public void setGarment(Integer garment) {
    this.garment = garment;
    } public long getCurrentPageNo() {
    return currentPageNo;
    } public long getPageSize() {
    return pageSize;
    } public void setPageSize(long pageSize) {
    this.pageSize = pageSize;
    } public void setCurrentPageNo(long currentPageNo) {
    this.currentPageNo = currentPageNo;
    } public void setListyle(String listyle) {
    this.listyle = listyle;
    } public void setUlstyle(String ulstyle) {
    this.ulstyle = ulstyle;
    } public Long getPageNo() {
    return pageNo;
    } public void setPageNo(Long pageNo) {
    this.pageNo = pageNo;
    } public String getPageUrl() {
    return pageUrl;
    } public void setPageUrl(String pageUrl) {
    this.pageUrl = pageUrl;
    } public long getTotalPageCount() {
    return totalPageCount;
    } public void setTotalPageCount(long totalPageCount) {
    this.totalPageCount = totalPageCount;
    } @Override
    public int doEndTag() throws JspException { if (getTotalPageCount() <= 1) {
    this.release();
    return super.doEndTag();
    }
    pageNo = getCurrentPageNo();
    // 设定默认值,startPage = 1,
    // endPage = 总页数,
    // pageStep 步长页=3,
    // pageShowSize 总显示页数 = pageStep*2+1;
    long startPage = 1;
    long endPage = getTotalPageCount(); long pageStep = 4;
    long pageShowSize = pageStep * 2 + 1;
    /*
     * System.out.println("****************getTotalPageCount:"+getTotalPageCount());
     * System.out.println("****************pageStep:"+pageStep);
     * System.out.println("****************pageNo:"+pageNo);
     */
    if (getTotalPageCount() > pageShowSize) { startPage = pageNo - pageStep;
    endPage = pageNo + pageStep + 1; // 2.1 当前起始页号小于零
    if (startPage < 1) {
    startPage = 1;
    endPage = pageShowSize;
    } /*
     * System.out.println("****************startPage:"+startPage);
     * System.out.println("****************endPage:"+endPage);
     */
    // 2.2 当前结束页号大于总页数
    if (endPage > getTotalPageCount()) {
    startPage = getTotalPageCount() - pageShowSize;
    endPage = getTotalPageCount();
    } } String contextPath = (String) pageContext.getServletContext()
    .getAttribute("ctx");
    contextPath = "";
    long currentPage = getCurrentPageNo();
    StringBuffer htmlCode = new StringBuffer();
    htmlCode.append("<ul class=\"" + ulstyle + "\">").append(
    "<li><a href=\"#\"></a></li>"); String path = contextPath + this.pageUrl;
    boolean flag = StringUtils.isNotEmpty(onclick); // *************************************************************************************
    if (startPage > 1) {
    htmlCode.append("<a href=\"").append(path);
    if (path.indexOf("?") < 0) {
    htmlCode.append("?");
    }
    htmlCode.append("&pageNo=").append(currentPageNo - 1).append("\"");
    if (null != onclick) {
    htmlCode.append("  onclick=\"").append(onclick).append(
    "; return false;\"");
    }
    htmlCode.append(" >上一页</a>&nbsp;");
    }

    // *************************************************************************************
    if (garment == 0) {
    for (long i = startPage; i <= endPage; i++) {
    htmlCode.append("<li><a href=\"").append(path);
    if (path.indexOf("?") < 0) {
    htmlCode.append("?");
    }
    htmlCode.append("&pageNo=").append(i).append("\" ");
    if (flag)
    htmlCode.append(" onclick = \"javascript: ")
    .append(onclick).append("; return false;\"");
    if (i == currentPage)
    htmlCode.append("class=" + listyle + ""); htmlCode.append(">").append(i).append("</a></li>&nbsp;");
    }
    } // ************************************************************************************* if (pageNo < endPage) {
    if (pageNo == 0)
    pageNo = Long.valueOf(1);
    htmlCode.append("<a href=\"").append(path);
    if (path.indexOf("?") < 0) {
    htmlCode.append("?");
    }
    htmlCode.append("&pageNo=").append(currentPageNo + 1).append("\"");
    if (null != onclick) {
    htmlCode.append("onclick=\"").append(onclick).append(
    "; return false;\"");
    }
    htmlCode.append(">下一页</a>&nbsp;");
    } htmlCode.append("</ul>"); try {
    pageContext.getOut().write(htmlCode.toString());
    } catch (IOException ex) {
    throw new JspException(ex);
    }
    this.release();
    return super.doEndTag();
    } public String getOnclick() {
    return onclick;
    } public void setOnclick(String onclick) {
    this.onclick = onclick;
    }}