適用於Jsp的通用分頁程序(示例代碼)

package ht.util;

import java.util.*;
/**
 * <p>Title: 通用分頁程序</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * 使用方法
 * ArrayList al = (ArrayList)session.getAttribute("sessionname");您的數據集列表
 * AutoPage autopage = new AutoPage(al,5);
 * if(request.getParameter("page")!=null){
 * String tempStr = (String)request.getParameter("page");
 * autopage.setCurrentPage((Integer.parseInt(tempStr)));
 * }else{
 *   autopage.setCurrentPage(1);
 * }
 *
 * autopage.setUrl(你的URL);
 * ArrayList currentPageList = autoPage.getPageList();
 * 輸出currentPageList中內容
 * String ctrlStr = autopage.getPageCtrlString();
 * out.print(ctrlStr);
 * 具體例子
 * ArrayList alist = null;
if(session.getAttribute("planBillList")!=null)
{
  alist = (ArrayList) session.getAttribute("planBillList");
}
 //實例化自動分頁類,第一個參數是要分頁的結果列表(ArrayList或Vector),第二個參數是每頁的行數
AutoPage autopage = new AutoPage(alist,3);
 //設置顯示結果的JSP頁面
autopage.setUrl(GlobalsShare.APPNAME+"jsp/material/commissary/planbill_query_result.jsp");
 //得到當前頁
      String strPage="1";
      if(request.getParameter("page")!=null)
      {
        strPage = request.getParameter("page");
        if(strPage==null)
        {
          strPage="1";
        }
      }
      autopage.setCurrentPage(Integer.parseInt(strPage));
 //得到當前頁的結果集列表
      ArrayList pageList = autopage.getPageList();
 遁環pageList
 //輸出分頁控制條
 out.print(autopage.getPageCtrlString());

 *
 */

public class AutoPage {
  private int pageSize = 10;//每頁大小
  private int nextPage;//下一頁
  private int prePage;//前一頁
  private int pageCount;//總頁數
  private int currentPage;//當前頁
  private int listSize = 0;//記錄總數
  private ArrayList alist = new ArrayList();
  private String url;

  /**
   * 初始化記錄列表
   * @param it Iterator
   * @return ArrayList
   */
  public ArrayList getAlist(Iterator it)
  {
    ArrayList al = new ArrayList();
    while(it.hasNext())
    {
      al.add(it.next());
    }
    return al;
  }
  /**
   * 構造方法
   * @param list Collection
   */
  public AutoPage(Collection list) {
    alist = this.getAlist(list.iterator());
    listSize = alist.size();
    nextPage = 1;
    prePage = 0;
    pageCount = listSize/pageSize+1;
    currentPage = 1;
  }
  /**
   * 構造方法
   * @param list Collection
   * @param pageSize int
   */
  public AutoPage(Collection list,int pageSize) {
    alist = (ArrayList)list;
    this.pageSize = pageSize;
    listSize = alist.size();
    nextPage = 1;
    prePage = 0;
    pageCount = listSize/pageSize;
    if(listSize%pageSize>0)
    {
      pageCount = pageCount + 1;
    }
    currentPage = 1;
  }
  public AutoPage(Vector v,int pageSize)
  {
    for(int i = 0;i<v.size();i++)
    {
      alist.add(v.get(i));
    }
    this.pageSize = pageSize;
    listSize = alist.size();
    nextPage = 1;
    prePage = 0;
    pageCount = listSize/pageSize;
    if(listSize%pageSize>0)
    {
      pageCount = pageCount + 1;
    }
    currentPage = 1;

  }
  /**
   * 下一頁
   * @return ArrayList
   */
  public int nextPage()
  {
    int tempInt = 1;
    if(currentPage<this.getPageCount() && currentPage>=1)
    {
      tempInt =currentPage + 1;
    }
    else
      tempInt = 1;
    return tempInt;
  }
  /**
   * 前一頁
   * @return ArrayList
   */
  public int prePage()
    {
      int tempInt = 1;

    if(currentPage>1)
    {
      tempInt = currentPage -1;
    }else
      tempInt = this.getPageCount();
      return tempInt;
  }
  /**
   * 第一頁
   * @return ArrayList
   */
  public int firstPage()
    {
        nextPage = 1;
        prePage = this.getPageCount();
        currentPage = nextPage;
      return currentPage;
  }
  /**
   * 最後一頁
   * @return ArrayList
   */
  public int endPage()
    {
        nextPage = this.getPageCount();
        prePage = nextPage - 1;
        currentPage = nextPage;
      return currentPage;
  }
  /**
   * 根據當前頁得到記錄列表
   * @param currentPage int
   * @return ArrayList
   */
  public ArrayList getPageList()
  {
     ArrayList tempList = new ArrayList();
    for(int i=(currentPage-1)*pageSize;i<(currentPage-1)*pageSize+pageSize;i++)
      {
        if(i>=0 && i<this.alist.size())
        {
          tempList.add(alist.get(i));
        }else
          break;
      }
     return tempList;
  }
public String getPageCtrlString()
  {
 String connector="?";
 if(this.getUrl().indexOf("?")>0){
  connector="&";
 }
    String strCtrl = "";
    if(this.currentPage == 1){
      strCtrl = "【第一頁】  ";
    }else{
    strCtrl = "【<a href='" + url + connector+"page=1'>第一頁</a>】  ";
    }
    if(this.currentPage == 1){
      strCtrl = strCtrl + "【上一頁】  ";
    }else{
      strCtrl = strCtrl + "【<a href='" + url + connector+"page=" + this.prePage() + "'>上一頁</a>】  ";
    }
    if(this.currentPage == this.pageCount){
      strCtrl = strCtrl + "【下一頁】  ";
    }else{
      strCtrl = strCtrl + "【<a href='" + url + connector+"page=" + this.nextPage() + "'>下一頁</a>】  ";
    }
    if(this.currentPage == this.pageCount){
      strCtrl = strCtrl + "【最後頁】  ";
    }else{
      strCtrl = strCtrl + "【<a href='" + url +connector+ "page=" + this.getPageCount() + "'>最後頁</a>】  ";
    }
    strCtrl = strCtrl + "跳到 <select name='toPage' onChange=/"window.location='" + url + connector+"page='+this.options[this.selectedIndex].value;/">";
    for(int i=1;i<=this.getPageCount();i++)
    {
      if(i==this.getCurrentPage())
      {
        strCtrl = strCtrl + "<option value='"+i+"' selected>第"+i+"頁</option>";
      }else
      {
        strCtrl = strCtrl + "<option value='"+i+"'>第"+i+"頁</option>";
      }

    }
    strCtrl = strCtrl + "</select>";
    strCtrl = strCtrl + " 本頁 第 " + this.getCurrentPage() + " 頁  每頁 " + this.getPageSize() + " 條 共 " + this.listSize + " 條 共 " + this.getPageCount() + " 頁";
    //return strCtrl;
    return "<div style='zIndex:1'>" + strCtrl + "<div>";
  }
  public int getCurrentPage() {
    return currentPage;
  }

