這個(gè)分頁(yè)思路適合于web中的各種分頁(yè)需要,不過(guò)只是最基本的 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;//記錄總數(shù) int page=0; //當(dāng)前要顯示的頁(yè)數(shù) int total_page=0;//頁(yè)面總數(shù) try{ count=ds.readCount(sql_count); } catch(Exception ex) { count=0; } total_page=count/6;//計(jì)算總頁(yè)面 try { rs=ds.read(sql); //讀取所有的記錄集 } catch(Exception e) { rs=null; } try { tmp=(String)request.getParameter("page");//從request中讀取page參數(shù) 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>編號(hào)</td><td height=30>標(biāo)題</td><td height=30>刪除</td></tr>"); //重點(diǎn) 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>首頁(yè)</a>"); out.print("<a href=news_edit.jsp?page="+(page-1)+">上一頁(yè)</a>"); } else { out.print("<tr align=center><td height=30 colspan=3>首頁(yè)"); out.print("上一頁(yè)"); } if(page<total_page) { out.print("<a href=news_edit.jsp?page="+(page+1)+">下一頁(yè)</a>"); out.print("<a href=news_edit.jsp?page="+total_page+">末頁(yè)</a></td></tr>"); } else { out.print("下一頁(yè)"); out.print("末頁(yè)</td></tr>"); } out.print("</table>"); } catch(Exception e) { } return super.doEndTag(); } }
|