自定义分页工具类
程序员文章站
2022-05-26 15:25:36
...
自定义分页工具类
后台代码:
package com.bootdo.utils;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/******
* 分页工具类
*/
public class PageUtil {
/***
* 总数
*/
private Integer total;
/****
* 当前页的数据
*/
private List rows;
/****
* 总页数
*/
private Integer totalPages;
/******
* 当前页
*/
private Integer curPage=1;
/****
* 每页多少条
*/
private Integer pageSize=10;
/******
* 分页跳转地址
*/
private String linkUrl;
public Integer getTotal() {
return total;
}
/*****
*
* 当设置总数,可以算出总页数
* @param total 总页数
*/
public void setTotal(Integer total) {
if(totalPages==null){
if(total%this.getPageSize()==0){
totalPages=total/this.getPageSize();
}else{
totalPages=total/this.getPageSize()+1;
}
}
this.total = total;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
public Integer getCurPage() {
return curPage;
}
public void setCurPage(Integer curPage) {
if(curPage!=null){
this.curPage = curPage;
}
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
/****
* 得到偏移量
* 根据当前页和 每页多少得到 偏移量
* @return
*/
public Integer getOffset() {
return (this.getCurPage()-1)*pageSize;
}
/*****
* 得到总页数,根据总数 和每页多少条
* @return
*/
public Integer getTotalPages() {
if(totalPages==null){
if(this.getTotal()%this.getPageSize()==0){
totalPages=this.getTotal()/this.getPageSize();
}else{
totalPages=this.getTotal()/this.getPageSize()+1;
}
}
return totalPages;
}
/*****
* 得到分页的最后一页
* @return
*/
public Integer getLastNumber() {
Integer lastNumber = 0;
if(this.getTotal()<this.getCurPage()*this.getPageSize()){
lastNumber=this.getTotal();
}else{
lastNumber=this.getCurPage()*this.getPageSize();
}
return lastNumber;
}
public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}
/*****
* 分页初始化 嵌套 mybatis
* @param curPageObj 当前页
* @param pageSizeObj 每页多少条
*/
public Page getInstantiation (Object curPageObj, Object pageSizeObj){
try {
curPage = curPageObj==null?getCurPage():Integer.parseInt(curPageObj.toString());
pageSize = pageSizeObj==null?getPageSize():Integer.parseInt(pageSizeObj.toString());
}catch (NumberFormatException e){
throw new NumberFormatException(e.getMessage());
}
Integer pageIndex = (curPage-1)*pageSize;
PageHelper.clearPage();
Page page = PageHelper.offsetPage(pageIndex,pageSize);
return page;
}
/****
* 得到分页的参数
* @return
*/
public Map<String, Object> getPageParam() {
Map<String,Object> pageParam = new HashMap<>();
generatePageData(pageParam);
generatePageLinkUrl(pageParam);
return pageParam;
}
/*****
* 生成链接地址
* @param pageParam
*/
private void generatePageLinkUrl(Map<String,Object> pageParam){
Integer start = pageParam.get("start")==null?0:(Integer)pageParam.get("start");
Integer count = pageParam.get("count")==null?1:(Integer)pageParam.get("count");
pageParam.put("firstPage",getLinkUrl().replace("{{curPage}}","1"));
pageParam.put("lastPage",getLinkUrl().replace("{{curPage}}",getTotalPages().toString()));
pageParam.put("prvPage",getLinkUrl().replace("{{curPage}}",(getCurPage()-1)+""));
pageParam.put("nextPage",getLinkUrl().replace("{{curPage}}",(getCurPage()+1)+""));
List<Object> dataList = new ArrayList<>();
for(int i=start;i<=count;i++){
JSONObject jsonObject = new JSONObject();
jsonObject.put("linkUrl",getLinkUrl().replace("{{curPage}}",i+""));
jsonObject.put("value",i);
dataList.add(jsonObject);
}
pageParam.put("data",dataList);
}
/*****
* 生成页面数据
* @param pageParam
*/
private void generatePageData(Map<String,Object> pageParam){
Integer start = 1;
if(getTotalPages()>10){
if(getCurPage()>6){
start=getCurPage()-4;
}else{
start=1;
}
}else{
start=1;
}
Integer count=0;
if(getTotalPages()<10){
count=getTotalPages();
}else{
count=start+9;
}
pageParam.put("start",start);
pageParam.put("count",count);
}
public String getLinkUrl() {
return linkUrl;
}
public void setLinkUrl(String linkUrl) {
this.linkUrl = linkUrl;
}
}
前段代码:
<div th:fragment="page" >
<dl>
<dd th:if="${page.curPage gt 1}">
<a th:href="${page.pageParam.prvPage}" class="prv">
<i class="icon icon-angle-left"></i>
上一页
</a>
</dd>
<dd th:if="${page.pageParam.start > 2}">
<a th:href="${page.pageParam.firstPage}" class="page">1</a>
</dd>
<dd th:if="${page.pageParam.start > 2}">
<a href="#" class="page disable">
...
</a>
</dd>
<dd th:each="data:${page.pageParam.data}">
<a th:if="${page.curPage==data.value}" th:href="${data.linkUrl}" class="page active" th:text="${data.value}"></a>
<a th:if="${page.curPage!=data.value}" th:href="${data.linkUrl}" class="page" th:text="${data.value}"></a>
</dd>
<dd th:if="${page.curPage lt page.totalPages}">
<a th:href="${page.pageParam.nextPage}" class="next">
下一页
<i class="icon icon-angle-right"></i>
</a>
</dd>
</dl>
</div>
效果图
上一篇: struts2的struts.properties配置
下一篇: ssh 免密码登录