侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

JSP通用高大上分页代码(超管用)

Java  /  管理员 发布于 7年前   192

先给大家展示下分页效果,如果亲们还很满意请参考以下代码。


在超链接中要保留参数

当使用多条件查询后,然后在点击第2 页时,这个第2页超链接没有条件了,所以会丢失条件,所以我们需要在页面上的所有链接都要保留条件!

我们要把条件以一个字符串的形式保存到PageBean的url中!这个任务交给Servlet!

pagebean

package cn.itcast.cstm.domain;import java.util.List;public class PageBean<T> {private int pc;// 当前页码page code//private int tp;// 总页数total pageprivate int tr;// 总记录数total recordprivate int ps;// 每页记录数page sizeprivate List<T> beanList;// 当前页的记录private String url;//它就是url后的条件!public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public int getPc() {return pc;}public void setPc(int pc) {this.pc = pc;}/*** 计算总页数* @return*/public int getTp() {// 通过总记录数和每页记录数来计算总页数int tp = tr / ps;return tr%ps==0 ? tp : tp+1;}// public void setTp(int tp) {// this.tp = tp;// }public int getTr() {return tr;}public void setTr(int tr) {this.tr = tr;}public int getPs() {return ps;}public void setPs(int ps) {this.ps = ps;}public List<T> getBeanList() {return beanList;}public void setBeanList(List<T> beanList) {this.beanList = beanList;}}

jsp页面

第${pb.pc }页/共${pb.tp }页<a href="https:/article/${pb.url }&pc=1">首页</a><c:if test="${pb.pc > 1 }"><a href="https:/article/${pb.url }&pc=${pb.pc-1}">上一页</a></c:if><%-- 计算begin、end --%><c:choose><%-- 如果总页数不足10页,那么把所有的页数都显示出来! --%><c:when test="${pb.tp <= 10 }"><c:set var="begin" value="1" /><c:set var="end" value="${pb.tp }" /></c:when><c:otherwise><%-- 当总页数>10时,通过公式计算出begin和end --%><c:set var="begin" value="${pb.pc-5 }" /><c:set var="end" value="${pb.pc+4 }" /> <%-- 头溢出 --%><c:if test="${begin < 1 }"><c:set var="begin" value="1" /><c:set var="end" value="10" /></c:if> <%-- 尾溢出 --%><c:if test="${end > pb.tp }"><c:set var="begin" value="${pb.tp - 9 }" /><c:set var="end" value="${pb.tp }" /></c:if> </c:otherwise></c:choose><%-- 循环遍历页码列表 --%><c:forEach var="i" begin="${begin }" end="${end }"><c:choose><c:when test="${i eq pb.pc }">[${i }]</c:when><c:otherwise><a href="https:/article/${pb.url }&pc=${i}">[${i }]</a> </c:otherwise></c:choose></c:forEach><c:if test="${pb.pc < pb.tp }"><a href="https:/article/${pb.url }&pc=${pb.pc+1}">下一页</a></c:if><a href="https:/article/${pb.url }&pc=${pb.tp}">尾页</a>

servlet

package cn.itcast.cstm.web.servlet;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.itcast.commons.CommonUtils;import cn.itcast.cstm.domain.Customer;import cn.itcast.cstm.domain.PageBean;import cn.itcast.cstm.service.CustomerService;import cn.itcast.servlet.BaseServlet;public class CustomerServlet extends BaseServlet {private CustomerService customerService = new CustomerService();/*** 获取pc* * @param request* @return*/private int getPc(HttpServletRequest request) {/** 1. 得到pc 如果pc参数不存在,说明pc=1 如果pc参数存在,需要转换成int类型即可*/String value = request.getParameter("pc");if (value == null || value.trim().isEmpty()) {return 1;}return Integer.parseInt(value);}//分页servletpublic String query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// System.out.println(getUrl(request));/** 0. 把条件封装到Customer对象中 1. 得到pc 2. 给定ps 3.* 使用pc和ps,以及条件对象,调用service方法得到PageBean 4. 把PageBean保存到request域中 5.* 转发到list.jsp*/// 获取查询条件Customer criteria = CommonUtils.toBean(request.getParameterMap(), Customer.class);/** 处理GET请求方式编码问题!*/criteria = encoding(criteria);int pc = getPc(request);// 得到pcint ps = 10;// 给定ps的值,第页10行记录PageBean<Customer> pb = customerService.query(criteria, pc, ps);// 得到url,保存到pb中pb.setUrl(getUrl(request));request.setAttribute("pb", pb);return "f:/list.jsp";}/*** 处理四样* * @param criteria* @return* @throws UnsupportedEncodingException*/private Customer encoding(Customer criteria) throws UnsupportedEncodingException {String cname = criteria.getCname();String gender = criteria.getGender();String cellphone = criteria.getCellphone();String email = criteria.getEmail();if (cname != null && !cname.trim().isEmpty()) {cname = new String(cname.getBytes("ISO-8859-1"), "utf-8");criteria.setCname(cname);}if (gender != null && !gender.trim().isEmpty()) {gender = new String(gender.getBytes("ISO-8859-1"), "utf-8");criteria.setGender(gender);}if (cellphone != null && !cellphone.trim().isEmpty()) {cellphone = new String(cellphone.getBytes("ISO-8859-1"), "utf-8");criteria.setCellphone(cellphone);}if (email != null && !email.trim().isEmpty()) {email = new String(email.getBytes("ISO-8859-1"), "utf-8");criteria.setEmail(email);}return criteria;}/*** 截取url /项目名/Servlet路径?参数字符串* * @param request* @return*/private String getUrl(HttpServletRequest request) {String contextPath = request.getContextPath();// 获取项目名String servletPath = request.getServletPath();// 获取servletPath,即/CustomerServletString queryString = request.getQueryString();// 获取问号之后的参数部份// 判断参数部份中是否包含pc这个参数,如果包含,需要截取下去,不要这一部份。if (queryString.contains("&pc=")) {int index = queryString.lastIndexOf("&pc=");queryString = queryString.substring(0, index);}return contextPath + servletPath + "?" + queryString;}}

以上内容是是小编给大家分享的JSP通用高大上分页代码(超管用),希望对大家有所帮助。


  • 上一条:
    JSP随机验证图片如何制作
    下一条:
    JSP对URL链接中的中文乱码处理方法总结
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在java中实现的脱敏工具类代码示例分享(0个评论)
    • zookeeper安装流程步骤(0个评论)
    • 在java中你背的“八股文”可能已经过时了(2个评论)
    • 在php8.0+版本中使用属性来增加值代码示例(3个评论)
    • java 正则表达式基础,实例学习资料收集大全 原创(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2018-03
    • 2020-03
    • 2023-05
    • 2023-11
    • 2024-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客