实现jsp页面得分页显示功能方案

发表于:2007-06-22来源:作者:点击数: 标签:
//接口的定义: public interface ContactBean { public int getAvailableCount()throws Exception; public int getAvailableCount(int id)throws Exception; public PageBean listData(String page)throws Exception ; public PageBean listData(String pag

   
  //接口的定义:
  
  public interface ContactBean {
  
   public int getAvailableCount()throws Exception;
  
   public int getAvailableCount(int id)throws Exception;
  

   public PageBean listData(String page)throws Exception ;
  
   public PageBean listData(String page,int id)throws Exception ;
  
   public Collection getResult()throws Exception;
  
  }
  
  //pageBean的定义:
  
  public class PageBean {
  
   public int curPage;//当前的页值。
  
   public int maxPage;//最大的页数。
  
   public int maxRowCount;//最大的行数。
  
   public static int rowsPerPage=10; //控制分页的行数。
  
   public java.util.Collection data; //所要显示的数据的集合。
  
   public PageBean() {
  
   }
  
   public void countMaxPage(){
  
    if(this.maxRowCount%this.rowsPerPage==0){
  
     this.maxPage=this.maxRowCount/this.rowsPerPage;
  
    }else{
  
     this.maxPage=this.maxRowCount/this.rowsPerPage+1;
  
    }
  
   }
  
   public Collection getResult(){
  
    return this.data;
  
   }
  
   public PageBean(ContactAdapter bean,int id)throws Exception{
  
    this.maxRowCount=bean.getAvailableCount(id);
  
    if(maxRowCount==0)
  
     maxRowCount=1;
  
    System.out.println(maxRowCount);
  
    this.data=bean.getResult();
  
    this.countMaxPage();
  
   }
  
   public PageBean(ContactAdapter bean)throws Exception{
  
    this.maxRowCount=bean.getAvailableCount();
  
    this.data=bean.getResult();
  
    this.countMaxPage();
  
   }
  
  }
  
  //对ContactBean接口进行adapter模式的封装。方便实用。
  
  public class ContactAdapter implements ContactBean{
  
   public ContactAdapter() { }
  
   public int getAvailableCount()throws Exception{
  
    return 0;
  
   }
  
   public int getAvailableCount(int id)throws Exception{
  
    return 0;
  
   }
  
   public PageBean listData(String page)throws Exception {
  
    return null;
  
   }
  
   public PageBean listData(String page,int id)throws Exception {
  
    return null;
  
   }
  
   public Collection getResult()throws Exception{
  
    return null;
  
   }
  
  }
  
  在使用中你可以实现com.cn.interfaces.ContactBean 或继承 com.cn.interfaces.ContactAdapter都可以。
  
  实现方法:
  
                实现一:其中不需要键入任何参数。
  
  public int getAvailableCount()throws Exception;
  
                 public PageBean listData(String page)throws Exception ;
  
                 public Collection getResult()throws Exception;
  
                实现二:需要键入参数。
  
  public int getAvailableCount(int id)throws Exception;
  
                 public PageBean listData(String page,int id)throws Exception ;
  
  public Collection getResult()throws Exception;
  
  例如:
  
  Public class TestContact extends ContactAdapter{
  
      Private Collection data;
  
      Public int getAvailableCount(int unitID) throws Exception{       
  
         BSIResultSet rs = new DBUtil().getResult(“
  
  select count(*) NUM from TableTest where unit_id ="+id);
  
         return rs.getInt(NUM);
  
  }
  
  public PageBean listData(String page,int incept_sort_id)throws Exception {
  
    PageBean p = new PageBean(this,incept_sort_id);
  
    int pageNum = Integer.parseInt(page);
  
    BSIResultSet rs = con.getResult(
  
  sql, ( pageNum -1)* p.rowsPerPage,, pageNum*p.rowsPerPage );
  
    p.curPage = pageNum;
  
    p.data = this.getTestBean(rs);
  
  //此方法是把查询的数据封装在一个collection中。
  
    return p;
  
  }
  
  public Collection getResult()throws Exception{
  
      return this.data;
  
  }
  
  }
  
  其中testBean是封装好的数据bean。
  
  如果没有参数的话。可以使用没有参数的一组接口。
  
  例如:在jsp中如何是用借口。
  
      <jsp:useBean id="pageCtl" class="com.cn.page.PageBean" scope="request"/>
  
    <%  
  
  Vector c=(Vector)pageCtl.getResult();
  
        Iterator it=c.iterator();
  
        while(it.hasNext()){
  
          TestBean d=(TestBean)it.next();//的到每一个单独的数据。
  
          //可以取出此bean的属性值。
  
  %>
  
      。。。。。//属性使用。业务逻辑。
  
      <form action="test.do" method="post" name="PageForm">
  
      <%out.print(ViewPage.getView(pageCtl));%>
  
  </form>
  
  //显示分页的表单。
  
  加入javaScript的page.js.在jsp页面上加入:
  
    <script language="JavaScript" src="js/page.js">即可。
  
  function jumping(){
  
      document.PageForm.submit();
  
      return;
  
  }
  
  function gotoPage(Pagenum){
  
      document.PageForm.jumpPage.value=Pagenum;
  
      document.PageForm.submit();
  
      return;

原文转自:http://www.ltesting.net