  public int getNextPage() {
    return nextPage;
  }

  public int getPageCount() {
    return pageCount;
  }

  public int getPageSize() {
    return pageSize;
  }

  public int getPrePage() {
    return prePage;
  }

  public String getUrl() {
    return url;
  }

  public void setPrePage(int prePage) {
    this.prePage = prePage;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public void setPageCount(int pageCount) {
    this.pageCount = pageCount;
  }

  public void setNextPage(int nextPage) {
    this.nextPage = nextPage;
  }

  public void setCurrentPage(int currentPage) {
    if(currentPage>this.getPageCount())
    {
      this.currentPage=this.getPageCount();
    }else if(currentPage<0)
    {
      this.currentPage=1;
    }else
    {
      this.currentPage = currentPage;
    }

  }

  public void setUrl(String url) {
    this.url = url;
  }
}

 

 

 

 

 

Jsp中使用該分頁程序代碼片段--

 

<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@include file="/inc/checklogin.jsp"%>
<%@page import="ht.util.AutoPage"%>
<%@ page import="java.util.*,java.text.*" %> 
<html>

<head>
  <title>操作票登記維護</title>
  
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  <link href="../../css/style.css" rel="stylesheet" type="text/css">
  <link href="<%=request.getContextPath()%>/css/style.css" rel="stylesheet" type="text/css">
  <script type="text/javascript" src="<%=request.getContextPath()%>/js/ajax.js"></script>
  <script  type="text/javascript" src="<%=request.getContextPath()%>/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
function add(){
        window.location="/scxt/opeRecordsAction.do?method=beforeAdd";
}

function edit(){
        var checkFlag = false;
        var recordId = "";
        var selectFlagArray= document.getElementsByName("selectFlag");
        if(selectFlagArray==undefined){
   alert("沒有要編輯的記錄!");
         return false;
        }
        //alert(document.all.selectFlag.length);
       
        for(var i=0;i<selectFlagArray.length;i++){
            if(selectFlagArray[i].checked){
                recordId = selectFlagArray[i].value;
                checkFlag = true;
            }
        }
       
        if(!checkFlag){
            alert("請選擇要編輯的記錄!");
            return false;
        }else{
                location.href="<%=request.getContextPath()%>/opeRecordsAction.do?method=beforeEdit&recordId="+recordId;
        }
}
function del(){
  var checkFlag = false;
        var recordId = "";
        var selectFlagArray= document.getElementsByName("selectFlag");
        if(selectFlagArray==undefined){
   alert("沒有要刪除的記錄!");
         return false;
        }
        for(var i=0;i<selectFlagArray.length;i++){
            if(selectFlagArray[i].checked){
                recordId = selectFlagArray[i].value;
                checkFlag = true;
            }
        }
       
        if(!checkFlag){
            alert("請選擇要刪除的記錄!");
            return false;
        }else{
         if(confirm("確定要刪除此記錄?")){
          location.href="<%=request.getContextPath()%>/opeRecordsAction.do?method=del&recordId="+recordId;
         }else{
                return false;
         }
        }
}

function view(){
  var checkFlag = false;
        var recordId = "";
        var selectFlagArray= document.getElementsByName("selectFlag");
        if(selectFlagArray==undefined){
   alert("沒有要查看詳情的記錄!");
         return false;
        }
        for(var i=0;i<selectFlagArray.length;i++){
            if(selectFlagArray[i].checked){
                recordId = selectFlagArray[i].value;
                checkFlag = true;
            }
        }
       
        if(!checkFlag){
            alert("請選擇要查看詳情的記錄!");
            return false;
        }else{
         location.href="<%=request.getContextPath()%>/opeRecordsAction.do?method=view&recordId="+recordId;
        }
}

</script>

</head>
<body>
<%@include file="/inc/myconfirm.jsp"%>


<%
Date dd=new Date();
String pattern="yyyy-MM-dd HH:mm:ss";
//當前時間
SimpleDateFormat currentTime=new SimpleDateFormat(pattern);
String currentTimeStr=currentTime.format(dd);
//初始化查詢條件
List hOpFacL = (List)session.getAttribute("hOpFacL");//運行單位列表
List dutyList = (List)session.getAttribute("dutyList");//班次列表
List spciltyList = (List)session.getAttribute("spciltyList");//專業列表
List zbList = (List)session.getAttribute("zbList");//值別列表
 %>
  <TABLE width="100%" border=0 align="center" cellPadding=3 cellSpacing=0>
   <TBODY>
    <TR>
     <TD align="center">
      <SPAN class=bigfont>操作票登記維護</SPAN>
     </TD>
    </TR>
   </TBODY>
  </TABLE>  
  <div align="center" id="div1">
  <form action="<%=request.getContextPath()%>/opeRecordsAction.do?method=query" name="form1" method="post">
  <table width="100%" border="1">
  <tr>
   <td>
   運行單位
   </td>
   <td>
   <select name="factoryId">
   <option value="">請選擇</option>
        <%if(hOpFacL.size()!= 0 ){
         for(int j = 0;j<hOpFacL.size();j++){
           List tempList = (List)hOpFacL.get(j);
        %>
        <option value="<%=(String)tempList.get(0)%>"><%=(String)tempList.get(1)%></option>
     <%
         }
         }
        %>
       </select>
   </td>
   <td>
   專業
   </td>
   <td>
   <select name="spciltyId">
   <option value="">請選擇</option>
        <%if(spciltyList.size()!= 0 ){
         for(int j = 0;j<spciltyList.size();j++){
           List tempList = (List)spciltyList.get(j);
        %>
        <option value="<%=(String)tempList.get(0)%>"><%=(String)tempList.get(1)%></option>
     <%
         }
         }
        %>
       </select>
   </td>
  </tr>
  <tr>
   <td>
   班次
   </td>
   <td>
   <select name="dutyId">
   <option value="">請選擇</option>
        <%if(dutyList.size()!= 0 ){
         for(int j = 0;j<dutyList.size();j++){
           List tempList = (List)dutyList.get(j);
        %>
        <option value="<%=(String)tempList.get(0)%>"><%=(String)tempList.get(1)%></option>
     <%
         }
         }
        %>
       </select>
   </td>
   <td>
   值別
   </td>
   <td>
   <select name="classId">
   <option value="">請選擇</option>
        <%if(zbList.size()!= 0 ){
         for(int j = 0;j<zbList.size();j++){
           List tempList = (List)zbList.get(j);
        %>
        <option value="<%=(String)tempList.get(0)%>"><%=(String)tempList.get(1)%></option>
     <%
         }
         }
        %>
       </select>
   </td>
  <tr>
   <td >
   接令時間
   </td>
   <td colspan="3">
   <input type="text" name="startTime" size="30" readonly οnclick="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" value="<%=currentTimeStr%>">
   至
            <input type="text" name="endTime" size="30" readonly οnclick="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" value="<%=currentTimeStr%>">   
   </td>
  </tr>
  <tr>
   <td colspan="4" align="center">
    <input type="submit" id="buttonSubmit" value="查詢" >
   </td>
  </tr>
  </table>
  </form>
  </div>
  <hr>
  <TABLE width="100%" border=0 align="center" cellPadding=3 cellSpacing=0>
   <TBODY>
    <TR>
     <TD height="41" align="center">
      <SPAN class=bigfont>查詢結果</SPAN>
     </TD>
    </TR>
   </TBODY>
  </TABLE>
  <logic:notEmpty name="opeRecordsList" scope="session">
   <%
    ArrayList alist = null;
    if (session.getAttribute("opeRecordsList") != null) {
     alist = (ArrayList) session.getAttribute("opeRecordsList");
    }

