信息管理系统——分页标签实现

一 分页实体

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("
\n" + "\t\t

\n" + "\t\t\t\n" + "\t\t\t

"); 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("
  • " + i + "
  • "); } else { String tempUrl = this.submitUrl.replace(TAG, String.valueOf(i)); str.append("
  • " + i + "
  • "); } } } else { /** 靠首页近些 */ if (this.pageIndex <= 8) { for (int i = 1; i <= 10; i++) { if (this.pageIndex == i) { /** 当前页码 */ str.append("
  • " + i + "
  • "); } else { String tempUrl = this.submitUrl.replace(TAG, String.valueOf(i)); str.append("
  • " + i + "
  • "); } } str.append("
  • ...
  • "); String tempUrl = this.submitUrl.replace(TAG, String.valueOf(this.totalPage)); str.append("
  • " + this.totalPage + "
  • "); } /** 靠尾页近些 */ else if (this.pageIndex + 8 >= this.totalPage) { String tempUrl = this.submitUrl.replace(TAG, String.valueOf(1)); str.append("
  • 1
  • "); str.append("
  • ...
  • "); for (int i = this.totalPage - 10; i <= this.totalPage; i++) { if (this.pageIndex == i) { /** 当前页码 */ str.append("
  • " + i + "
  • "); } else { tempUrl = this.submitUrl.replace(TAG, String.valueOf(i)); str.append("
  • " + i + "
  • "); } } } /** 在中间 */ else { String tempUrl = this.submitUrl.replace(TAG, String.valueOf(1)); str.append("
  • 1
  • "); str.append("
  • ...
  • "); for (int i = this.pageIndex - 4; i <= this.pageIndex + 4; i++) { if (this.pageIndex == i) { /** 当前页码 */ str.append("
  • " + i + "
  • "); } else { tempUrl = this.submitUrl.replace(TAG, String.valueOf(i)); str.append("
  • " + i + "
  • "); } } str.append("
  • ...
  • "); tempUrl = this.submitUrl.replace(TAG, String.valueOf(this.totalPage)); str.append("
  • " + this.totalPage + "
  • "); } } } /** 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标签文件

    
    
         
         Pager 1.0 core library
         
         Pager core
         
         1.0
         
         fkjava
         
         /pager-tags
         
         
               
               pager
               
               org.fkit.common.util.pager.PagerTag
               
               empty
               
               
                    
                    pageIndex
                    
                    true
                    
                    true
               
               
               
                    
                    pageSize
                    
                    true
                    
                    true
               
               
               
                    
                    recordCount
                    
                    true
                    
                    true
               
               
               
                    
                    submitUrl
                    
                    true
                    
                    true
               
               
               
                    
                    style
                    
                    false
                    
                    true
               
         
    

    四 标签的使用

    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"  %>
    
    <%@ taglib prefix="fkjava" uri="/pager-tags" %>

    2 标签使用片段

                    
    <%-- --%> <%-- --%> <%-- --%> <%-- --%>
    编号 名称 链接 操作 修改人 操作
    ${module.code} ${module.name.replaceAll("-","")}${module.remark}${module.url} 查看下级${module.creater.name }${module.modifier.name } 修改

    五 显示效果

    信息管理系统——分页标签实现_第1张图片

    你可能感兴趣的:(Spring,Boot)