//import 分页类包
private void savePageNum(AppContext ac)
{
//取得列表的当前页码
sPageNum = (String)ac.getRequest().getParameter("pageNum");
//从session取页码
if(sPageNum == null) {
this.sPageNum = (String)ac.getRequest().getSession().getAttribute("page");
}
else { //用session记录页码
ac.getRequest().getSession().setAttribute("page", sPageNum);
}
try {
this.iPageNum = Integer.parseInt(sPageNum);
}
catch(NumberFormatException nfe) {
this.iPageNum = 1;
}
}//此方法必须调用,用来记下前端几页的信息
//以下在具体的类中的使用
ps = new PageSpliterBean(this.iPageNum, this.PAGE_NUM, lstQuery.size(), lstQuery);//传入参数看构造函数
request.getAttribute("currentList",ps.getCurrentPageList());//得到单页LIST
request.getAttribute("pageSplisterHtml",ps.getPageSpliterHtml());//得到客户端脚本
<script language="javascript" type="text/JavaScript">
//页面跳转
function gotoPage(pagenum)
{
document.forms[0].pageNum.value = pagenum;
document.forms[0].submit();
return ;
}
//跳转
function Jumping()
{
document.forms[0].submit();
return ;
}
</script>
<!--在HTML中调用-->
<bean:write name="pageSplisterHtml" filter="false"/>
package com.newland.util;
import java.util.*;
/**
* <p>Title:分页工具条生成类</p>
* <p>Description:实现从后台取出分页程序的各个参数</p>
* <P>HISTORY
* Create Time:2005-05-24
* Modify :
* </P>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company:NewLand</p>
* @version 1.0
* @author MaoKing.Lao
*/
public class PageSpliterBean
{
private int curPage; //当前是第几页
private int maxPage; //一共有多少页
private int maxRowCount; //一共有多少行
private int rowsPerPage; //每页多少行 在controller中设置
private int iBeginRow = 0; //显示列表起始行位置
private int iEndRow = 0; //显示列表结束行位置
private String pageSpliterHtml; //生成客户端分页脚本
private List currentPageList; //当前List
private List sourceList; //所有记录集list
//
public PageSpliterBean()
{
}
/**
* 构造函数
* @param iPageNum int 当前页码
* @param iRowsPerPager int 每页几条记录
* @param iMaxRowCount int 共有多少条记录
* @param sourceList List 源List
*/
public PageSpliterBean(int iPageNum, int iRowsPerPager, int iMaxRowCount, List sourceList)
{
if(iPageNum == 0) {
iPageNum = 1;
}
if(iRowsPerPager == 0) {
iRowsPerPager = Integer.MAX_VALUE;
}
this.curPage = iPageNum;
this.rowsPerPage = iRowsPerPager;
this.maxRowCount = iMaxRowCount;
if(this.rowsPerPage != 0) {
if(iMaxRowCount % this.rowsPerPage == 0) {
this.maxPage = iMaxRowCount / this.rowsPerPage;
}
else {
this.maxPage = (iMaxRowCount / this.rowsPerPage + 1);
}
}
if(iPageNum > maxPage) {
this.curPage = maxPage; //如果pageNum参数大于实现页数,则取实际页数.
}
this.resetBeginEnd();
this.sourceList = sourceList;
}
/**
* 重新设置起始位置
*/
private void resetBeginEnd()
{
iBeginRow = (this.curPage - 1) * this.rowsPerPage;
iEndRow = iBeginRow + this.rowsPerPage;
}
/**
* 取出当前页的List
* @return List
*/
public List getCurrentPageList()
{
if(sourceList.size() < this.iEndRow) {
iEndRow = sourceList.size();
}
this.currentPageList = this.interceptList(sourceList, iBeginRow, iEndRow);
return currentPageList;
}
/**
* 根据起始/结束位置截取数组列表
* @param src 源数组列表
* @param iBegin 起始位置
* @param iEnd 结事位置
* @return 截取后的数组列表
*/
private List interceptList(List src, int iBegin, int iEnd)
{
ArrayList dest;
int destSize;
destSize = iEnd - iBegin;
if( (iBegin >= iEnd) || (iEnd == 0)) {
return null;
}
dest = new ArrayList(Arrays.asList(new Object[destSize]));
if(destSize > src.size()) {
throw new IndexOutOfBoundsException("源数组size小于目标数组size.");
}
for(int i = iBegin, j = 0; i < iEnd; j++, i++) {
dest.set(j, src.get(i));
}
return dest;
}
/**
* 生成分页的客户端脚本
* @return 客户端分页条脚本
*/
public String getPageSpliterHtml()
{
StringBuffer retBuffer = new StringBuffer(512);
retBuffer.append("<table border=0 cellpadding=/"0/" cellspacing=/"0/">/n<tr>/n<td width=/"50%/"
nowrap=/"nowrap/" class=/"table_line/" align=/"left/" /></tr></table>");
retBuffer.append("<table border=0 width=/"95%/" align=/"center/" cellpadding=/"0/" cellspacing=/"0
/">/n<tr>/n<td width=/"50%/" nowrap=/"nowrap/" class=/"list_page/" align=/"left/">");
if( (this.rowsPerPage == Integer.MAX_VALUE) || (this.rowsPerPage == 0) ||
(this.maxRowCount == 0)) {
retBuffer.append("共有<font color=/"red/">" + this.maxRowCount + "</font>条记录 ");
}
else {
retBuffer.append("共有<font color=/"red/">" + this.maxRowCount + "</font>条记录 ");
retBuffer.append(" 当前是第<u><font color=/"red/">" + this.curPage + "</font></u>页");
retBuffer.append("/共有<u><font color=/"red/">" + this.maxPage + "</font></u>页 ");
retBuffer.append("(每页显示<u><font color=/"red/">" + this.rowsPerPage + "</font></u>条记录) ");
retBuffer.append(" </td>/n");
//第一页时处理
if(this.curPage == 1) {
retBuffer.append(
"<td nowrap=/"nowrap/" class=/"list_page/" align=/"right/">首页 上一页/n");
}
else {
retBuffer.append("<td nowrap=/"nowrap/" class=/"list_page/" align=/"right/"><A
HREF=/"javascript:gotoPage(1)/">首页</A> ");
retBuffer.append("<A HREF=/"javascript:gotoPage(" + (this.curPage - 1) +
")/">上一页</A> /n");
}
//最后一页时处理
if(this.curPage == this.maxPage) {
retBuffer.append(" 下一页 尾页 /n");
}
else {
retBuffer.append("<A HREF=/"javascript:gotoPage(" + (this.curPage + 1) +
")/">下一页</A> ");
retBuffer.append("<A HREF=/"javascript:gotoPage(" + this.maxPage + ")/">尾页</A> /n");
}
retBuffer.append("跳转到第/n");
//下拉选择框
retBuffer.append("<SELECT name=/"pageNum/" οnchange=/"Jumping()/">/n");
for(int i = 1; i <= this.maxPage; i++) {
if(i == this.curPage) {
retBuffer.append("<option selected value=/"" + i + "/">" + i + "</option>/n");
}
else {
retBuffer.append("<option value=/"" + i + "/">" + i + "</option>/n");
}
}
retBuffer.append("</select>页/n");
retBuffer.append("<input name='Where' type='hidden' value=''>/n");
}
retBuffer.append("</td></tr>/n</table>/n");
retBuffer.append("/n");
return retBuffer.toString();
}
//测试
public static void main(String[] args)
{
PageSpliterBean ps = new PageSpliterBean(1, 10, 98, null);
System.out.print(ps.getPageSpliterHtml());
}
}