strut中分處理

----------------------分頁類-------------------

package entity;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:何桂坤 註釋導入快捷鍵 Shift+alt+j 時間:2011-1-7
 * 電子郵件:[email protected] QQ:307839227
 * MSN:[email protected]
 */
@SuppressWarnings("unchecked")
public class PageManage {
 // 初始值都爲0,因爲有時候數據爲空
 int currentPage = 1;// 當前頁
 int beforePage = 1;// 上一頁
 int nextPage = 1;// 下一頁
 int totalPages = 0;// 總頁數
 int pagesize = 0;// 每頁顯示的長度
 int allsize = 0;// 數據總長度
 public PageManage() {// 構造方法
 }
 // 構造方法:數據集合 總數據長度,每頁顯示的長度,當以的頁碼
 public PageManage( int allsize, int pagesize, int currentPage) {
  this.allsize = allsize;// 數據總長度
  this.pagesize = pagesize;// 每頁顯示的長度
  this.currentPage = currentPage;// 當前頁
  System.out.println("數據總長度:" + allsize + "/n每頁顯示的長度:" + pagesize
    + "/n當前頁:" + currentPage);
  pm();// 調用方法分頁
  System.out.println("調用分頁bean運行結束!");
 }

 public void pm() {
   double a = allsize;// 設置爲daouble 下面計算總頁才正確
   totalPages = (int) Math.ceil(a/pagesize);// 計算總頁
   if (1<currentPage&&currentPage < totalPages) {// 當前頁<總頁
      this.nextPage = this.currentPage + 1;// 設置下一頁
      this.beforePage = currentPage - 1;// 設置上一頁
       System.out.println("已設置上下一頁");
   } else {
    if(currentPage<=1){
      this.beforePage=1;//異常就默認爲1
      this.nextPage = 2;// 當前頁>最大頁,則把它定爲最後一頁
    }
    if(currentPage>totalPages){
     this.nextPage = totalPages;// 當前頁>最大頁,則把它定爲最後一頁
     this.beforePage=totalPages-1;
       }
    if(currentPage==totalPages){
      this.nextPage = 1;// 當前頁>最大頁,則把它定爲最後一頁
      this.beforePage=totalPages-1;
        }
     }
  } 
 public int getCurrentPage() {
  return currentPage;
 }
 public void setCurrentPage(int currentPage) {
  this.currentPage = currentPage;
 }
 public int getBeforePage() {
  return beforePage;
 }
 public void setBeforePage(int beforePage) {
  this.beforePage = beforePage;
 }
 public int getNextPage() {
  return nextPage;
 }
 public void setNextPage(int nextPage) {
  this.nextPage = nextPage;
 }
 public int getTotalPages() {
  return totalPages;
 }
 public void setTotalPages(int totalPages) {
  this.totalPages = totalPages;
 }
 public int getPagesize() {
  return pagesize;
 }
 public void setPagesize(int pagesize) {
  this.pagesize = pagesize;
 }
 public int getAllsize() {
  return allsize;
 }
 public void setAllsize(int allsize) {
  this.allsize = allsize;
 }
 // 測試
 public static void main(String[] args) {
  List list = new ArrayList();
  list.add("1");
  list.add("1");
  list.add("1");
  list.add("1");
  list.add("1");
  list.add("1");
  PageManage dd = new PageManage( 26, 5, 5);// 共26條,每頁顯示5條,當前第5頁
  System.out.println("上頁" + dd.getBeforePage());
  System.out.println("下頁" + dd.nextPage);
  System.out.println("當前頁" + dd.currentPage);
  System.out.println("總頁數" + dd.totalPages);

 }

}
--------------------------------action---------------------------

public ActionForward ToPxxm(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    String  Page=request.getParameter("page");//取得要查詢的頁碼
    currentpage= Integer.parseInt(Page==null?"1":Page.trim());//等於空就轉爲1頁
    pagesize=5;//每頁顯示15條
       Maxsize=dao.getTotalCount("Psxm");//查詢總共有多少條信息,出入類的字符串形式
   if (currentpage>1) {
    startsize=(currentpage-1)*pagesize;//開始查找的索引=當前頁*沒也顯示的條數
   }else{startsize=0;currentpage=1;}
    List psxmlist=null;
    try {
     System.out.println("====================開始查詢數據================");
   psxmlist = dao.Query("from Psxm", startsize, pagesize);//對象,開始索引,查找長度startsize, pagesize
   
    } catch (Exception e) {
     System.out.println("調用dao.Query(from Psxm, startsize, pagesize)時出錯!");
     e.printStackTrace();
    }
    //總數據長度,本頁顯示的長度,當以的頁碼//
   PageManage pmManage=new PageManage(Maxsize,pagesize,currentpage);//分頁bean
   request.setAttribute("psxmlist", psxmlist);
   request.setAttribute("pmManage", pmManage);//把分頁的bean放到請求中
   System.out.println("==================================返回頁面==============================");
   return mapping.findForward("pxglPxxm");
  }

---------------------------------頁面使用分頁---------------------------------

 <!-- -------------------腳部分頁---------------------------- -->
              <TR style="BACKGROUND-COLOR: #f0f1f0">
                 <td colspan="12" background="${pageContext.request.contextPath }/page/heguikunPxgl/images/dg_head_bg.gif">
                    <A href="http://localhost:8080/HRMS/pxgl.do?operate=ToPxxm&page=1"  id="First" title="第一頁"  style="FONT-SIZE: medium; FONT-FAMILY: webdings"  >7</A>&nbsp;&nbsp;
                    <A href="http://localhost:8080/HRMS/pxgl.do?operate=ToPxxm&page=${pmManage.beforePage}" id="Prev" title="上一頁" style="FONT-SIZE: medium; FONT-FAMILY: webdings"  >3</A>&nbsp;&nbsp;
                    <A href="http://localhost:8080/HRMS/pxgl.do?operate=ToPxxm&page=${pmManage.nextPage}" id="_Next" title="下一頁" style="FONT-SIZE: medium; FONT-FAMILY: webdings"  >4</A>&nbsp;&nbsp;
                    <A  href="http://localhost:8080/HRMS/pxgl.do?operate=ToPxxm&page=${pmManage.totalPages}" id="Last" title="最後一頁"  style="FONT-SIZE: medium; FONT-FAMILY: webdings" >8</A>
                     <B>現在是第</B>
                     <FONT color="#ff0000">${requestScope.pmManage.currentPage}</FONT><B>頁,一共有</B>
                     <FONT  color="#0000ff">${requestScope.pmManage.totalPages}</FONT><B>頁</B>&nbsp;&nbsp;
                     <B>本頁顯示條${requestScope.pmManage.pagesize}記錄&nbsp;&nbsp共有</B>${requestScope.pmManage.allsize}<B>條記錄</B>
                      <B>&nbsp;轉到:<B>
                      <INPUT onKeyUp="CheckNumBer(this,${requestScope.pmManage.totalPages})"; style="WIDTH: 40px"  maxLength="6" value="4"  onafterpaste="CheckNumBer(this,${requestScope.pmManage.totalPages});" id="pageNum">
                     <a onClick="sbpage()">
                      <INPUT style="WIDTH: 54px; HEIGHT: 22px" type="image" src="${pageContext.request.contextPath}/page/heguikunPxgl/images/gotopage.gif" align="bottom" border="0"  />
                      </a>
                      </B></B>
                 </td>
               </TR>
                <!-- --------------------腳部分頁end -------------------------->

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章