创建一个分页对象PageBean<T>来存储分页信息+实体信息,

客户端请求时传递分页信息,

服务端将实体信息+分页信息放进分页对象返回给客户端。

实例如下:

  listStudent.jsp

<%@page import="com.pojo.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
</style>
</head>
<script type="text/javascript">
function del(id){
if(confirm("是否确定要删除该数据?")){
window.location.href="updateStu?type=del&id="+id;
}
} function toPage(index){
window.location.href="queryStu?index="+index;
}
</script> <body>
<div align="center">
<table style="width: 500px;" border="1">
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>生日</th>
<th>专业</th>
<th>操作</th>
</tr>
<c:forEach items="${pb.list}" var="stu">
<tr>
<td>${stu.stuId}</td>
<td>${stu.stuName }</td>
<td>${stu.stuAge}</td>
<td>
<c:if test="${stu.stuSex=='1' }">男</c:if>
<c:if test="${stu.stuSex=='2' }">女</c:if>
</td>
<td><fmt:formatDate value="${stu.stuDate }" pattern="yyyy-MM-dd"/></td>
<td>${stu.showStuProfess}</td>
<td><a href="javascript:del('${stu.stuId }')">删除</a>
<a href="updateStu?type=toupdate&id=${stu.stuId }">修改</a></td>
</tr>
</c:forEach>
<tr>
<td colspan="7" align="center">
<a href="queryStu?index=1">首页</a>
<a <c:if test="${pb.pageIndex>1 }"> href="queryStu?index=${pb.pageIndex-1 }"</c:if>>上一页</a>
<a <c:if test="${pb.pageIndex<pb.maxPage }"> href="queryStu?index=${pb.pageIndex+1 }"</c:if>>下一页</a>
<a href="queryStu?index=${pb.maxPage }">末页</a>
跳转到<select onchange="toPage(this.value)">
<c:forEach begin="1" end="${pb.maxPage }" varStatus="status">
<option <c:if test="${pb.pageIndex==status.index}">selected="selected"</c:if> >${status.index}</option>
</c:forEach>
</select>
总共${pb.maxPage }页
</td>
</tr>
</table>
</div>
</body>
</html>

  PageBean.java

package com.pojo;
/**
* @author allen
* 分页对象
* @param <T> 表示该分页对象可以针对任何数据
*/ import java.util.List; public class PageBean<T> {
int pageIndex;//当前第几页
int pageCount;//每页显示的条数
int totalCount;//数据的总量
int maxPage;//最大页数
List<T> list;
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getMaxPage() {
return maxPage;
}
public void setMaxPage(int maxPage) {
this.maxPage = maxPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
StudentService.java
package com.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List; import com.dao.IStudentDAO;
import com.dao.StudentDAO;
import com.pojo.PageBean;
import com.pojo.Student; public class StudentService implements IStudentService {
IStudentDAO stuDAO = new StudentDAO(); @Override
public int addStu(String stuname, String stusex, String stuage, String studate, String stuprofess) {
Student stu = new Student();
stu.setStuName(stuname);
stu.setStuSex(stusex);
stu.setStuAge(Integer.valueOf(stuage));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
stu.setStuDate(sdf.parse(studate));
} catch (ParseException e) {
e.printStackTrace();
}
stu.setStuProfess(stuprofess);
return stuDAO.saveStu(stu);
} @Override
public List<Student> getAllStu() {
return stuDAO.queryStu("select * from student");
} @Override
public void delStu(String id) {
stuDAO.delStu(id);
} @Override
public Student getStuById(String id) {
List<Student> list = stuDAO.queryStu("select * from student where stuid='" + id + "'");
if (list != null && list.size() > 0) {
return list.get(0);
}
return null;
} @Override
public void updateStu(String stuid, String stuname, String stusex, String stuage, String studate,
String stuprofess) {
Student stu = new Student();
stu.setStuId(Integer.valueOf(stuid));
stu.setStuName(stuname);
stu.setStuSex(stusex);
stu.setStuAge(Integer.valueOf(stuage));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
stu.setStuDate(sdf.parse(studate));
} catch (ParseException e) {
e.printStackTrace();
}
stu.setStuProfess(stuprofess);
stuDAO.updateStu(stu);
} @Override
public PageBean<Student> queryStudentPageByIndex(int index, int pageCount, String sql) {
PageBean<Student> pb = new PageBean<>();
pb.setPageIndex(index);
pb.setPageCount(pageCount);
pb.setTotalCount(stuDAO.getCountBySql(sql));
int maxPage = pb.getTotalCount() % pageCount == 0 ? pb.getTotalCount() / pageCount
: (pb.getTotalCount() / pageCount) + 1;
pb.setMaxPage(maxPage);
sql = "select * from (select a.*,rownum num_ from ("+sql+") a) b where b.num_>="+((index-1)*pageCount+1)+" and b.num_<="+index*pageCount;
pb.setList(stuDAO.queryStu(sql));
return pb;
}
}
QueryStudentServlet.java
package com.control;

