pager-taglib -- 分页标签用法
Using JSP Pager Taglib with Servlet To Paginate
2009-03-26 20:55
package com.lucas.service;
import java.util.List;
public class PageModel { private int total ; // 总记录数 private int size = 10; private List items ;
............................................
setter getter ...................
} -----------------------------------------------------------------------------------------------------------------------------------index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri=" http://java.sun.com/jsp/jstl/core " prefix="c"%> <%@ taglib uri=" http://jsptags.com/tags/navigation/pager " prefix="pg"%> <!-- 以上保持不变 --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Pager Test</title> </head>
<body> <c:forEach items="${pager.items}" var="user"> <c:out value="${user.id}"></c:out> <c:out value="${user.name}"></c:out> <br/> </c:forEach> <pg:pager scope="request" maxIndexPages="5" index="center" maxPageItems="${pager.size }" url="UserServlet " items="${pager.total }" // size total 是 PagerModel中的属性 export="currentPageNumber=pageNumber"> <!-- url{ UserServlet } 中要传递的参数 --> <pg:param name="method" value="getPager" />
<!-- 以下内容保持不变 --> <pg:first> <a href="${pageUrl}">首页</a> </pg:first> <pg:prev> <a href="${pageUrl}">前页</a> </pg:prev> <pg:pages> <c:choose> <c:when test="${pageNumber eq currentPageNumber }"> <font color="red">${pageNumber }</font> </c:when> <c:otherwise> <a href="${pageUrl }">${pageNumber}</a> </c:otherwise> </c:choose> </pg:pages> <pg:next> <a href="${pageUrl}">下页</a> </pg:next> <pg:last> <a href="${pageUrl}">尾页</a> </pg:last> </pg:pager>
</body> </html>
================================================================================= private void list(HttpServletRequest request, HttpServletResponse response) { String temp1 = request.getParameter("pager.offset");
// pager.offset 是 pager-tablib.jar 中自带的参数, 在参数传递过程中传递 offset 参数
int start = convertToInt(temp1, 0); try {
// service.getPager(...) ; 返回一个 PagerModel PageObject pager = service.getPager(start);
----------------------------------------------------- request.setAttribute("pager", pager); request.getRequestDispatcher("/index.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); } }
private int convertToInt(String value,int defaultValue){ int ret = 0; try{ ret = Integer.parseInt(value); if(ret<0 )ret = defaultValue; // limit 0,5 }catch (Exception e) { ret = defaultValue; } return ret; } ===================================== test