日期:2014-05-16 浏览次数:20463 次
package com.test.pagemanager; import java.util.ArrayList; import java.util.List; public class PageManager { private static List<String> names; { names = new ArrayList<String>(); names.add(" black "); names.add(" black "); names.add(" black "); names.add(" black "); names.add(" blue "); names.add(" blue "); names.add(" blue "); names.add(" blue "); names.add(" blue "); names.add(" red "); names.add(" yellow "); names.add(" blue "); names.add(" green "); names.add(" blue "); names.add(" black "); names.add(" green "); names.add(" black "); } /** * 查询函数 * * @param pageNum 页码 * @param pageSize 页大小 * @param condition 查询条件 */ public List<String> query(int pageNum, int pageSize, String condition) { List<String> tmpNames = getNames(condition); List<String> ret = new ArrayList<String>(); int start = (pageNum - 1) * pageSize; int end = start + pageSize - 1; if (start >= tmpNames.size()) return ret; for (int i = 0; i < tmpNames.size(); i++) { if (i >= start && i <= end) ret.add(tmpNames.get(i)); } return ret; } /** * 虚拟查询方法 * @param condition * @return */ private List<String> getNames(String condition) { if (null == condition) return names; List<String> ret = new ArrayList<String>(); for (int i = 0; i < names.size(); i++) { if (condition.equals(names.get(i))) ret.add(names.get(i)); } return ret; } /** * 得到查询结果 * @param condition * @return */ public int getSize(String condition) { return getNames(condition).size(); } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg"%> <jsp:useBean id="pm" scope="page" class="com.test.pagemanager.PageManager"/> <html><head></head><body> <form name="cc" action="MyJsp.jsp " method="post"> <input type="text" name="name" value="<%=request.getParameter("name")%>" /> <input type="submit" value="search" /> </form> <pg:pager items="<%=pm.getSize(request.getParameter("name"))%>" index="center" maxPageItems="5" maxIndexPages="5" isOffset="<%=false%>" export="pageOffset,currentPageNumber=pageNumber" scope="request"> <% int pageNum = 1; if (request.getParameter("pageNo") != null) pageNum = Integer.valueOf(request.getParameter("pageNo")); int pageSize = 5; List result = pm.query(pageNum, pageSize, request.getParameter("name")); %> <table><tr><td>result</td></tr> <%for (int i = 0; i < result.size(); i++) {%> <tr><td><%=result.get(i)%></td></tr><%}%> </table> <pg:param name="name" /> <pg:index> <jsp:include page="jsptags.jsp" flush="true" /> </pg:index> </pg:pager> </body> </html>
<%@ page session="false" contentType="text/html;charset=utf-8"%> <%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg"%> <pg:first> <a href="<%=pageUrl%>&pageNo=<%=pageNumber%>"><nobr>[#<%=pageNumber%>首页]</nobr></a> </pg:first> <pg:skip pages="<%=-5%>"> <a href="<%=pageUrl%>&pageNo=<%=pageNumber%>"><nobr>[#<%=pa