11-page分页原理
创建一个分页对象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分页原理的更多相关文章
- 在pycharm中批量插入表数据、分页原理、cookie和session介绍、django操作cookie
昨日内容回顾 ajax发送json格式数据 ''' 1. urlencoded 2. form-data 3. json ''' 1. ajax $.ajax({ data: JSON.stringi ...
- php分页原理教程及简单实例
<?php //连接数据库 $con = mysql_connect("localhost","root",""); mysql_se ...
- Bootstrap入门(十七)组件11:分页与标签
Bootstrap入门(十七)组件11:分页与标签 1.默认样式的分页 2.分页的大小 3.禁用的分页 4.翻页的效果 5.两端对齐的分页 6. 标签的不同样式 7. 标签的大小 先引入本地的 ...
- amazeui学习笔记--css(常用组件11)--分页Pagination
amazeui学习笔记--css(常用组件11)--分页Pagination 一.总结 1.分页使用:还是ul包li的形式: 分页组件,<ul> / <ol> 添加 .am-p ...
- page分页问题,根据页码获取对应页面的数据,接口调用
添加一个log.js文件,进行接口调用. import axios from '@/libs/api.request' const MODULE_URL = '/log'; export const ...
- layui -page 分页类
<?phpnamespace page; // +---------------------------------------------------------------------- / ...
- 七.数据分页原理,paginator与page对象
1.分页: Paginator对象 Page对象 2.Paginator: class Paginator(object_list, per_page, orphans=0, allow_empty_ ...
- Android Launcher分析和修改11——自定义分页指示器(paged_view_indicator)
Android4.0的Launcher自带了一个简单的分页指示器,就是Hotseat上面那个线段,这个本质上是一个ImageView利用.9.png图片做,效果实在是不太美观,用测试人员的话,太丑了. ...
- Oracle 分页原理
oracle rownum 及分页处理的使用方法 在实际应用中我们经常碰到这样的问题,比如一张表比较大,我们只要其中的查看其中的前几条数据,或者对分页处理数据.在这些情况下我们都需要用到rownum. ...
- mysql分页原理和高效率的mysql分页查询语句
该博来自网络转载!!!供自己学习使用!!! 以前我在mysql中分页都是用的 limit 100000,20这样的方式,我相信你也是吧,但是要提高效率,让分页的代码效率更高一些,更快一些,那我们又该怎 ...
随机推荐
- JavaScriptCore全面解析
本文由云+社区发表 作者:殷源,专注移动客户端开发,微软Imagine Cup中国区特等奖获得者 JavaScript越来越多地出现在我们客户端开发的视野中,从ReactNative到JSpatch, ...
- [二]基础数据类型之Long详解
Long Long 基本数据类型long 的包装类 Long 类型的对象包含一个 long类型的字段 属性简介 值为 263-1 的常量,它表示 long 类型能够表示的最大值 ...
- MVC Scaffolding SmartCode-Engine 更新
概述 通过扩展visual studio.net scaffolding组件,添加了一套功能完善的代码模板,包括Controller,Model,View,Businessd等各种功能的代码,配合En ...
- linux系统管理--top命令
这些日子,运维公司三台服务器,一个小伙伴貌似对top命令不太感冒,我告诉他去百度(不懂谷歌百度的程序员不是好厨师),然后突然发现也许我自己该整理一下相关的东西了 top命令是Linux下常用的性能分析 ...
- Java_设计模式之享元模式
1.关于享元模式 享元模式有点类似于单例模式,都是只生成一个对象被共享使用.享元模式主要目的就是让多个对象实现共享,减少不会要额内存消耗,将多个对同一对象的访问集中起来,不必为每个访问者创建一个单独的 ...
- Go开发之路(目录)
知识点 1. Go语言 简介 2. Go语言 基本语法 3. Go语言 strings以及strconv的使用 4. Go语言 时间和日期类型 5. Go语言 指针类型 6. Go语言 流程控制 7. ...
- CSS像素、物理像素、逻辑像素、设备像素比、PPI、Viewport
1.PX(CSS pixels) 1.1 定义 虚拟像素,可以理解为“直觉”像素,CSS和JS使用的抽象单位,浏览器内的一切长度都是以CSS像素为单位的,CSS像素的单位是px. 1.2 注意 在CS ...
- 使用bfd监控静态路由,达到网络故障及时切换功能。
结论:通过BFD可以联动静态路由,从而监控整个网络上的网络情况,当出现故障时及时进行切换. 下面的例子,就是通过BFD监控上面的这个往返路由,当中间网络出现故障时,两端全部切换到下面的第二条路由进行通 ...
- QT获取本机IP和Mac地址
#include <QNetworkInterface> #include <QList> void MainWindow::getIPPath() { QString str ...
- WinServer-FTP搭建
FTP服务器(File Transfer Protocol Server)是在互联网上提供文件存储和访问服务的计算机,它们依照FTP协议提供服务. FTP是File Transfer Protocol ...