SSH 分页代码

记录下分页用到的代码:
import java.io.IOException;

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

public class DoFenYeTaglib extends SimpleTagSupport {
 private int pageNum;//当前页数
 private int totalPage;//总页数
 private int allCounts;//取出记录总条数
 private int count;//每页显示多少条
 
 private String className;
 private String para;
 private String functionName;
 
 public int getCount() {
  return count;
 }

 public void setCount(int count) {
  this.count = count;
 }

 public String getFunctionName() {
  return functionName;
 }

 public int getAllCounts() {
  return allCounts;
 }

 public void setAllCounts(int allCounts) {
  this.allCounts = allCounts;
 }

 public void setFunctionName(String functionName) {
  this.functionName = functionName;
 }

 public String getPara() {
  return para;
 }

 public void setPara(String para) {
  this.para = para;
 }

 public int getPageNum() {
  return pageNum;
 }

 public void setPageNum(int pageNum) {
  this.pageNum = pageNum;
 }

 public int getTotalPage() {
  return totalPage;
 }

 public void setTotalPage(int totalPage) {
  this.totalPage = totalPage;
 }

 public String getClassName() {
  return className;
 }

 public void setClassName(String className) {
  this.className = className;
 }

 public void doTag() throws JspException, IOException {
  JspWriter out = getJspContext().getOut();
  
        int liststep = 10;//最多显示分页页数
        int pages = 1;//默认显示第一页
        if (pageNum != 0) {
            pages = pageNum;//分页页码变量
        }
       
        if (totalPage < pages) {
            pages = totalPage;//如果分页变量大总页数,则将分页变量设计为总页数
        }
        if (pages < 1) {
            pages = 1;//如果分页变量小于1,则将分页变量设为1
        }
        int listbegin = (pages - (int) Math.ceil((double) liststep / 2));//从第几页开始显示分页信息
        if (listbegin < 1) {
            listbegin = 1;
        }
        int listend = pages + liststep/2;//分页信息显示到第几页
        if (listend > totalPage) {
            listend = totalPage + 1;
        }
          
        // 显示 首页 上一页
        if (pages > 1) {
         out.print("<a href='" + this.className + ".do?" + this.para + "="+ this.functionName + "&pageNum=1&count=" 
           + this.count + "&allCounts=" + this.allCounts +"'> 首页   </a>");
         out.print("<a href='" + this.className + ".do?" + this.para + "="+ this.functionName + "&pageNum=" + (this.pageNum - 1)
     + "&count=" + this.count + "&allCounts=" + this.allCounts + "'>   <上一页   </a>");
        }
        //显示 分页码
        for (int i = listbegin; i < listend; i++) {
            if (i != pages) {//如果i不等于当前页
             out.print("<a href='" + this.className + ".do?" + this.para + "="+ this.functionName + "&pageNum=" + i
         + "&count=" + this.count + "&allCounts=" + this.allCounts + "'> <b>" + i + "</b> </a>");
            } else {
             out.print(" <b>" + i + " </b>");
            }
        }
        //显示 尾页 下一页
        if (pages != totalPage) {
         out.print("<a href='" + this.className + ".do?" + this.para + "="+ this.functionName + "&pageNum=" + (this.pageNum + 1)
     + "&count=" + this.count + "&allCounts=" + this.allCounts + "'>   下一页>   </a>");
         out.print("<a href='" + this.className + ".do?" + this.para + "="+ this.functionName + "&pageNum=" + this.totalPage
     + "&count=" + this.count + "&allCounts=" + this.allCounts + "'>   尾页 </a>");
        }
  super.doTag();
 }
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值