Java(基于Struts2) 分页实现代码
发布时间:2023-11-04 05:58:58
分页实现的基本过程是这样的:
1. 设置自己的分页器的基本参数(可以从配置文件中读取)
■每页显示的记录条数
■每次最多显示多少页
2. 编写设置分页器其他参数的函数
主要参数有以下几个:
总记录条数
总页数
当前页号:现在显示的页数
每页显示的记录条数
当前页开始行(第一行是0行)
第一页页号
最后页页号
下一页页号
上一页页号
画面上显示的起始页号
画面上显示的结束页号
参数基本实现原理:设置以上各个参数,实际上只需要三个参数就可以对所有的其他变量进行设置,即总记录条数,每页显示记录数,每次最多显示多少页。
分页器的代码实现如下(省略get,set函数):
Page.java
{
this.onePageSize = Integer.valueOf(PageResource.get(PageResource.ONE_PAGE_SIZE));
this.displayPageCount = Integer.valueOf(PageResource.get(PageResource.DISPLAY_PAGE_COUNT)) - 1;
}
/** 页号式导航, 最多显示页号数量为displayPageCount+1 */
private int displayPageCount;
/** 每页显示的记录条数 */
private int onePageSize;
/** 总记录条数 */
private int totalRecord;
/** 总页数 */
private int totalPage;
/** 当前页号 */
private int currentPageNum = 1;
/** 当前页开始行(第一行是0行) */
private int currentStartRow;
/** 第一页页号 */
private int firstPageNum = 1;
/** 最后页页号 */
private int lastPageNum;
/** 下一页页号 */
private int nextPageNum;
/** 上一页页号 */
private int prevPageNum;
/** 页号式导航 起始页号 */
private int startPageNum;
/** 页号式导航 结束页号 */
private int endPageNum;
/**
*
* @param onePageSize
* @param currentPageNum
* @param totalRecord
*/
public Page(int totalRecord) {
this.totalRecord = totalRecord;
this.setPageInfo();
}
public Page() {
}
public void setPageInfo() {
this.totalPage = (totalRecord + onePageSize - 1) / onePageSize;
this.currentPageNum = Math.max(1, Math.min(currentPageNum, totalPage));
this.lastPageNum = this.totalPage;
this.nextPageNum = Math.min(this.totalPage, this.currentPageNum + 1);
this.prevPageNum = Math.max(1, this.currentPageNum - 1);
// 分页控制信息
this.currentStartRow = (this.currentPageNum - 1) * onePageSize;
startPageNum = Math.max(this.currentPageNum - displayPageCount / 2,
firstPageNum);
endPageNum = Math.min(startPageNum + displayPageCount, lastPageNum);
if (endPageNum - startPageNum < displayPageCount) {
startPageNum = Math.max(endPageNum - displayPageCount, 1);
}
}
3. 编写前端代码(以Struts2为例)
当在前台点击各个跳转页面的链接时,只需要将要跳转到的页号和总页数传给后台,后台会重新更新分页器,进而实现页码的跳转。
<div>
<div>
总页数:
<s:property value="#request.p.totalPage" />
总记录数:
<s:property value="#request.p.totalRecord" />
</div>
<s:url id="firstURL" action="PageAction!toPage">
<s:param name="p.currentPageNum">
<s:property value="#request.p.firstPageNum" />
</s:param>
<s:param name="p.totalRecord">
<s:property value="#request.p.totalRecord" />
</s:param>
</s:url>
<s:a href="%{firstURL}">首页</s:a>
<s:url id="prev" action="PageAction!toPage">
<s:param name="p.currentPageNum">
<s:property value="#request.p.prevPageNum" />
</s:param>
<s:param name="p.totalRecord">
<s:property value="#request.p.totalRecord" />
</s:param>
</s:url>
<s:a href="%{prev}">上一页</s:a>
<s:bean name="org.apache.struts2.util.Counter" id="counter">
<s:param name="first" value="p.startPageNum" />
<s:param name="last" value="p.endPageNum" />
<s:iterator var="pageNum">
<s:if test="p.currentPageNum==#pageNum">
<s:property />
</s:if>
<s:else>
<s:url id="page" action="PageAction!toPage">
<s:param name="p.currentPageNum">
<s:property value="#pageNum" />
</s:param>
<s:param name="p.totalRecord">
<s:property value="#request.p.totalRecord" />
</s:param>
</s:url>
<s:a href="%{page}"><s:property /></s:a>
</s:else>
</s:iterator>
</s:bean>
<s:url id="next" action="PageAction!toPage">
<s:param name="p.currentPageNum">
<s:property value="#request.p.nextPageNum" />
</s:param>
<s:param name="p.totalRecord">
<s:property value="#request.p.totalRecord" />
</s:param>
</s:url>
<s:a href="%{next}">下一页</s:a>
<s:url id="lastURL" action="PageAction!toPage">
<s:param name="p.currentPageNum">
<s:property value="#request.p.lastPageNum" />
</s:param>
<s:param name="p.totalRecord">
<s:property value="#request.p.totalRecord" />
</s:param>
</s:url>
<s:a href="%{lastURL}">尾页</s:a>
</div>
猜你喜欢
- SpringBoot默认使用HikariDataSource数据源定义数据源:存储了所有建立数据库连接的信息。通过提供正确的数据源名称,你可
- JDK * :利用反射机制生成一个实现代理接口的匿名类,在调用具体方法前调用InvokeHandler来处理。CGlib * :利用AS
- 添加群机器人可以查看这篇文章:添加机器人到钉钉群 使用命令行工具curl快速验证自定义机器人是否可以正常工作。可以使用如下命令,把对应的链接
- 工作原理:Spring Cloud框架下的服务发现Eureka包含两个组件分别是: Eureka Server与Eureka ClientE
- 代码实例一 using System; using System.IO; using System.Collections; using S
- 项目概况:Spring Cloud搭的微服务,使用了eureka,FeignClient,现在遇到FeignClient调用接口时不支持上传
- 简介JSR-303 是 JAVA EE 6 中的一项子规范,叫做 Bean Validation。在任何时候,当你要处理一个应用程序的业务逻
- timer的schedule和scheduleAtFixedRate方法一般情况下是没什么区别的,只在某个情况出现时会有区别--当前任务没有
- 1.普通轮询算 * 询(Round Robin,RR)是依次将用户的访问请求,按循环顺序分配到web服务节点上,从1开始到最后一台服务器节点结
- SpringBoot线程池和Java线程池的用法和实现原理使用默认的线程池方式一:通过@Async注解调用public class Asyn
- Mybatis @SelectKey用法用处主要用来解决主键自增问题用法@SelectKey(statement="SELECT
- 前言之前在SpringBoot项目中简单使用定时任务,不过由于要借助cron表达式且都提前定义好放在配置文件里,不能在项目运行中动态修改任务
- 首先引入jar包:<dependency> <groupId>com.alibaba</grou
- 在java中,可以根据Class类的对象,知道某个类(接口)的一些属性(成员 ,方法,注释,注解)等。由于最近的工作中用到了这些,其中需要在
- 查询文档 & 基本操作为了方便学习, 本节中所有示例沿用上节的索引按照ID单个GET class_1/_doc/1查询结果:{ &n
- Maven打包没有指定主类在使用IDEA开发项目的时候经常会遇到使用Maven打包项目(打成jar包或者zip包),但是之前不太清楚打包插件
- 一、项目简述功能包括(管理员和用户角色): 酒店预订,酒店管理,员工管理,入住原理,订单管理, 楼层管理,退房管理,营业额报表等等。二、项目
- 前言入职新公司到现在也有一个月了,完成了手头的工作,前几天终于有时间研究下公司旧项目的代码。在研究代码的过程中,发现项目里用到了Spring
- 目录前言I. 项目环境1. 项目依赖2. 配置II. 邮件发送1. 简单文本邮件发送2. html发送3. 添加附件4. Freemaker
- WebFilter.javapackage com.hongyuan.route;import java.io.File;import ja