简单分页类 [转]

public abstract class PagingPage extends BasePage implements PageRenderListener
{
    /*****************************************************
     *分页功能
     *****************************************************/
    
    protected int getPagesize()
    {
        return 12;
    }
    
    /**数据源设置
     * @return 数据源
     */
    public abstract ArrayList iniPage();
    
    public abstract void setCurrentpage(String value);
    public abstract void setPagecount(String value);

    public abstract void setCmd(String cmd);
    public abstract String getCmd();
    
    public abstract void setPageList(List list);
    public abstract List getPageList();
  
    public abstract void setPageNo(String pageno);
    public abstract String getPageNo();
    
    private int getPageCount()
    {
        int pagesize = getPagesize();
        ArrayList temp = iniPage();
        if(temp!=null && temp.size()!=0)
     {
     int count = temp.size();
     int pagecount = count/pagesize;
     if(count > pagesize*pagecount)
     {
     pagecount++;
     }
     return pagecount;
     }
        else
        {
            return 0;
        }
    }

    /** 初始化绑定表格
     *count:记录数
     *pagecount:分页数
     *currentpage:当前页码
     */
    public void bind(int page)
    {
//     设置数据源
     List list = new ArrayList();
     ArrayList temp = iniPage();
    
     int pagecount = getPageCount();
     int currentpage = page;
     int pagesize = getPagesize();

     setPageNo(Integer.toString(page));
     //绑定到表格
     if(temp!=null && temp.size()!=0)
     {
     int count = temp.size();
     
     setCurrentpage(Integer.toString(currentpage));
    
     setPagecount(Integer.toString(pagecount));  
    
     if(count<=pagesize*page)
     {
     for(int i=pagesize*(page-1);i<count;i++)
     {
     list.add(temp.get(i));
     }
     }
     else
     {
     for(int i=pagesize*(page-1);i<pagesize*page;i++)
     {
     list.add(temp.get(i));
     }
     }
     }
        else
        {
            setCurrentpage(null);
            setPagecount(null);
        }
     setPageList(list);
    }
    
    /**
     * 页面呈现
     */
    public void pageBeginRender(PageEvent event)
    {
     if(getCmd()!=null)
        {
         int cmd =0;
         cmd = Integer.parseInt(getCmd());
         int pagecount = getPageCount();
         int pageno = Integer.parseInt(getPageNo());
         switch(cmd)
{
             case 1:
                 if(pageno!=1)
                 {
                     pageno--;
                 }
              bind(pageno);
              break;
         case 2:
             if(pageno!=pagecount)
             {
                 pageno++;
             }
         bind(pageno);
         break;
         case 3:
         bind(1);
             break;
         case 4:
         bind(pagecount);
              break;
         default:
         bind(1);
}
        }
     else
     {
     bind(1);
     }
    }
}

zhanghui代无忌补充:<tr>
        <td height="23" colspan="14" class="infoblock"><div align="right"> 第<span jwcid="@Insert" value="ognl:currentpage">1</span>页 共<span jwcid="@Insert" value="ognlagecount">10</span>页 <a jwcid="first@LinkSubmit" selected="ognl:cmd" tag="ognl:3">首页</a> <a jwcid="pre@LinkSubmit" selected="ognl:cmd" tag="ognl:1">上一页</a> <a jwcid="next@LinkSubmit" selected="ognl:cmd" tag="ognl:2">下一页</a> <a jwcid="last@LinkSubmit" selected="ognl:cmd" tag="ognl:4">尾页</a> </div></td>
     </tr>

你可能感兴趣的:(分页)