import java.io.IOException;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.pojo.PageBean;
import com.pojo.Student;
import com.service.IStudentService;
import com.service.StudentService;
@WebServlet("/queryStu")
public class QueryStudentServlet extends HttpServlet{
IStudentService stuSer = new StudentService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pageIndex = req.getParameter("index");
int index = 0;
if(pageIndex==null){
index = 1;
}else{
index = Integer.valueOf(pageIndex);
} //1.调用业务查询数据 分页显示
PageBean<Student> pb = stuSer.queryStudentPageByIndex(index, 5, "select * from student");
//2.将数据保存到request中传递到页面
req.setAttribute("pb", pb);
//3.转发页面
req.getRequestDispatcher("student/listStudent.jsp").forward(req, resp);
}
}

11-page分页原理的更多相关文章

  1. 在pycharm中批量插入表数据、分页原理、cookie和session介绍、django操作cookie

    昨日内容回顾 ajax发送json格式数据 ''' 1. urlencoded 2. form-data 3. json ''' 1. ajax $.ajax({ data: JSON.stringi ...

  2. php分页原理教程及简单实例

    <?php //连接数据库 $con = mysql_connect("localhost","root",""); mysql_se ...

  3. Bootstrap入门(十七)组件11:分页与标签

    Bootstrap入门(十七)组件11:分页与标签   1.默认样式的分页 2.分页的大小 3.禁用的分页 4.翻页的效果 5.两端对齐的分页 6. 标签的不同样式 7. 标签的大小   先引入本地的 ...

  4. amazeui学习笔记--css(常用组件11)--分页Pagination

    amazeui学习笔记--css(常用组件11)--分页Pagination 一.总结 1.分页使用:还是ul包li的形式: 分页组件,<ul> / <ol> 添加 .am-p ...

  5. page分页问题,根据页码获取对应页面的数据,接口调用

    添加一个log.js文件,进行接口调用. import axios from '@/libs/api.request' const MODULE_URL = '/log'; export const ...

  6. layui -page 分页类

    <?phpnamespace page; // +---------------------------------------------------------------------- / ...

  7. 七.数据分页原理,paginator与page对象

    1.分页: Paginator对象 Page对象 2.Paginator: class Paginator(object_list, per_page, orphans=0, allow_empty_ ...

  8. Android Launcher分析和修改11——自定义分页指示器(paged_view_indicator)

    Android4.0的Launcher自带了一个简单的分页指示器,就是Hotseat上面那个线段,这个本质上是一个ImageView利用.9.png图片做,效果实在是不太美观,用测试人员的话,太丑了. ...

  9. Oracle 分页原理

    oracle rownum 及分页处理的使用方法 在实际应用中我们经常碰到这样的问题,比如一张表比较大,我们只要其中的查看其中的前几条数据,或者对分页处理数据.在这些情况下我们都需要用到rownum. ...

  10. mysql分页原理和高效率的mysql分页查询语句

    该博来自网络转载!!!供自己学习使用!!! 以前我在mysql中分页都是用的 limit 100000,20这样的方式,我相信你也是吧,但是要提高效率,让分页的代码效率更高一些,更快一些,那我们又该怎 ...

随机推荐

  1. 使用mpvue开发小程序教程(二)

    在上篇文章中,我们介绍了使用mpvue开发小程序所需要的一些开发环境的搭建,并创建了第一个mpvue小程序代码骨架并将其运行起来.在本文中,我们来研究熟悉一下mpvue项目的主要目录和文件结构. 在V ...

  2. 解决Springboot 的ajax跨域问题-动静分离

    @SpringBootApplication public class FsSysApiApp { public static void main(String[] args) { SpringApp ...

  3. [深度思考]·为什么CNN是同步(并行)而RNN是异步(串行)的呢?

    为什么CNN是同步(并行)而RNN是异步(串行)的呢? 个人主页--> https://xiaosongshine.github.io/ 1.问题引出 CNN与RNN都是采用parameter ...

  4. Asp.net Core 使用Jenkins + Dockor 实现持续集成、自动化部署(二):部署

    前面又是废话 我之前写过: Asp.Net Core 程序部署到Linux(centos)生产环境(一):普通部署 Asp.Net Core 程序部署到Linux(centos)生产环境(二):doc ...

  5. flag.xls

    Topic Link http://ctf5.shiyanbar.com/misc/flag.xls 1)  打开表格发现需要密码 但是flag又在表里,但我们可以改他的格式为.txt文本,因为这样就 ...

  6. ansible学习系列2-ansible常用模块使用

    1. 查看支持的模块 [root@localhost ~]# ansible-doc -l 这里我们看下ansible的支持的模块个数 [root@localhost ~]# ansible-doc ...

  7. Docker系列09—Docker的系统资源限制及验证

    本文收录在容器技术学习系列文章总目录 1.限制容器的资源 默认情况下,容器没有资源限制,可以使用主机内核调度程序允许的尽可能多的给定资源.Docker提供了控制容器可以使用多少内存或CPU的方法,设置 ...

  8. Spring Boot 系列总目录

    一.Spring Boot 系列诞生原因 上学那会主要学的是 Java 和 .Net 两种语言,当时对于语言分类这事儿没什么概念,恰好在2009年毕业那会阴差阳错的先找到了 .Net 的工作,此后就开 ...

  9. 【转】NotificationCopat.Builder全部设置

    1.方法:setContentTitle(CharSequence title)   功能:设置通知栏标题.   例子:setContentTitle("测试标题"). 2.方法: ...

  10. json 按照字段分类

    let arr = [ { Category:'A', Amount:, },{ Category:'B', Amount:, },{ Category:'A', Amount:, },{ Categ ...