struts开发实践—分页的实现

类别:Java 点击:0 评论:0 推荐:

 struts开发实践-分页的实现

本案主要功能是完成数据集的分页显示。示例代码如下: 

1.PageInfo类:定义分页信息。

/******************program begin**************************/

package test;

import java.io.*; 

public final class PaginationInfo

    implements Serializable {

  /**页大小描述每页有多少行*/

  private int pageSize = 20;

  /**是否有上一页*/

  private boolean hasPrevious;

  /**是否有下一页*/

  private boolean hasNext;

  /**总行数 */

  private int total;

  /**总页数 */

  private int totalPage;

  /**当前页码*/

  private int currentPageNumber=1;

  /**跳转动作:1:首页;2:前一页;3:后一页;4:末页*/

  private int jumpState;

  public void setPageSize(int pageSize) {

    this.pageSize = pageSize;

  }

 

  public int getPageSize() {

    return pageSize;

  } 

  public void setHasPrevious(boolean hasPrevious) {

    this.hasPrevious = hasPrevious;

  } 

  public boolean getHasPrevious() {

    return hasPrevious;

  }

  public void setPreviousPageNumber(int previousPageNumber) {

    this.previousPageNumber = previousPageNumber;

  } 

  public int getPreviousPageNumber() {

    return previousPageNumber;

  } 

  public void setHasNext(boolean hasNext) {

    this.hasNext = hasNext;

  } 

  public boolean getHasNext() {

    return hasNext;

  } 

  public void setNextPageNumber(int nextPageNumber) {

    this.nextPageNumber = nextPageNumber;

  } 

  public int getNextPageNumber() {

    return nextPageNumber;

  } 

  public void setTotal(int total) {

    this.total = total;

  } 

  public int getTotal() {

    return total;

  } 

  public void setTotalPage(int totalPage) {

    this.totalPage=totalPage;

  } 

  public int getTotalPage() {

    return totalPage;

  } 

  public void setCurrentPageNumber(int currentPageNumber) {

    this.currentPageNumber=currentPageNumber;

  } 

  public int getCurrentPageNumber() {

    return currentPageNumber;

  }

  public void setJumpState(int jumpState){

    this.jumpState=jumpState;

  }

  public int getJumpState(){

    return jumpState;

  } 

}

2.分页逻辑方法代码节选:

public void setPageInfo(PaginationInfo paginationInfo) {

    //跳转页

    if (paginationInfo.getJumpState() == 1) { //首页

      paginationInfo.setCurrentPageNumber(1);

    }

    if (paginationInfo.getJumpState() == 2) {

      paginationInfo.setCurrentPageNumber(paginationInfo.getCurrentPageNumber() - 1);

    }

    if (paginationInfo.getJumpState() == 3) {

      paginationInfo.setCurrentPageNumber(paginationInfo.getCurrentPageNumber()+1);

    }

    if (paginationInfo.getJumpState() == 4) {

      paginationInfo.setCurrentPageNumber(paginationInfo.getTotalPage());

    }

    int totalPage = total / paginationInfo.getPageSize();

    if (total % paginationInfo.getPageSize() > 0) {

      paginationInfo.setTotalPage(totalPage + 1);

    }

    else {

      paginationInfo.setTotalPage(totalPage);

    }

    if (paginationInfo.getCurrentPageNumber() <= 1) {

      paginationInfo.setCurrentPageNumber(1);

      paginationInfo.setHasPrevious(false);

      paginationInfo.setHasNext(true);

    }

    else if (paginationInfo.getCurrentPageNumber() >=

             paginationInfo.getTotalPage()) {

      paginationInfo.setCurrentPageNumber(paginationInfo.getTotalPage());

      paginationInfo.setHasNext(false);

      paginationInfo.setHasPrevious(true);

    }

    else {

      paginationInfo.setHasPrevious(true);

      paginationInfo.setHasNext(true);

    }

  }

3。jsp页面的分页显示代码节选:

<logic:equal name="testForm" property="paginationInfo.currentPageNumber" value="1">

   <td align="right" width="30" height="22"><img src="images/pages/distop.gif" alt="首页" border="0"></td>

   <td align="right" width="30" height="22"><img src="images/pages/disprevious.gif" alt="上一页" border="0" ></td>

</logic:equal>

<logic:greaterThan name="testForm" property="paginationInfo.currentPageNumber" value="1">

   <td align="right" width="30" height="22"><a href="javascript:gotoPage(1)"><img src="images/pages/top.gif" alt="首页" border="0" ></a></td>

   <td align="right" width="30" height="22"><a href="javascript:gotoPage(2)"><img src="images/pages/previous.gif" alt="上一页" border="0"></a></td>

</logic:greaterThan>

<logic:equal name="testForm" property="paginationInfo.hasNext" value="false">

      <td align="right" width="30" height="22"><img src="images/pages/disnext.gif" alt="下一页" border="0" ></td>

      <td align="right" width="30" height="22"><img src="images/pages/disbottom.gif" alt="末页" border="0" ></td>

</logic:equal>

<logic:equal name="testForm" property="paginationInfo.hasNext" value="true">

       <td align="right" width="30" height="22"><a href="javascript:gotoPage(3)"><img src="images/pages/next.gif" alt="下一页" border="0" ></a></td>

       <td align="right" width="30" height="22"><a href="javascript:gotoPage(4)"><img src="images/pages/bottom.gif" alt="末页" border="0"></a></td>

</logic:equal>

<html:hidden name="testForm" property="paginationInfo.jumpState">

...//省略

 

 

 

 

本文地址:http://com.8s8s.com/it/it16602.htm