package com.
web.utils; import java.io.Serializable; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apachemons.logging.Log; import org.apachemons.logging.LogFactory; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowCallbackHandler;
/** * 分页代码 * * @author Star * @version 1.0 2008/07/08 */ public class CutPage implements Serializable{ private static Log log = LogFactory.getLog(CutPage.class); private int curPageNo = 0; // 当前页数,从 0 开始 private int size = 0; // 所有数据条数 private String url; // 页面跳转的路径 private List showList; // 当前页面需要显示的数据
列表 private int pageSize = 20;// 每页显示的数据条数 private int groupSize = 1;// 多少页为一组 private String pageNavigation;// 导航条 /**
* 每次通过 sql 语句从数据库里面分组取出需要显示的数据 * * @param request * javax.servlet.http.HttpServletRequest 对象 * @param sql * String 查询数据库的 sql 语句 * @param pageSize * int 每页显示的条数 * @param groupSize * int 分成多少组 * @param url * String 页面跳转的路径,若没有特殊的参数传递,可以传入 null 或"", * 如是在 aciton 里面调用,并且 action 是继承自 DispatherAction 的话最 好传入完整的路径 */ public void init(HttpServletRequest request, String sql, int pageSize, int groupSize, int pageNo, String url) { // 上一页、下一页跳转路径 if (url != null) { this.url = url; } else { this.url = request.getRequestURL() + ""; } if (pageSize > 0) this.pageSize = pageSize;// 每页多少条记录 if (groupSize > 0) this.groupSize = groupSize; // 当前第几页 if (pageNo < 0) { this.curPageNo = 0; } else { this.curPageNo = pageNo; } int curGroup = this.curPageNo / this.groupSize + 1; // 是否是新的一组数据,如果是则到数据库取数据 this.size = parseInt(request.getSession().getAttribute("page_all_size") + "", 0); if (this.curPageNo % this.groupSize == 0 || (request.getSession().getAttribute("cur_group") != null &;&; parseInt( "" + request.getSession().getAttribute("cur_group"), 1) != curGroup) || this.size == 0 || request.getParameter("reload") != null) {
request.getSession().setAttribute("cur_group", curGroup); if (pageNo > 0 &;&; request.getSession().getAttribute("page_sql") != null) { sql = request.getSession().getAttribute("page_sql") + ""; } else { request.getSession().setAttribute("page_sql", sql); } this.size = getTotalCount(
sql); List list = getPageData(sql, (this.curPageNo / this.groupSize) * this.pageSize * this.groupSize, this.pageSize * this.groupSize); request.getSession().setAttribute("page_all_size", this.size); request.getSession().setAttribute("page_cur_list", list); this.setShowList(list);// 设置页面上的显示数据 } else { this
.setShowList((List) request.getSession().getAttribute( "page_cur_list"));// 设置页面上的显示数据 } } /** * 每次通过 sql 语句从数据库里面分组取出需要显示的数据 * * @param request * javax.servlet.http.HttpServletRequest 对象 * @param sql * String 查询数据库的 sql 语句 * @param pageSize * int 每页显示的条数 * @param groupSize * int 分成多少组 * @param url * String 页面跳转的路径,若没有特殊的参数传递,可以传入 null 或"", * 如是在 ac