标签引入分页

public class PageModel {
    /** 分页总数据条数 */
    private int recordCount;
    /** 当前页面 */
    private int pageIndex;
    /** 每页分多少条数据 */
    private int pageSize = 5;

    /** 总页数 */
    private int totalSize;

    public int getRecordCount() {
        this.recordCount = this.recordCount <= 0 ? 0 : this.recordCount;
        return recordCount;
    }

    public void setRecordCount(int recordCount) {
        this.recordCount = recordCount;
    }

    public int getPageIndex() {
        // this.pageIndex = this.pageIndex;// <= 0?1:this.pageIndex;
        /** 判断当前页面是否超过了总页数:如果超过了默认给最后一页作为当前页 */
        this.pageIndex = this.pageIndex >= this.getTotalSize() ? this.getTotalSize() : this.pageIndex;

        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
        // this.pageSize = pageSize;
        return pageSize;
    }

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

    // 计算总页数
    public int getTotalSize() {
        if (this.getRecordCount() <= 0) {
            totalSize = 0;
        } else { // 6-1/5 +1
            totalSize = (this.getRecordCount() - 1) / this.getPageSize() + 1;
        }
        return totalSize;
    }

    // 计算页面的起始索引
    public int getFirstLimitParam() {
        return (this.getPageIndex() - 1) * this.getPageSize() + 1;
    }
}
PageModel
import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class PageTag 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 String style = "sabrosus";

    /** 定义总页数 */
    private int totalPage = 0;

    /** 在页面上引用自定义标签就会触发一个标签处理类 */
    @Override
    public void doTag() throws JspException, IOException {
        /** 定义它拼接是终的结果 */
        StringBuilder res = new StringBuilder();
        /** 定义它拼接中间的页码 */
        StringBuilder str = new StringBuilder();
        /** 判断总记录条数 */
        if (recordCount > 0) { // 1499 / 15 = 100
            /** 需要显示分页标签,计算出总页数 需要分多少页 */

            totalPage = (this.recordCount - 1) / this.pageSize + 1;

            /** 判断上一页或下一页需不需要加a标签 */
            if (this.pageIndex == 1) { // 首页
                str.append("上一页");

                /** 计算中间的页码 */
                this.calcPage(str);

                /** 下一页需不需要a标签 */
                if (this.pageIndex == totalPage) {
                    /** 只有一页 */
                    str.append("下一页");
                } else {
                    String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex + 1));
                    str.append("下一页");
                }
            } else if (this.pageIndex == totalPage) { // 尾页
                String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex - 1));
                str.append("上一页");

                /** 计算中间的页码 */
                this.calcPage(str);

                str.append("下一页");
            } else { // 中间
                String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex - 1));
                str.append("上一页");

                /** 计算中间的页码 */
                this.calcPage(str);

                tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex + 1));
                str.append("下一页");
            }

            /** 拼接其它的信息 */
            res.append("");
            res.append("");
            res.append(
                    "");
            res.append("
" + str.toString()); res.append( " 跳转到  "); res.append( " "); res.append("
"); /** 开始条数 */ int startNum = (this.pageIndex - 1) * this.pageSize + 1; /** 结束条数 */ int endNum = (this.pageIndex == this.totalPage) ? this.recordCount : this.pageIndex * this.pageSize; res.append( "总共" + this.recordCount + "条记录,当前显示" + startNum + "-" + endNum + "条记录。"); res.append("
"); res.append(""); } else { res.append( "
总共0条记录,当前显示0-0条记录。
"); } 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 方法 */ 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; } public void setStyle(String style) { this.style = style; } }
PageTag
"1.0" encoding="utf-8"?>
"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">
   
   
  Pager 1.0 core library
  
  Pager core
  
  1.0
  
  <short-name>fkjavashort-name>
  
  http://zhulina.pager-tags
  
  
  
          
          pager
          
          class>com.baizhi.zln.util.PageTagclass>
          
          empty
          
          
          
              
              pageIndex
              
              true
              
              true
          
          
          
          
              
              pageSize
              
              true
              
              true
          
          
          
              
              recordCount
              
              true
              
              true
          
          
          
              
              submitUrl
              
              true
              
              true
          
          
          
              
              style
              
              false
              
              true
          
  
page.tld

 

1. 首先引入上面的工具类

2. 在你的page.tld 文件中修改uri

标签引入分页_第1张图片

 标签引入分页_第2张图片

 

 

 3. 在jsp 页面中引入

标签引入分页_第3张图片

 

 4. 在这个jsp页面中 引入需要添加的参数

 
"align-content: center; margin-left: 40%"> "${pageModel.pageIndex }" pageSize="${pageModel.pageSize }" recordCount="${sum}" submitUrl="${path }/adminBack/adminBack_listCatoryAdmin?pageIndex={0}"/>

注意:page Index ={0} 这个值不需要动

标签引入分页_第4张图片

 

 

 5.  后台代码

    public String listCatoryAdmin() {
        // 查找类别的所有记录
        sesondCategory = adminBackService.findCategory();
        for (Category category : sesondCategory) {
            System.out.println(category);
        }
        // 查找类别数量
        sum = adminBackService.findALlSumCatory();
        // 分页信息
        pageModel.setRecordCount(sum); // 总共有catogorys.size()条数据

        if (pageIndex == null) {
            pageModel.setPageIndex(1);
        }
        if (pageIndex != null) {
            pageModel.setPageIndex(pageIndex);// 当前页是第一页
        }

        categoryCount = adminBackService.findCountCategory(pageModel.getFirstLimitParam(),
                pageModel.getFirstLimitParam() + 5);
        // 查找指定条数的记录

        return "listCatoryAdmin";
    }
后台代码

注意:只需要向法定发中传递两个参数 一个是起始索引 另外一个是结束索引

而这两个值是根据 总计量数和页面索引记录出来的

标签引入分页_第5张图片

 

 我们在这个测试类中只需要向 pageModel 类中传入三个对象

     (1). 记录总条数

      (2). 当前页也就是默认第一页

注意: 其实是不用判断pageIndex的 只需要在一次使用的时候设置上默认值为第一页即可

标签引入分页_第6张图片

 

  可以在这里修改每页显示所少条数据

 

6. 数据库代码

 <select id="selectCategoryCount" resultMap="myCategorycountMap">
    select * from ( select dd.*, rownum r from dd_category  dd ) 
     <where>
        <if test="firstParam !=null">
                r >= #{firstParam}
            if>
            <if test="endParam !=null">
                and r <= #{endParam}
            if>
     
     where>
  
  select>
数据库代码

标签引入分页_第7张图片

 

你可能感兴趣的:(标签引入分页)