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

 
实现jsp页面得分页显示功能方案
[ 作者: | 来源:| 点击数: 1285]

 

实现jsp页面得分页显示功能方案:
com.cn.page.PageBean.java 用于控制分页的情况和数据的处理的。
com.cn.page.PageView.java 用于在jsp页面中显示表单的。
 
//接口的定义:
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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值