最简单的一个分页思路

这个分页思路适合于web中的各种分页需要,不过只是最基本的,贴出来下:

 package com.netshop.tag;

import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.netshop.data.*;

public class shownews extends TagSupport {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    public int i=0;
    @Override
    public int doEndTag() throws JspException {
        // TODO Auto-generated method stub
        JspWriter out=pageContext.getOut();
        HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
        DataStore ds=DataStore.getInstance();
        ResultSet rs=null;
        String sql_count="select count(*) from news";
        String sql="select * from news";
        String tmp=null;
        int count=0;//记录总数
        int page=0; //当前要显示的页数
        int total_page=0;//页面总数
        try{
            count=ds.readCount(sql_count);
        }
        catch(Exception ex)
        {
            count=0;
        }
        total_page=count/6;//计算总页面
        try
        {
            rs=ds.read(sql); //读取所有的记录集
        }
        catch(Exception e)
        {
            rs=null;
        }
        try
        {
          tmp=(String)request.getParameter("page");//从request中读取page参数
           if(tmp==null)
             page=0;
           else
               page=Integer.parseInt(tmp);
        }
        catch(Exception ex1)
        {
            page=0;
        }
        
        //输出记录
        try
        {
            out.print("<table width=600 height=30 border=1 cellpadding=0 bordercolor=#0066FF>");
            out.print("<tr align=center><td height=30>编号</td><td height=30>标题</td><td height=30>删除</td></tr>");
           //重点
            for(i=0;i<page*6;i++)
            {
                rs.next();
            }
            i=page*6;
            int j=0;
            while((rs!=null)&&(rs.next())&&(j<6))
            {
                String newsid=rs.getString(1);
                String newstitle=rs.getString(2);
                newstitle=new String(newstitle.getBytes("ISO-8859-1"),"UTF-8");
                  out.print("<tr align=center><td height=30>"+newsid+"</td>");
                  out.print("<td height=30>"+newstitle+"</td>");
                  out.print("<td height=30><a href=deletenews.jsp?id="+newsid+">删除</a></td></tr>");
                  j++;
            }
           if(page>0)
           {
               out.print("<tr align=center><td height=30 colspan=3><a href=news_edit.jsp?page=0>首页</a>");
               out.print("<a href=news_edit.jsp?page="+(page-1)+">上一页</a>");
           }
           else
           {
               out.print("<tr align=center><td height=30 colspan=3>首页");
               out.print("上一页");
           }
           if(page<total_page)
           {
               out.print("<a href=news_edit.jsp?page="+(page+1)+">下一页</a>");
               out.print("<a href=news_edit.jsp?page="+total_page+">末页</a></td></tr>");
           }
           else
           {
               out.print("下一页");
               out.print("末页</td></tr>");
           }
           out.print("</table>");
        }
        catch(Exception e)
        {
            
        }
        return super.doEndTag();
    }
}

posted on 2009-10-17 18:08  小顾问  阅读(432)  评论(0编辑  收藏  举报