利用The Pager Tag Library 实现分页功能

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

我在这里简单的介绍一下利用The Pager Tag Library 实现分页功能的步骤:
1: 在应用服务器中,你所应用到项目的web-inf/web.xml中的<web-app>...</web-app> 标签中添加:
            <taglib>
                  <taglib-uri>
                        http://jsptags.com/tags/navigation/pager //可以更改
                  </taglib-uri>
                  <taglib-location>
                        /WEB-INF/jsp/pager-taglib.tld   //指明pager-taglib.tld存放的位置
                  </taglib-location>
           </taglib>
2:   到http://jsptags.com/tags/navigation/pager/下载pager-taglib-2.0.war包,解压后可获得pager-taglib.jar,将其放到web-inf/lib/包下。pager-taglib.tld可以到/WEBINF/jsp/包下。
3:在所需要分页的jsp页面顶部添加:<%@ taglib uri="http://jsptags.com/tags/navigation/pager " prefix="pg" %>
4:
<% totalItems = list.size(); //接收到的list
        String style = “simple“
        Sing position = "both"; //top bottom
        int maxPageItems =1; //每页显示数据的个数
       int maxIndexPages = 4;//显示跳转页面的个数
       String index = "center";
%>
  <pg:pager
    items="<%= totalItems %>"
    index="<%= index %>"
    maxPageItems="<%= maxPageItems %>"
    maxIndexPages="<%= maxIndexPages %>"

    export="offset,currentPageNumber=pageNumber"
    scope="request">

  <pg:param name="style"/>
  <pg:param name="position"/>
  <pg:param name="index"/>
  <pg:param name="maxPageItems"/>
  <pg:param name="maxIndexPages"/>

 <input type="hidden" name="pager.offset" value="<%= offset %>">

  <%-- warn if offset is not a multiple of maxPageItems --%>
  <% if (offset.intValue() % maxPageItems != 0 &&
         ("alltheweb".equals(style) || "lycos".equals(style)) )
     {
  %>
    <p>Warning: The current page offset is not a multiple of Max. Page Items.
    <br>Please
    <pg:first><a href="<%= pageUrl %>">return to the first page</a></pg:first>
    if any displayed range numbers appear incorrect.</p>
  <% } %>

// 显示跳转的jsp;
<% if ("top".equals(position) || "both".equals(position)) { %>
  <pg:index><jsp:include page="paging-links.jsp" flush="true"/></pg:index>
<% } %>

<% for(int i = 0; i <size; i++ ){%>

<pg:item>
    <td> <%= list.get(i)%></td> // 简单的显示数据;
 </pg:item>

<%
   }
%>
</pg:pager>

5. paging-links.jsp 如下:或者参照jsptags中的示范copy你所需要的显示格式
<%@ page session="false" %>
<%@ taglib uri="http://jsptags.com/tags/navigation/pager " prefix="pg" %>
<table width=100% cellpadding=2 cellspacing=0 border=0 bgcolor=e3e9f8>
<tr><td><font face=arial size=2>&nbsp;<b>Matches</b></font>
</td><td align=right nowrap width=1%><font face=arial size=-1>
<pg:index export="total=itemCount">
<pg:page export="first,last">
    <%= first %> - <%= last %> of <%= total %>
</pg:page>
<pg:first export="url" unless="current">
&nbsp;<b><a href="<%= url %>">First Page</a></b>&nbsp;|
</pg:first>
<pg:prev export="url,first,last">
<% int prevItems = (last.intValue() - first.intValue()) + 1; %>
<b><a href="<%= url %>">Previous <%= prevItems %></a></b>
</pg:prev>
<pg:next export="url,first,last">
<% int nextItems = (last.intValue() - first.intValue()) + 1; %>
|&nbsp;<b><a href="<%= url %>">Next <%= nextItems %></a></b>
</pg:next>
 &nbsp;&nbsp;</font></td></tr>
</table>
</pg:index>

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