    AutoPage autopage = new AutoPage(alist, 10);
    autopage.setUrl(request.getContextPath()
      + "/jsp/rundata/operecords/opeRecordsManage.jsp");
    String strPage = "1";
    if (request.getParameter("page") != null) {
     strPage = request.getParameter("page");
     if (strPage == null) {
      strPage = "1";
     }
    }
    autopage.setCurrentPage(Integer.parseInt(strPage));
    ArrayList pageList = autopage.getPageList();
    request.setAttribute("pageList", pageList);
   %>
   <TABLE width="100%" border="0" align="center" cellpadding="1"
    cellspacing="1" bgcolor="#666666" id="table_dirttype">
     <TBODY>
    <THEAD>
     <TR class=TableHeader>
      <td width="11%" height="29" align="center" nowrap class=tablehead>
       選擇
      </Td>
      <td width="11%" height="29" align="center" nowrap class=tablehead>
       接票時間
      </Td>
      <td width="9%" align="center" nowrap class=tablehead>
       操作票
      </Td>
      <td width="9%" align="center" nowrap class=tablehead>
       操作票編號
      </Td>
      <td width="11%" height="29" align="center"  class=tablehead>
       原因
      </Td>
     </TR>
    </THEAD>

    <logic:iterate id="opeRecord" name="pageList" scope="request">
     <TR bgcolor="#ffffff"
      onMouseOver="this.style.backgroundColor='#fee5ca'"
      onMouseOut="this.style.backgroundColor='#ffffff'"
      id="tr_<bean:write name='opeRecord' property='opeid'></bean:write>">
      <TD align=center noWrap>
       <input type="radio" id="selectFlag" name="selectFlag" value="<bean:write name='opeRecord' property='opeid'></bean:write>">
      </TD>
      <TD align=center noWrap>
       <bean:write name='opeRecord' property='gettime'></bean:write>
      </TD>
      <TD align=center noWrap>
      <c:forEach var="opeModel" items="${OpeModelList}">
       <c:if test="${opeModel.modelid==opeRecord.modelid}">
       ${opeModel.modelname}
       </c:if>
      </c:forEach>
      </TD>
      <TD align=center noWrap>
       <bean:write name='opeRecord' property='openum'></bean:write>
      </TD> 
      <TD align=center noWrap>
       <bean:write name='opeRecord' property='reason'></bean:write>
      </TD>
      
