闽公网安备 35020302035485号
分页功能是我们日常开发中最常见的功能,但是很多初学者对于这种最为常见的分页功能却不知道该如何实现,今天我们就把我们实际项目中用到的分页代码分享出来,希望对于那些还不知道如何写分页代码的初学者有所帮助。
分页被引用页面 standard.jsp
<!-- 分页在底端被引用页面 -->
<!-- standard.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
总 ${page.totalCount} 条
每页 ${page.pageSize} 条
总 ${page.pageCount} 页
第 ${page.pageNow} 页
<c:if test="${page.pageNow==1}">首页</c:if>
<c:if test="${page.pageNow!=1}"><a href="${pageContext.request.contextPath}${page.pageURL}?${page.fuSeaPara}&page.pageNow=1&page.pageSize=${page.pageSize}">首页</a></c:if>
<c:if test="${page.pageNow==1}">上一页</c:if>
<c:if test="${page.pageNow!=1}"><a href="${pageContext.request.contextPath}${page.pageURL}?${page.fuSeaPara}&page.pageNow=${page.pageNow-1}&page.pageSize=${page.pageSize}">上一页</a></c:if>
第<select onchange="location.href='${pageContext.request.contextPath}${page.pageURL}?${page.fuSeaPara}&page.pageNow='+this.value">
<c:forEach var="selectvalue" begin="1" end="${page.pageCount}" step="1">
<option value="${selectvalue}" ${page.pageNow eq selectvalue ?"selected='selected'":""}>
${selectvalue}
</option>
</c:forEach>
</select>
页
<c:if test="${page.pageNow==page.pageCount}">下一页</c:if>
<c:if test="${page.pageNow!=page.pageCount}"><a class="nextPage" href="${pageContext.request.contextPath}${page.pageURL}?${page.fuSeaPara}&page.pageNow=${page.pageNow+1}&page.pageSize=${page.pageSize}" >下一页</a></c:if>
<c:if test="${page.pageNow==page.pageCount}">尾页</c:if>
<c:if test="${page.pageNow!=page.pageCount}"><a href="${pageContext.request.contextPath}${page.pageURL}?${page.fuSeaPara}&page.pageNow=${page.pageCount}&page.pageSize=${page.pageSize}">尾页</a></c:if>
每页显示
<select onchange="location.href='${pageContext.request.contextPath}${page.pageURL}?${page.fuSeaPara}&page.pageSize='+this.value">
<!-- 总数如果小于15 显示 总数页,如果大于15 显示15 -->
<c:forEach var="selectvalue" begin="1" end="${page.totalCount<15?page.totalCount:15}" step="1">
<option value="${selectvalue}" ${page.pageSize eq selectvalue ?"selected='selected'":""}>
${selectvalue}
</option>
</c:forEach>
</select>
条
分页类专用PagingMS.javapackage com.auth.util;
import java.util.ArrayList;
import java.util.List;
/*
* author duidaima.com
* 2023.03.09
*
* */
//MySql 下通用 分页类
public class PagingMS {
int pageSize; //每页条目数 limit 第二个参数,查询多少条
int totalCount; //总条目数
int pageCount; //总页数
int pageNow; //当前页码
int startNum; //limit 第一个参数从第几条开始查
String fuSeaPara; // 模糊查询参数存储 ,下一页 的参数
String pageURL; //为满足 分页页面复用性 跳不同的action
private List<?> resultList; //返回数据库分页查询 结果集
public String getFuSeaPara() {
return fuSeaPara;
}
public void setFuSeaPara(String fuSeaPara) {
this.fuSeaPara = fuSeaPara;
}
public String getPageURL() {
return pageURL;
}
public void setPageURL(String pageURL) {
this.pageURL = pageURL;
}
public int getPageSize() {
//防止查出总条目为0,<5 ,系统会把 totalCount为0 赋值给 pageSize,而在求总页数 pageCount 是 pageSize会作为分母 ,系统报错。
if(pageSize==0){
pageSize=1;
}
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
//通过参数计算每页的条目数
public int calcPageSize(String ps){
if(null==ps){
//总条数大于5 ,默认5,小于5,就是 总条数,不写的话,在总条数小于5 ,页面会显示每页显示1条
//return 5;
return this.getTotalCount()>5?5:this.getTotalCount();
}
else return Integer.parseInt(ps);
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getPageCount() {
return totalCount%getPageSize()==0?totalCount/getPageSize():totalCount/getPageSize()+1;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
//计算当前页
public int calcPageNow(String pn){
if(null==pn){
return 1;
}
else return Integer.parseInt(pn);
}
public int getStartNum() {
return (getPageNow()-1)*getPageSize();
}
public void setStartNum(int startNum) {
this.startNum = startNum;
}
public List<?> getResultList() {
return resultList;
}
public void setResultList(List<?> resultList) {
this.resultList = resultList;
}
@Override
public String toString() {
return "PagingMS [pageSize=" + pageSize + ", totalCount=" + totalCount
+ ", pageCount=" + pageCount + ", pageNow=" + pageNow
+ ", startNum=" + startNum + ", resultList=" + resultList + "]";
}
}
Controllerpackage com.auth.controller;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.druid.util.StringUtils;
import com.auth.entity.CheckBatch;
import com.auth.entity.Store;
import com.auth.entity.StoreCheck;
import com.auth.entity.UserInfo;
import com.auth.service.CheckProductService;
import com.auth.service.StoreService;
import com.auth.util.PagingMS;/*
* author 堆代码 duidaima.com
* data 2023.03.15
*
*
* */
//全查所有盘点表并 分页显示
@RequestMapping("/showAllStoreCheListByPaging")
public ModelAndView showAllStoreCheListByPaging(HttpServletRequest request,HttpServletResponse response){
System.out.println("进入分页显示全部盘点表");
//下拉列表显示所有 盘点批次 ,利用模糊查询,hsm2为空,进行全查。
HashMap hsm2=new HashMap();
List <CheckBatch> CheckBatchList=cps.likeFindCheckBatchByPagingSer(hsm2);
request.setAttribute("CheckBatchListEL", CheckBatchList);
PagingMS pgm=new PagingMS();
int scCount=cps.findAllStoreCheckCountSer();
pgm.setTotalCount(scCount);
//每页条目数 ,limit 第2个参数
String ps=request.getParameter("page.pageSize");
//给对象赋 每页显示条数
pgm.setPageSize(pgm.calcPageSize(ps));
//当前页数目
String pn=request.getParameter("page.pageNow");
//赋当前页值
pgm.setPageNow(pgm.calcPageNow(pn));
//为满足 分页页面复用性,设置
String pageURL="/checkbatch/showAllStoreCheListByPaging.action";
pgm.setPageURL(pageURL);
HashMap hsm=new HashMap();
hsm.put("startNumHKey", pgm.getStartNum());
hsm.put("pageSizeHKey", pgm.getPageSize());
pgm.setResultList(cps.findAllStoreCheckByPagingSer(hsm));
System.out.println("pgm "+pgm);
ModelAndView mav = new ModelAndView();
mav.addObject("page", pgm);
//xml有配置 所以 可以 pages 也不能加 .jsp
mav.setViewName("storeCheck-list");
return mav;
}
总结:
以上就是基于jsp技术的分页功能代码实现,希望对大家有所帮助。