這是我們項目中的分頁代碼,是用JSTL + 自訂標籤檔案 + JAVABEAN實現的,或許對你有所協助:
一、自訂標籤檔案
1、iterator.tag
<!-- 標準標籤和自訂標籤聲明 -->
<!-- 支援未聲明的屬性 -->
<%@ tag dynamic-attributes="attributes"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!-- 聲明屬性值 -->
<!-- data 待處理的資料,為List類型 -->
<!-- rows 每頁顯示的行數 -->
<!-- page 當前頁數 -->
<!-- isPagination 是否顯示分頁資訊 -->
<!-- trBgColor 表的背景色 -->
<!-- info_list JSP動作屬性調用值 -->
<!-- var 資料傳遞屬性 -->
<!-- current 資料傳遞屬性固定名稱即別名 -->
<%@ attribute name="data" required="true" type="java.util.List"%>
<%@ attribute name="rows"%>
<%@ attribute name="page"%>
<%@ attribute name="isPagination"%>
<%@ attribute name="trBgColor"%>
<%@ attribute name="info_list" fragment="true" required="true"%>
<%@ attribute name="var" rtexprvalue="false" required="true"%>
<%@ variable name-from-attribute="var" alias="current" variable-class="java.lang.Object" scope="NESTED" %>
<!-- 設定預設值 -->
<!-- rows=10 即預設每頁顯示10行 -->
<!-- page=1 即預設顯示第一頁 -->
<!-- trBgColor=white 即一行表格預設底色為無色 -->
<c:set var="rows" value="${empty rows ? 10 : rows}"/>
<c:set var="page" value="${empty page ? 1 : page}"/>
<c:set var="trBgColor" value="${empty trBgColor ? '' : trBgColor}"/>
<!-- 初始化分頁類相關資料 -->
<!-- data 待處理的資料 -->
<!-- perpageRows 每頁顯示的行數 -->
<!-- currentPage 當前頁 -->
<c:if test="${! empty isPagination}">
<jsp:useBean id="pagination" class="com.winmess.japanese.util.Pagination"/>
<c:set target="${pagination}" property="data" value="${data}"/>
<c:set target="${pagination}" property="perpageRows" value="${rows}"/>
<c:set target="${pagination}" property="currentPage" value="${page}"/>
</c:if>
<c:set var="beginRow" value="${empty pagination.currentRow ? 0 : pagination.currentRow}"/>
<c:set var="endRow" value="${empty pagination.lastRow ? (rows - 1) : pagination.lastRow}"/>
<!-- 顯示內容 -->
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td>
<table
<c:forEach items="${attributes}" var="a">
${a.key}="${a.value}"
</c:forEach>
>
<form name="infolistform" method="post" action="">
<!-- 資訊迭代 -->
<c:forEach items="${data}" var="current" begin="${beginRow}" end="${endRow}">
<tr bgcolor="${trBgColor}">
<!-- 動作顯示(即JSP頁面出現的相關動作將在這裡顯示出來) -->
<jsp:invoke fragment="info_list"/>
</tr>
</c:forEach>
</form>
</table>
</td>
</tr>
<!-- 分頁顯示資訊 -->
<c:if test="${! empty isPagination}">
<tr><td>
<table width="100%" height="80%">
<form name="turnPageForm" method="post" action="">
<tr>
<jsp:useBean id="prompt" class="com.winmess.japanese.util.PaginationPrompt"/>
<td width="50%">
${prompt.total}${pagination.totalRows}${prompt.record}
${pagination.currentPage}/${pagination.totalPages}${prompt.pagePrompt}
</td>
<td width="50%" align="right">
<a href="<c:url value="${requestURL}?page=1&className=${className}&sort=${sort}"/>">${prompt.firstPage}</a>
<a href="<c:url value="${requestURL}?page=${pagination.currentPage - 1}&className=${className}&sort=${sort}"/>">${prompt.prePage}</a>
<a href="<c:url value="${requestURL}?page=${pagination.currentPage + 1}&className=${className}&sort=${sort}"/>">${prompt.nextPage}</a>
<a href="<c:url value="${requestURL}?page=${pagination.totalPages}&className=${className}&sort=${sort}"/>">${prompt.lastPage}</a>
<!-- 下拉顯示頁數 -->
<select name="jumpPage" size="1" onChange="javascript:location='${requestURL}?page='+this.options[this.selectedIndex].value+'&className=${className}&sort=${sort}';">
<c:forEach begin="1" end="${pagination.totalPages}" var="i">
<option value = "${i}" ${i == pagination.currentPage ? "selected" : ""}>
${i}
</option>
</c:forEach>
</select>${prompt.page}
</td>
</tr>
</form>
</table>
</td>
</tr>
</c:if>
</table>
2、param.tag
<!-- 標準標籤和自訂標籤聲明 -->
<!-- 支援未聲明的屬性 -->
<%@ tag dynamic-attributes="attributes"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- 聲明屬性值 -->
<!-- name 待處理的資料 -->
<!-- name1 待處理的資料2 -->
<!-- length 每條資訊顯示的長度 -->
<!-- url 連結到什麼頁面 -->
<!-- align 表格居什麼顯示 -->
<!-- img 載入圖片地址 -->
<!-- checkout 載入多選框 -->
<%@ attribute name="name"%>
<%@ attribute name="name1"%>
<%@ attribute name="length"%>
<%@ attribute name="url"%>
<%@ attribute name="align"%>
<%@ attribute name="img"%>
<%@ attribute name="checkbox"%>
<%@ attribute name="target"%>
<!-- 設定預設值 -->
<!-- length=50 每條資訊預設顯示的長度為50 -->
<!-- align=center 表格預設置中顯示 -->
<!-- name1 待處理的資料2 -->
<c:set var="length" value="${empty length ? 50 :length}"/>
<c:set var="align" value="${empty align ? 'left' :align}"/>
<c:set var="name1" value="${empty name1 ? '' : name1}"/>
<c:set var="target" value="${empty target ? '_blank' : ''}"/>
<!-- 顯示內容 -->
<td
<c:forEach items="${attributes}" var="a">
${a.key}="${a.value}"
</c:forEach>
>
<c:if test="${checkbox == 'true'}">
<input type="checkbox" name="are" value="ok"/>
</c:if>
<c:set var="checktitle" value="${empty fn:substring(name,length,length+20)}"/>
<c:choose>
<c:when test="${empty url}">
<div align="${align}">
<font size="2.5"><c:out value="${fn:substring(name,0,length)}"/></font>
${checktitle ? "" : "..."}
<font size="2.5"><c:out value="${name1}"/></font>
</div>
</c:when>
<c:otherwise>
<a href="${url}" target="${target}">
<div align="${align}">
<font size="2.5"><c:out value="${fn:substring(name,0,length)}"/></font>
${checktitle ? '' : "..."}
<font size="2.5"><c:out value="${name1}"/></font>
</div>
</a>
</c:otherwise>
</c:choose>
<c:if test="${! empty img}">
<img src="${img}" align="right">
</c:if>
</td>
前面兩個標記檔案放在/WEB-INF/tags/mytags中
二、JAVABEAN
1、pagination.java
/*
* @date 2005-2-18
* @project JapaneseInDalian
* @company winmess
* @version 1.2
*/
package com.winmess.japanese.util;
import java.util.List;
/**
* @author chanson
* @description 分頁的基本類
*/
public class Pagination {
private List data; //記錄數
private int totalRows; //總行數
private int perpageRows; //每頁行數
private int totalPages; //總頁數
private int currentPage; //當前頁
private int currentRow; //當前行
private int lastRow; //尾行
/**
* 預設的建構函式
*/
public Pagination(){
super();
}
/**
* 根據data,perpageRows和currentPage計算各自的值
* @param list
* @param perPageRows
* @param currentPage
*/
public void reckon(){
/**
* 值設定
*/
this.totalRows = data.size(); //總行數
this.perpageRows = perpageRows < 1 ? 10 : perpageRows; //每頁行數,預設10行
this.totalPages = totalRows / perpageRows + 1; //總頁數
if((totalRows % perpageRows == 0)&&(totalRows != 0)){
this.totalPages = this.totalPages -1;
}
this.currentPage = currentPage < 1 ? 1 : currentPage; //當前頁數,預設第1頁
//當前頁超過最大頁時,當前頁設定為最大頁
this.currentPage = currentPage > totalPages ? totalPages : currentPage;
this.currentRow = (currentPage - 1) * perpageRows; //當前開頭行
this.lastRow = currentRow + perpageRows - 1; //尾行
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
public int getLastRow() {
return lastRow;
}
public void setLastRow(int lastRow) {
this.lastRow = lastRow;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
reckon();
}
public int getCurrentRow() {
return currentRow;
}
public void setCurrentRow(int currentRow) {
this.currentRow = currentRow;
}
public int getperPageRows() {
return perpageRows;
}
public void setPerpageRows(int perpageRows) {
this.perpageRows = perpageRows;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
}///:~
2、PaginationPrompt.java
/*
* @date 2005-3-18
* @project JapaneseInDalian
* @company winmess
* @version 1.2
*/
package com.winmess.japanese.util;
import java.io.UnsupportedEncodingException;
/**
* @author chanson
*
*/
public class PaginationPrompt {
private String firstPage = "最新"; // 首頁
private String lastPage = "最初"; // 尾頁
private String prePage = "前へ"; // 前一頁
private String nextPage = "次へ"; // 下一頁
private String page = "ページへ"; // 跳轉到第幾頁
private String pagePrompt = "ページを表示"; // 頁提示 :ページを表示
private String total = "合計:"; // 總共
private String record = "件"; // 幾條記錄
public String getFirstPage() throws UnsupportedEncodingException {
firstPage = new String(firstPage.getBytes("shift-jis"), "ISO-8859-1");
return firstPage;
}
public String getTotal() throws UnsupportedEncodingException {
total = new String(total.getBytes("shift-jis"), "ISO-8859-1");
return total;
}
public String getLastPage() throws UnsupportedEncodingException {
lastPage = new String(lastPage.getBytes("shift-jis"), "ISO-8859-1");
return lastPage;
}
public String getNextPage() throws UnsupportedEncodingException {
nextPage = new String(nextPage.getBytes("shift-jis"), "ISO-8859-1");
return nextPage;
}
public String getPage() throws UnsupportedEncodingException {
page = new String(page.getBytes("shift-jis"), "ISO-8859-1");
return page;
}
public String getPagePrompt() throws UnsupportedEncodingException {
pagePrompt = new String(pagePrompt.getBytes("shift-jis"), "ISO-8859-1");
return pagePrompt;
}
public String getPrePage() throws UnsupportedEncodingException {
prePage = new String(prePage.getBytes("shift-jis"), "ISO-8859-1");
return prePage;
}
public String getRecord() throws UnsupportedEncodingException {
record = new String(record.getBytes("shift-jis"), "ISO-8859-1");
return record;
}
}
三、在JSP頁面中調用
1、聲明
<%@ taglib prefix="my" tagdir="/WEB-INF/tags/mytags"%>
2、使用
<%@ attribute name="align"%>
<%@ attribute name="info_list" fragment="true" required="true"%>
<my:iterator data="${list}" page="${current_page}" var="current" border="1" isPagination="true" align="center">
<jsp:attribute name="info_list">
<my:param name="${current.id}" width="4"/>
<my:param name="${current.callnumber}" width="10" length="6" url="..${requestURI}"/>
<my:param name="${current.bookType}" width="10"/>
<my:param name="${current.bookID}" width="6"/>
<my:param name="${current.bookName}" width="10"/>
<my:param name="${current.inNumber}" width="5"/>
<my:param name="${current.bookAuthor}" width="8"/>
<my:param name="${current.bookPublisher}" width="10"/>
<my:param name="${current.bookEditionOrder}" width="10"/>
<my:param name="${current.bookEditionOrder}" width="10"/>
<my:param name="${current.bookPrice}" width="10"/>
<my:param name="${current.bookParenthesis}" width="5"/>
</jsp:attribute>
</my:iterator>