效果图

直接代码

PagerTag.java

/**
*
*/
package com.web.taglib;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
* 分页标签库
*
*
@author hlw
*
*/
@SuppressWarnings(
"serial")
public class PagerTag extends TagSupport {

private String url;

/**
* 从1开始
*/
private int pageNo = -1;

private int pageSize = 10;

private int totalCount = 0;

private int totalPage = 0;

private int prev = -1;

private int next = -1;

private boolean show = false;

private int btnNum = 9;

private String disableClass = "disablepage";
private String currentClass = "currentpage";
private String id = "pageFormId";
private String name;
private int cutBtnNum;
private int rest;

@Override
public int doStartTag() throws JspException {
if(pageSize <=0 ){
throw new JspException("pageSize must be more than 0 !");
}
if(btnNum<3){
throw new JspException("btnNum must be more than 3!");
}
if(totalCount > 0){
show
= true;
totalPage
= totalCount/pageSize + ((totalCount%pageSize) == 0?0:1);
pageNo
= Math.max(pageNo, 1);
pageNo
= Math.min(pageNo, totalPage);
prev
= pageNo > 1 ? pageNo - 1 : -1;
next
= pageNo < totalPage ? pageNo + 1 : -1;
url
= url != null ? url : ((HttpServletRequest)this.pageContext.getRequest()).getRequestURI();
cutBtnNum
= btnNum - 2;
rest
= cutBtnNum%2;
}
return SKIP_BODY;
}

@Override
public int doEndTag() throws JspException {
if(show == false){
return super.doEndTag();
}
String html
= createHtml();
// 输出显示分页的代码
try {
// 输出分页创建的form
pageContext.getOut().println(html);
}
catch (Exception e) {
e.printStackTrace();
}
// 表示按照正常的流程继续执行JSP网页
return EVAL_PAGE;
}

private String createHtml(){
StringBuffer html
= new StringBuffer();
html.append(
"<form");
if(id != null){
html.append(attr(
"id", id));
}
if(name != null){
html.append(attr(
"name", name));
}
html.append(
" method=\"post\"").append(attr("action", url)).append(">");

html.append(
"<table width=\"90%\" height=\"26\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">")
.append(
"<tr><td width=\"60%\" align=\"left\" valign=\"middle\">").append("<div class=\"pagination\">")
.append(
"<ul>");
html.append(
"总共" + totalCount + "个记录<span style=\"margin-left:2px;\">每页" + pageSize + "条&nbsp;当前" + pageNo
+ "/" + totalPage + "页</span>");
//首页
if(pageNo>1){
html.append(
"<li>" + wrapGoLink(1, "&lt;&lt;") + "</li>");
}
else{
html.append(
"<li class=\"" + disableClass + "\">&lt;&lt;</li>");
}
//上一页按钮
if(prev>0){
html.append(
"<li>" + wrapGoLink(prev, "上一页") + "</li>");
}
else{
html.append(
"<li class=\"" + disableClass + "\">上一页</li>");
}
//总页数小于最大按钮数
if(totalPage<=cutBtnNum){
for(int i = 1;i<= totalPage;i++){
if(pageNo == i){
html.append(
"<li class=\"" + currentClass + "\">" + i + "</li>");
}
else{
html.append(
"<li>" + wrapGoLink(i, i) + "</li>");
}
}
}
else{
//中间有省略
//第一页
if(pageNo == 1){
html.append(
"<li class=\"" + currentClass + "\">1</li>");
}
else{
html.append(
"<li>" + wrapGoLink(1, 1) + "</li>");
}
int harf = cutBtnNum/2;
//中间
if(pageNo>harf + cutBtnNum%2 + 1){
html.append(
"...");
}
int start = pageNo - harf;
int end = pageNo + harf;
if(pageNo - harf<2){
start
= 2;
end
= start + cutBtnNum - rest;
}
if(pageNo + harf>totalPage -1){
end
= totalPage - 1;
start
= end - cutBtnNum + rest;
}
for(int i = start;i<=end;i++){
if(pageNo == i){
html.append(
"<li class=\"" + currentClass + "\">" + i + "</li>");
}
else{
html.append(
"<li>" + wrapGoLink(i, i) + "</li>");
}
}
if(pageNo<totalPage - harf - cutBtnNum%2){
html.append(
"...");
}
//最后一页
if(pageNo == totalPage){
html.append(
"<li class=\"" + currentClass + "\">" + totalPage + "</li>");
}
else{
html.append(
"<li>" + wrapGoLink(totalPage, totalPage) + "</li>");
}
}
//下一页按钮
if(next>0){
html.append(
"<li>" + wrapGoLink(next, "下一页") + "</li>");
}
else{
html.append(
"<li class=\"" + disableClass + "\">下一页</li>");
}
//末页
if(pageNo<totalPage){
html.append(
"<li>" + wrapGoLink(totalPage, "&gt;&gt;") + "</li>");
}
else{
html.append(
"<li class=\"" + disableClass + "\">&gt;&gt;</li>");
}
html.append(
"</ul></div></td><td width=\"10%\" align=\"left\" valign=\"middle\">" +
"" +
"<input type=\"hidden\" name=\"pageSize\" value=\"" + pageSize + "\">" +
"到<input name=\"pageNo\" id=\"" + id + "pageNo" + "\" type=\"text\" size=\"4\" value=\"" + pageNo + "\"/>页</td>")
.append(
"<td width=\"30%\" align=\"left\" valign=\"middle\"><input name=\"Submit3564\" type=\"image\" value=\"提交\" src=\"images/search_13.gif\" align=\"middle\" width=\"43\" height=\"17\" onclick=\"goPage()\"/></td></tr></table>");
html.append(
"</form>");


// 创建JavaScript代码
html.append("<script type='text/javascript'>\n");
html.append(
"function goPage(pageNo){\n");
html.append(
"var pageNoInput = document.getElementById(\"pageFormIdpageNo\");\n");
html.append(
"if(pageNo == undefined){\n");
html.append(
"pageNo = pageNoInput.value;\n");
html.append(
"}\n");
html.append(
"if(isNaN(pageNo)||pageNo<1){\n");
html.append(
"pageNo = 1;\n");
html.append(
"}\n");
html.append(
"var form = document.getElementById(\"pageFormId\");\n");
html.append(
"pageNoInput.value=pageNo;\n");
html.append(
"form.submit();\n");
html.append(
"}\n");
html.append(
"</script>");
return html.toString();
}

private String wrapGoLink(int pageNo, Serializable text){
return new StringBuilder().append("<a href=\"javascript:goPage(" + pageNo + ")\">" + text + "</a>").toString();
}

private String attr(String attrName, Object value){
return new StringBuilder(" ").append(attrName).append("=\"")
.append(value).append(
"\"").toString();
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public int getPageNo() {
return pageNo;
}

public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getTotalCount() {
return totalCount;
}

public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}

public int getBtnNum() {
return btnNum;
}

public void setBtnNum(int btnNum) {
this.btnNum = btnNum;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCurrentClass() {
return currentClass;
}

public void setCurrentClass(String currentClass) {
this.currentClass = currentClass;
}

public String getDisableClass() {
return disableClass;
}

public void setDisableClass(String disableClass) {
this.disableClass = disableClass;
}

public int getCutBtnNum() {
return cutBtnNum;
}

public void setCutBtnNum(int cutBtnNum) {
this.cutBtnNum = cutBtnNum;
}

}

web-tag.tld

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"
>
<taglib>
<tlib-version>1.1.2</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>/web</short-name>
<uri>web</uri>
<description>页面上的标签工具类</description>

<tag>
<name>pager</name>
<tag-class>
com.web.taglib.PagerTag
</tag-class>
<body-content>jsp</body-content>
<description>用于分页的标签</description>
<attribute>
<name>url</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>分页页面URL</description>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>表单id</description>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>表单name</description>
</attribute>
<attribute>
<name>pageNo</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<description>页码</description>
</attribute>
<attribute>
<name>pageSize</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<description>每页记录数</description>
</attribute>
<attribute>
<name>totalCount</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<description>总记录数</description>
</attribute>
<attribute>
<name>btnNum</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>显示的按钮数量,必须大于3</description>
</attribute>
<attribute>
<name>disableClass</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>无效按钮样式</description>
</attribute>
<attribute>
<name>currentClass</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>当前面按钮样式</description>
</attribute>
</tag>
</taglib>

web.xml

<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/web</taglib-uri>
<taglib-location>web-tag.tld</taglib-location>
</taglib>
</jsp-config>

页面使用

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding
="UTF-8"%>
<%@taglib prefix="web" uri="/WEB-INF/web" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="css/index.css" rel="stylesheet" type="text/css" />

<title>Insert title here</title>
</head>
<body>
<% String pageNoStr = request.getParameter("pageNo");
String pageSizeStr = request.getParameter("pageSize");
String totalCountStr = request.getParameter("totalCount");
int pageNo = 1;
int pageSize = 10;
int totalCount = 192;
if(pageNoStr!=null){
pageNo
= Integer.parseInt(pageNoStr);
}
if(pageSizeStr!=null){
pageSize
= Integer.parseInt(pageSizeStr);
}
if(totalCountStr!=null){
totalCount
= Integer.parseInt(totalCountStr);
}
%>
<web:pager pageNo="<%=pageNo %>" currentClass="currentpage" pageSize="<%=pageSize %>" totalCount="<%=totalCount %>"></web:pager>
</body>
</html>
 posted on 2011-06-24 12:59  ﹎敏ō  阅读(2881)  评论(3编辑  收藏  举报