这是我们项目中的分页代码,是用JSTL + 自定义标签文件 + JAVABEAN实现的

这是我们项目中的分页代码,是用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%">
 &nbsp;
 ${prompt.total}${pagination.totalRows}${prompt.record}
 &nbsp;&nbsp;
    ${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>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值