创建一个分页对象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. React Native (一) 入门实践

    上周末开始接触react native,版本为0.37,边学边看写了个demo,语法使用es6/7和jsx.准备分享一下这个过程.之前没有native开发和react的使用经验,不对之处烦请指出.笔者 ...

  2. 手把手教你如何优雅的使用Aop记录带参数的复杂Web接口日志

    前言 不久前,因为需求的原因,需要实现一个操作日志.几乎每一个接口被调用后,都要记录一条跟这个参数挂钩的特定的日志到数据库.举个例子,就比如禁言操作,日志中需要记录因为什么禁言,被禁言的人的id和各种 ...

  3. Chapter 4 Invitations——22

    "Are you going all by yourself?" he asked, and I couldn't tell if he was suspicious I had ...

  4. Jvm垃圾回收器(算法篇)

    在<Jvm垃圾回收器(基础篇)>中我们主要学习了判断对象是否存活还是死亡?两种基础的垃圾回收算法:引用计数法.可达性分析算法.以及Java引用的4种分类:强引用.软引用.弱引用.虚引用.和 ...

  5. 使用yum安装不知道到底安装在什么文件夹

    find /* >yum001    #记录之前的文件夹 find /* >yum002    #记录安装完成后的文件夹 diff yum001 yum002 >yum000     ...

  6. 痞子衡嵌入式:飞思卡尔i.MX RT系列MCU启动那些事(3)- Serial Downloader模式(sdphost/MfgTool)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是飞思卡尔i.MX RT系列MCU的Serial Downloader模式. 在上一篇文章 Boot配置(BOOT Pin, eFUSE) ...

  7. 【憩园】C#并发编程之概述

    写在前面 并发编程一直都存在,只不过过去的很长时间里,比较难以实现,随着互联网的发展,人口红利的释放,更加友好的支持并发编程已经成了主流编程语言的标配,而对于软件开发人员来说,没有玩过并发编程都会有点 ...

  8. Spring框架浅析

    一.一个简单的示例 1.引入依赖和配置 pom.xml <?xml version="1.0" encoding="UTF-8"?> <pro ...

  9. ioc初步理解(一) 简单实用autofac搭建mvc三层+ioc(codeFirst)

    1]首先搭好框架 1.1]搭建ui层 1.2]创建其他内库文件 整个项目基本部分搭建完毕之后如下 2]使用nuget引用文件 先在每一个项目中引入ef 然后再UI层引入以下两个文件autofac和Au ...

  10. [转]在node.js中,使用基于ORM架构的Sequelize,操作mysql数据库之增删改查

    本文转自:https://www.cnblogs.com/kongxianghai/p/5582661.html Sequelize是一个基于promise的关系型数据库ORM框架,这个库完全采用Ja ...