版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
一 分页实体
package org.fkit.common.util.pager;
public class PageModel {
/** 分页中默认一个4条数据 */
public static final int PAGE_DEFAULT_SIZE = 4;
/** 分页总数据条数 */
private long recordCount;
/** 当前页面 */
private int pageIndex;
/** 每页分多少条数据 */
private int pageSize = PAGE_DEFAULT_SIZE;
/** 总页数 */
private int totalSize;
public long getRecordCount() {
this.recordCount = this.recordCount <= 0 ? 0 : this.recordCount;
return recordCount;
}
public void setRecordCount(long recordCount) {
this.recordCount = recordCount;
}
public int getPageIndex() {
// this.pageIndex = this.pageIndex>=this.getTotalSize()?this.getTotalSize():this.pageIndex;
this.pageIndex = this.pageIndex <= 0 ? 1 : this.pageIndex;
// /** 判断当前页面是否超过了总页数:如果超过了默认给最后一页作为当前页 */
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
this.pageSize = this.pageSize <= PAGE_DEFAULT_SIZE ? PAGE_DEFAULT_SIZE : this.pageSize;
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalSize() {
if (this.getRecordCount() <= 0) {
totalSize = 0;
} else {
totalSize = (int) ((this.getRecordCount() - 1) / this.getPageSize() + 1);
}
return totalSize;
}
public int getFirstLimitParam() {
return (this.getPageIndex() - 1) * this.getPageSize();
}
}
二 定义标签
package org.fkit.common.util.pager;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class PagerTag extends SimpleTagSupport {
/** 定义请求URL中的占位符常量 */
private static final String TAG = "{0}";
/** 当前页码 */
private int pageIndex;
/** 每页显示的数量 */
private int pageSize;
/** 总记录条数 */
private int recordCount;
/** 请求URL page.action?pageIndex={0} */
private String submitUrl;
/** 定义总页数 */
private int totalPage = 0;
/** 在页面上引用自定义标签就会触发一个标签处理类 */
@Override
public void doTag() throws JspException, IOException {
/** 定义它拼接是终的结果 */
StringBuilder res = new StringBuilder();
res.append("<center>\n" + "\t\t<p style=\"text-align: center;\">\n" + "\t\t\t<!-- 设计导航-->\n"
+ "\t\t\t<nav class=\"nav form-inline\">\n" + "\t\t\t\t <ul class=\"pagination alin\">");
/** 定义它拼接中间的页码 */
StringBuilder str = new StringBuilder();
/** 判断总记录条数 */
if (recordCount > 0) { // 1499 / 15 = 100
/** 需要显示分页标签,计算出总页数 需要分多少页 */
totalPage = (this.recordCount - 1) / this.pageSize + 1;
/** 判断上一页或下一页需不需要加a标签 */
if (this.pageIndex == 1) { // 首页
str.append("<li class=\"disabled\" ><a href=\"#\">上一页</a></li>");
/** 计算中间的页码 */
this.calcPage(str);
/** 下一页需不需要a标签 */
if (this.pageIndex == totalPage) {
/** 只有一页 */
str.append("<li class=\"disabled\" ><a href=\"#\">下一页</a></li>");
} else {
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex + 1));
str.append("<li><a href='" + tempUrl + "'>下一页</a></li>");
}
} else if (this.pageIndex == totalPage) { // 尾页
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex - 1));
str.append("<li><a href='" + tempUrl + "'>上一页</a></li>");
/** 计算中间的页码 */
this.calcPage(str);
str.append("<li class=\"disabled\" ><a href=\"#\">下一页</a></li>");
} else { // 中间
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex - 1));
str.append("<li><a href='" + tempUrl + "'>上一页</a></li>");
/** 计算中间的页码 */
this.calcPage(str);
tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex + 1));
str.append("<li><a href='" + tempUrl + "'>下一页</a></li>");
}
res.append(str);
/** 开始条数 */
int startNum = (this.pageIndex - 1) * this.pageSize + 1;
/** 结束条数 */
int endNum = (this.pageIndex == this.totalPage) ? this.recordCount : this.pageIndex * this.pageSize;
res.append("<li><a style=\"background-color:#D4D4D4;\" href=\"#\">共<font color='red'>" + this.recordCount
+ "</font>条记录,当前显示" + startNum + "-" + endNum + "条记录</a> </li>");
res.append("<div class=\"input-group\">\n"
+ "\t\t\t\t\t\t\t\t\t <input id='pager_jump_page_size' value='" + this.pageIndex
+ "' type=\"text\" style=\"width: 60px;text-align: center;\" class=\"form-control\" placeholder=\""
+ this.pageIndex + "\"\">\n" + "\t\t\t\t\t\t\t\t\t <span class=\"input-group-btn\">\n"
+ "\t\t\t\t\t\t\t\t\t <button class=\"btn btn-info\" id='pager_jump_btn' type=\"button\">GO</button>\n"
+ "\t\t\t\t\t\t\t\t\t </span>\n" + "\t\t\t\t\t \t\t\t\t </div>");
res.append("<script type='text/javascript'>");
res.append(" document.getElementById('pager_jump_btn').onclick = function(){");
res.append(" var page_size = document.getElementById('pager_jump_page_size').value;");
res.append(" if (!/^[1-9]\\d*$/.test(page_size) || page_size < 1 || page_size > " + this.totalPage
+ "){");
res.append(" alert('请输入[1-" + this.totalPage + "]之间的页码!');");
res.append(" }else{");
res.append(" var submit_url = '" + this.submitUrl + "';");
res.append(" window.location = submit_url.replace('" + TAG + "', page_size);");
res.append(" }");
res.append("}");
res.append("</script>");
} else {
res.append(
"<li><a style=\"background-color:#D4D4D4;\" href=\"#\">总共<font color='red'>0</font>条记录,当前显示0-0条记录。</a> </li>");
}
res.append("</ul></nav></p></center>");
this.getJspContext().getOut().print(res.toString());
}
/** 计算中间页码的方法 */
private void calcPage(StringBuilder str) {
/** 判断总页数 */
if (this.totalPage <= 11) {
/** 一次性显示全部的页码 */
for (int i = 1; i <= this.totalPage; i++) {
if (this.pageIndex == i) {
/** 当前页码 */
str.append("<li class=\"active\" ><a href=\"#\">" + i + "</a></li>");
} else {
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(i));
str.append("<li><a href='" + tempUrl + "'>" + i + "</a></li>");
}
}
} else {
/** 靠首页近些 */
if (this.pageIndex <= 8) {
for (int i = 1; i <= 10; i++) {
if (this.pageIndex == i) {
/** 当前页码 */
str.append("<li class=\"active\" ><a href=\"#\">" + i + "</a></li>");
} else {
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(i));
str.append("<li><a href='" + tempUrl + "'>" + i + "</a></li>");
}
}
str.append("<li><a href=\"#\">...</a></li>");
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(this.totalPage));
str.append("<li><a href='" + tempUrl + "'>" + this.totalPage + "</a></li>");
}
/** 靠尾页近些 */
else if (this.pageIndex + 8 >= this.totalPage) {
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(1));
str.append("<li><a href='" + tempUrl + "'>1</a></li>");
str.append("<li><a href=\"#\">...</a></li>");
for (int i = this.totalPage - 10; i <= this.totalPage; i++) {
if (this.pageIndex == i) {
/** 当前页码 */
str.append("<li class=\"active\" ><a href=\"#\">" + i + "</a></li>");
} else {
tempUrl = this.submitUrl.replace(TAG, String.valueOf(i));
str.append("<li><a href='" + tempUrl + "'>" + i + "</a></li>");
}
}
}
/** 在中间 */
else {
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(1));
str.append("<li><a href='" + tempUrl + "'>1</a></li>");
str.append("<li><a href=\"#\">...</a></li>");
for (int i = this.pageIndex - 4; i <= this.pageIndex + 4; i++) {
if (this.pageIndex == i) {
/** 当前页码 */
str.append("<li class=\"active\" ><a href=\"#\">" + i + "</a></li>");
} else {
tempUrl = this.submitUrl.replace(TAG, String.valueOf(i));
str.append("<li><a href='" + tempUrl + "'>" + i + "</a></li>");
}
}
str.append("<li><a href=\"#\">...</a></li>");
tempUrl = this.submitUrl.replace(TAG, String.valueOf(this.totalPage));
str.append("<li><a href='" + tempUrl + "'>" + this.totalPage + "</a></li>");
}
}
}
/** setter method */
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
public void setSubmitUrl(String submitUrl) {
this.submitUrl = submitUrl;
}
}
三 tld标签文件
<?xml version="1.0" encoding="utf-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<!-- 描述 自定义标签版本的一种描述 -->
<description>Pager 1.0 core library</description>
<!-- 显示的名称 导包进行的一个展示 -->
<display-name>Pager core</display-name>
<!-- 版本号 -->
<tlib-version>1.0</tlib-version>
<!-- 短名 -->
<short-name>fkjava</short-name>
<!-- uri :导包 -->
<uri>/pager-tags</uri>
<!-- 定义一个标签 -->
<tag>
<!-- 标签名 -->
<name>pager</name>
<!-- 标签处理类 -->
<tag-class>org.fkit.common.util.pager.PagerTag</tag-class>
<!-- 设置标签为空 -->
<body-content>empty</body-content>
<!-- 定义标签的属性 -->
<attribute>
<!-- 属性名 表示分页的第几页 -->
<name>pageIndex</name>
<!-- 必须的 -->
<required>true</required>
<!-- run time expression value 为true支持EL表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 定义标签的属性 -->
<attribute>
<!-- 属性名 表示分页标签 ,每页显示多少条数据 -->
<name>pageSize</name>
<!-- 必须的 -->
<required>true</required>
<!-- run time expression value 为true支持EL表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 定义标签的属性 -->
<attribute>
<!-- 属性名 记录分页的总数 -->
<name>recordCount</name>
<!-- 必须的 -->
<required>true</required>
<!-- run time expression value 为true支持EL表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 定义标签的属性 -->
<attribute>
<!-- 属性名 -->
<name>submitUrl</name>
<!-- 必须的 -->
<required>true</required>
<!-- run time expression value 为true支持EL表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 定义标签的属性 -->
<attribute>
<!-- 属性名 -->
<name>style</name>
<!-- 必须的 -->
<required>false</required>
<!-- run time expression value 为true支持EL表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
四 标签的使用
1 包含标签taglib.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
<%@ taglib prefix="fkjava" uri="/pager-tags" %>
2 标签使用片段
<div class="panel-body">
<table class="table table-bordered" style="float: right;">
<thead>
<tr style="font-size: 12px;" align="center">
<th style="text-align: center;"><input type="checkbox" id="checkAll"/></th>
<th style="text-align: center;">编号</th>
<th style="text-align: center;">名称</th>
<!-- <th>备注</th> -->
<th style="text-align: center;">链接</th>
<th style="text-align: center;">操作</th>
<!-- <th style="text-align: center;">创建日期</th> -->
<!-- <th style="text-align: center;">创建人</th> -->
<!-- <th>修改日期</th> -->
<th style="text-align: center;">修改人</th>
<th style="text-align: center;">操作</th>
</tr>
</thead>
<c:forEach items="${modules}" var="module" varStatus="stat">
<tr align="center" id="dataTr_${stat.index}">
<td><input type="checkbox" name="box" id="box_${stat.index}" value="${module.code}"/></td>
<td>${module.code}</td>
<td>${module.name.replaceAll("-","")}</td>
<%-- <td>${module.remark}</td> --%>
<td>${module.url}</td>
<td><span class="label label-success"><a href="${ctx}/identity/module/getModulesByParent?parentCode=${module.code}" style="color: white;">查看下级</a></span></td>
<%-- <td><fmt:formatDate value="${module.createDate}" pattern="yyyy-MM-dd HH:mm:ss"/></td> --%>
<%-- <td>${module.creater.name }</td> --%>
<%-- <td><fmt:formatDate value="${module.modifyDate}" pattern="yyyy-MM-dd HH:mm:ss"/></td> --%>
<td>${module.modifier.name }</td>
<td><span class="label label-info"><a href="javascript:updateModule('${module.code}');" style="color: white;">修改</a></span></td>
</tr>
</c:forEach>
</table>
<!-- 分页标签区 -->
<fkjava:pager pageIndex="${pageModel.pageIndex}"
pageSize="${pageModel.pageSize}"
recordCount="${pageModel.recordCount}"
submitUrl="${ctx}/identity/module/getModulesByParent?pageIndex={0}&parentCode=${parentCode}"/>
</div>
五 显示效果