学习 JavaWeb项目开发案例精粹14(新闻发布系统)之五

分页处理pagination类
package com.ppcms.common;

import com.ppcms.persistence.*;
import java.io.PrintStream;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;

public class Pagination
{

    public Pagination()
    {
        strPage = null;
    }

    public String strPage(HttpServletRequest request, String page)
    {
        try
        {
            strPage = request.getParameter(page);
        }
        catch(Exception e)
        {
            System.out.println("delcolumn" + e.getMessage());
        }
        return strPage;
    }

    public int curPages(String strPage)
    {
        try
        {
            if(strPage == null)
            {
                curPages = 1;
            } else
            {
                curPages = Integer.parseInt(strPage);
                if(curPages < 1)
                    curPages = 1;
            }
        }
        catch(Exception e)
        {
            System.out.print("curPages");
        }
        return curPages;
    }

    public void setRows(int rows)
    {
        m_rows = rows;
    }

    public int getPages(int rowcounts)
    {
        int test = rowcounts % m_rows;
        if(test == 0)
            pages = rowcounts / m_rows;
        else
            pages = rowcounts / m_rows + 1;
        return pages;
    }

    public Iterator getPageSet(Iterator rs, int curPages)
    {
        int i;
        if(curPages == 1)
            return rs;
        i = 1;
        while(rs.hasNext()) 
        {
            NEWSTable tableNews = (NEWSTable)rs.next();
            i++;
            if(i > (curPages - 1) * m_rows)
                break;
        }
        return rs;
    }

    public Iterator getPageSetNote(Iterator rs, int curPages)
    {
        int i;
        if(curPages == 1)
            return rs;
        i = 1;
        while(rs.hasNext()) 
        {
            NOTEGuest tableNote = (NOTEGuest)rs.next();
            i++;
            if(i > (curPages - 1) * m_rows)
                break;
        }
        return rs;
    }

    public Iterator getPageSetUsr(Iterator rs, int curPages)
    {
        int i;
        if(curPages == 1)
            return rs;
        i = 1;
        while(rs.hasNext()) 
        {
            NEWSUsr tableUsr = (NEWSUsr)rs.next();
            i++;
            if(i > (curPages - 1) * m_rows)
                break;
        }
        return rs;
    }

    private String strPage;  //传入的字页码,为字符串
    private int curPages;  //转换后的当前页码为整型,
    private int m_rows;    //设定的行数。
    private int pages;    //总的页数。
}

public String strPage(HttpServletRequest request, String page)从request中获取page值,返回的值为字符串。

    public int curPages(String strPage)将String类型的转换成int型,也就是传入的当前页码值。

public void setRows(int rows)设置返回的行数

 public int getPages(int rowcounts)获取总页数,由参数总行数除以设置的行数。得到总页数。

  public Iterator getPageSet(Iterator rs, int curPages)
public Iterator getPageSetNote(Iterator rs, int curPages)
public Iterator getPageSetUsr(Iterator rs, int curPages)
这三个是根把传入的当前页数,返回相关的内容。

你可能感兴趣的:(学习 JavaWeb项目开发案例精粹14(新闻发布系统)之五)