创建一个分页对象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. 知其所以然~mongodb副本集

    MongoDB 复制(副本集) MongoDB复制是将数据同步在多个服务器的过程. 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性. 复制还允许您 ...

  2. shell实战之tomcat看门狗

    1.脚本简介 tomcat看门狗,在tomcat进程异常退出时会自动拉起tomcat进程并记录tomcat运行的日志. 函数说明: log_info:打印日志的函数,入参为需要在日志中打印的msg s ...

  3. 从0打卡leetcode之day 6--最长回文串

    题目描述 给定一个字符串 s,找到 s中最长的回文子串.你可以假设 s 的最大长度为1000. 示例1 输入: "babad" 输出: "bab" 注意: &q ...

  4. Chapter 5 Blood Type——4

    "Does he mean you?" Jessica asked with insulting astonishment in her voice. “他对你有意思吗?”Jess ...

  5. 基于 Zookeeper 的分布式锁实现

    1. 背景 最近在学习 Zookeeper,在刚开始接触 Zookeeper 的时候,完全不知道 Zookeeper 有什么用.且很多资料都是将 Zookeeper 描述成一个“类 Unix/Linu ...

  6. 【Java基础】【12String类】

    12.01_常见对象(Scanner的概述和方法介绍)(掌握) A:Scanner的概述 B:Scanner的构造方法原理 Scanner(InputStream source) System类下有一 ...

  7. springboot+mybatis+dubbo+aop日志第二篇

    本篇主要介绍dubbo-demo-api接口层和dubbo-demo-service层,以及如何通过dubbo把服务发布出去,介绍代码前,咱们先来回顾一下整个demo工程的结构,如下图所示: 1.du ...

  8. Java递归读取文件路径下所有文件名称并保存为Txt文档

    本文用递归的方法实现读取一个路径下面的所有文件并将文件名称保存到Txt文件中,亲测可用. 递归读取文件路径下的所有文件: /** * 递归读取文件路径下的所有文件 * * @param path * ...

  9. 基本 SQL 之增删改查(二)

    上一篇文章中,我们介绍了 SQL 中最基本的 DML 语法,包括 insert 的插入数据.update 的更新数据.delete 的删除数据以及基本的查询语法,但大多比较简单不能解决我们日常项目中复 ...

  10. angularjs1.X进阶笔记(3)——如何重构controller

    目录 一. 结构拆分 二.基本代码优化 本篇是内部培训交流会的摘要总结. 培训PPT和示例代码已托管至我的github仓库: https://github.com/dashnowords/blogs/ ...