JSP分页demo
首先创建一个类PagingDemo提供测试数据,方法getResults获得请求的数据,prepareURL产生每一个page的url。prepareURL方法有点笨,用正则表达式也可以完成,不过我不知道如何写package test.demo;import java.util.ArrayList;import java.util.List;import javax.servlet.http.HttpServletRequest;public class PagingDemo {private static final List testRepo = new ArrayList();private int totalCount;static {for (int i = 0; i < 100; i++) {testRepo.add("testdata." + i);}}public static void main(String[] args) {}public int getTotalCount() {return this.totalCount;}public List getResults(int page, int count) {if (page < 1 || count < 1) {return null;}totalCount = testRepo.size();int firstIndex = (page - 1) * count;if (firstIndex >= totalCount) {return null;}int lastIndex = firstIndex + count;if (lastIndex > totalCount) {lastIndex = totalCount;}return testRepo.subList(firstIndex, lastIndex);}public static String prepareURL(HttpServletRequest request) {String requestUrl = request.getRequestURI() + "?";String queryString = request.getQueryString();if (queryString != null) {// remove the parameter 'page' from the query stringint index = queryString.indexOf("page=");if (index != -1) {int i = index;for (; i < queryString.length(); i++) {char ch = queryString.charAt(i);if (ch == '&') {i++;break;}}String sub = queryString.substring(index, i);requestUrl += queryString.replace(sub, "")+ (i < queryString.length() ? "&page=" : "page=");}}else {requestUrl += "page=";}return requestUrl;}}
页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@page import="test.demo.PagingDemo"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";String requestUrl = PagingDemo.prepareURL(request);String pageNum = request.getParameter("page");if(pageNum == null) {pageNum = "1";}%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <base href="<%=basePath%>"> <title>pages</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><style type="text/css">ul {margin:0px;padding:0px;}ul li {list-style-type: none;}a:link, a:visited {color:gray;text-decoration:none;padding:2px 5px;}.paging li {float:left;border:1px solid gray;text-align:center;margin:2px;color:gray;}</style></head> <body><div style="width:400px;height:300px;border:1px solid blue;"><ul><%int pn = 1;try {pn = Integer.valueOf(pageNum);if(pn < 1) {pn = 1;}}catch (Exception e) {pn = 1;}int maxResultCount = 15;PagingDemo demo = new PagingDemo();List results = demo.getResults(pn, maxResultCount);if(results == null || results.isEmpty()) {out.println("<li>No results need to be shown</li>");}else {for(int i = 0; i < results.size(); i++) {out.println("<li>" + results.get(i) + "</li>"); } int total = demo.getTotalCount();int totalPages = total / maxResultCount;if(total % maxResultCount != 0) {totalPages++;} if(totalPages > 1) {int firstPage = pn - 2;int lastPage = pn + 2;if(firstPage < 1) {firstPage = 1;}if(lastPage > totalPages) {lastPage = totalPages;}out.println("<div class='paging'><ul>");out.println("<li>" + pn + "/" + totalPages + "</li>");if(pn > 1) {out.println("<li><a href='"+ requestUrl + (pn - 1) +"'>&lt;&lt;</a></li>");}for(int i = firstPage; i <= lastPage; i++) {if(i != pn) {out.println("<li><a href='"+ requestUrl + i + "'>" + i +"</a></li>");}else {out.println("<li><a style='color:white;background:gray;' href='"+ requestUrl + i + "'>" + i +"</a></li>");}}if(pn < totalPages) {out.println("<li><a href='"+ requestUrl + (pn + 1) +"'>&gt;&gt;</a></li>");}out.println("</ul></div>");}}%></ul></div> <script type="text/javascript"> function removeParamFromUrl(url, param){ if(url === null || url.indexOf("http:") != 0 || typeof param === "undefined" || param === null) { return null; } index = url.indexOf(param + "="); if(index === -1) { return null; } replacedStr = null; lastChar = url.charAt(index - 1); if(lastChar === "&" || lastChar === "?"){ for (i = index; i < url.length; i += 1) { if(url === "&") { replacedStr = url.substring(index, i + 1); break; } } if(i === url.length) { replacedStr = url.substring(index - 1); } } if(replacedStr !== null) { url = url.replace(replacedStr, ""); } return url; } PagingBar = function(total, pages, page){ } </script></body></html>
一个简单的分页就完成了
页:
[1]