     </TR>
    </logic:iterate>
   </TABLE>
   <table width="95%" border="0" align="center" cellpadding="0"
    cellspacing="0">
    <tr>
     <td height="25" align="center">
      <%
      out.print(autopage.getPageCtrlString());
      %>

     </td>
    </tr>
   </table>
  </logic:notEmpty>

  <logic:empty name="pageList" scope="request">
   <br>
   <br>
   <br>
   <div align="center" title="提示信息框">
   <span style="BACKGROUND:#fff;COLOR:#666;margin: 10px;border:1px dotted #666;font-weight:bold;padding:8px;width=200">
   無記錄!
   </span>
   </div>
   <br>
   <br>
  </logic:empty>
  <hr>
  <div align="center">
  <input type="button" id="addButton" value="新增" οnclick="javaScript:add();" class="fm">
     <input type="button" id="editButton" value="編輯"  οnclick="javaScript:edit();" class="fm">
     <input type="button" id="delButton" value="刪除"  οnclick="javaScript:del();" class="fm">
     <input type="button" id="viewButton" value="查看詳情"  οnclick="view();" class="fm">
  </div>
<script type="text/javascript">
var opeFlag=${opeFlag};
var firstEntryFlag =${firstEntryFlag};
if(opeFlag.toString()=='false'){
 if(firstEntryFlag.toString()=='true'){
  alert("您不是當班人員,僅能進行查詢操作!");
 }
 document.getElementById("addButton").disabled="true";
 document.getElementById("delButton").disabled="true";
 document.getElementById("editButton").disabled="true";
}
</script>
</body>
</html>

 


發佈了51 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章