分页
0.导入依赖
一.easyui分页
1.controller
@RequestMapping(value = "/findAllRegion.action",produces = {"application/json;charset=UTF-8"})
public @ResponseBody
Map<String,Object> findAllRegion(String page ,String rows,Region region){
System.out.println(region.toString());
if (page==null){
page="1";
}
if (rows==null){
rows="1";
}
PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(rows), true);
List<Region> all = regionService.findAll(region);
PageInfo<Region> pi = new PageInfo<>(all);
List<Region> lists = pi.getList();
HashMap<String, Object> map = new HashMap<>();
map.put("total",pi.getTotal());
map.put("rows",lists);
return map;
}
2.jsp
//工具栏
var toolbar = [ {
id : 'button-edit',
text : '查询',
iconCls : 'icon-search',
handler : doView
}, {
id : 'button-add',
text : '增加',
iconCls : 'icon-add',
handler : doAdd
}, {
id : 'button-delete',
text : '删除',
iconCls : 'icon-cancel',
handler : doDelete
}, {
id : 'button-import',
text : '导入',
iconCls : 'icon-redo',
handler : doRedo
},{
id : 'button-export',
text : '导出',
iconCls : 'icon-undo',
handler : doExport
}];
// 定义列
var columns = [ [ {
field : 'id',
checkbox : true,
},{
field : 'province',
title : '省',
width : 120,
align : 'center'
}, {
field : 'city',
title : '市',
width : 120,
align : 'center'
}, {
field : 'district',
title : '区',
width : 120,
align : 'center'
}, {
field : 'postcode',
title : '邮编',
width : 120,
align : 'center'
}, {
field : 'shortcode',
title : '简码',
width : 120,
align : 'center'
}, {
field : 'citycode',
title : '城市编码',
width : 200,
align : 'center'
} ] ];
$(function(){
// 先将body隐藏,再显示,不会出现页面刷新效果
$("body").css({visibility:"visible"});
// 收派标准数据表格
$('#grid').datagrid( {
iconCls : 'icon-forward',
fit : true,
border : false,
//行号
rowNumbers : true,
striped : true,
pageList: [30,50,100],
//初始页数
pageNumber:1,
//是否分页
pagination : true,
//页面传值多少
pageSie:30,
toolbar : toolbar,
url : "${pageContext.request.contextPath}/findAllRegion.action",
idField : 'id',
columns : columns,
});
二. layui分页
1.controller
@ResponseBody
@RequestMapping(value = "/selectAllStaff.action",produces = {"application/json;charset=UTF-8"})
public Map<String,Object> findAllRegion(String page , String limit,Staff staff,HttpSession session){
if (page==null){
page="1";
}
if (limit==null){
limit="1";
}
PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit), true);
System.out.println(staff.toString());
List<Staff> all = staffService.selectAll(staff);
PageInfo<Staff> pi = new PageInfo<>(all);
List<Staff> lists = pi.getList();
HashMap<String, Object> map = new HashMap<>();
map.put("count",pi.getTotal());
map.put("data",lists);
map.put("code",0);
map.put("msg","");
return map;
}
2.jsp
<table class="layui-table" lay-data="{url:'${pageContext.request.contextPath}/selectAllStaff.action'
,page:true,toolbar: '#toolbarDemo',id:'test'}" lay-filter="test">
<thead>
<tr>
<th lay-data="{field:'id',type:'checkbox',fixed:'left'}">ID</th>
<th lay-data="{field:'name', width:120, sort: true, edit: 'text'}">姓名</th>
<th lay-data="{field:'password', width:120,sort: true, edit: 'text'}">密码</th>
<th lay-data="{field:'idcard', width:120, sort: true, edit: 'text'}">身份证号</th>
<th lay-data="{field:'birthday', width:120, edit: 'text',sort: true}">年龄</th>
<th lay-data="{field:'joindate', width:120, edit: 'text',sort: true}">入职时间</th>
<th lay-data="{field:'phone', width:120, edit: 'text',sort: true}">联系电话</th>
<th lay-data="{field:'type', width:120, edit: 'text',sort: true}">类型</th>
</tr>
</thead>
</table>
三. Mybatis分页
1.PageBean (domain实体类)
package com.tjetc.domain;
import java.util.List;
public class PageBean<T> {
private Integer nowpage;
private Integer eachpage;
private Integer totalnews;
private Integer totalpages;
private String url;
private List<T> lists;
@Override
public String toString() {
return "PageBean{" +
"nowpage=" + nowpage +
", eachpage=" + eachpage +
", totalnews=" + totalnews +
", totalpages=" + totalpages +
", url='" + url + '\'' +
", lists=" + lists +
'}';
}
public PageBean() {
}
public PageBean(Integer nowpage, Integer eachpage, Integer totalnews, Integer totalpages, String url, List<T> lists) {
this.nowpage = nowpage;
this.eachpage = eachpage;
this.totalnews = totalnews;
this.totalpages = totalpages;
this.url = url;
this.lists = lists;
}
public Integer getNowpage() {
return nowpage;
}
public void setNowpage(Integer nowpage) {
this.nowpage = nowpage;
}
public Integer getEachpage() {
return eachpage;
}
public void setEachpage(Integer eachpage) {
this.eachpage = eachpage;
}
public Integer getTotalnews() {
return totalnews;
}
public void setTotalnews(Integer totalnews) {
this.totalnews = totalnews;
}
public Integer getTotalpages() {
return totalpages;
}
public void setTotalpages(Integer totalpages) {
this.totalpages = totalpages;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public List<T> getLists() {
return lists;
}
public void setLists(List<T> lists) {
this.lists = lists;
}
}
2.PageBean (Servlet)
@WebServlet("/find.do")
public class FindServlet extends HttpServlet {
EmpService empService = new EmpServiceImpl();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//直接访问 tpye null key null
String type = request.getParameter("type");
String key = request.getParameter("key");
if(type==null||"".equals(type)){
type="0";
}
String nowpage = request.getParameter("nowpage");
if(nowpage==null||"".equals(nowpage)){
nowpage="1";
}
Integer eachpage = 4;
PageBean<Emp> page = empService.page(Integer.valueOf(nowpage), eachpage, Integer.valueOf(type), key);
String url = request.getRequestURI()+"?"+(request.getQueryString()==null?"":request.getQueryString());
if(url.indexOf("&nowpage")>-1){
url = url.substring(0,url.indexOf("&nowpage"));
}
System.out.println(url);
page.setUrl(url);
request.setAttribute("pb",page);
request.getRequestDispatcher("/success.jsp").forward(request,response);
}
}
3.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: sun
Date: 2020/6/11
Time: 9:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
<body>
<center>
<form action="${pageContext.request.contextPath}/find.do" method="get">
<select name="type">
<option VALUE="0">请选择类别</option>
<option value="1">员工姓名</option>
<option value="2">员工编号</option>
</select>
<INPUT type="text" name="key"><input type="submit" value="搜索">
</form>
</center>
<hr>
<FORM ACTION="${pageContext.request.contextPath}/delete.do" method="post">
<table align="center" border="1" style="width: 800px">
<tr>
<td>选中</td>
<td>员工编号</td>
<td>员工姓名</td>
<td>职位</td>
<td>领导编号</td>
<td>入职时间</td>
<td>薪水</td>
<td>奖金</td>
<td>部门编号</td>
<td>修改</td>
</tr>
<c:forEach items="${pb.lists}" var="emp">
<tr>
<td><INPUT TYPE="checkbox" name="ids" value="${emp.empno}"></td>
<td>${emp.empno}</td>
<td>${emp.ename}</td>
<td>${emp.job}</td>
<td>${emp.mgr}</td>
<td>${emp.hiredate}</td>
<td>${emp.sal}</td>
<td>${emp.comm}</td>
<td>${emp.deptno}</td>
<td><A href="${pageContext.request.contextPath}/update.jsp?empno=${emp.empno}">修改</A></td>
</tr>
</c:forEach>
</table>
<center> <nav aria-label="Page navigation">
<ul class="pagination">
<c:choose>
<c:when test="${pb.nowpage eq 1}">
<li>
<a class="disabled" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:when>
<c:otherwise>
<li>
<a href="${pb.url}&nowpage=${pb.nowpage-1}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:otherwise>
</c:choose>
<c:forEach begin="1" end="${pb.totalpages}" var="i" step="1">
<li><a href="${pb.url}&nowpage=${i}">${i}</a></li>
</c:forEach>
<li>
<c:choose>
<c:when test="${pb.nowpage eq pb.totalpages}">
<a class="disabled" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</c:when>
<c:otherwise>
<a href="${pb.url}&nowpage=${pb.nowpage+1}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</c:otherwise>
</c:choose>
</li>
</ul>
</nav></center>
<center><input type="submit" value="批量删除"></center>
</FORM>
</body>
</html>
4.资源列表
四.servlet 原始分页(无需依赖)
1.PageBean (domain实体类)
package domain;
import java.io.Serializable;
import java.util.List;
public class PageBean<T> implements Serializable {
//当前页
private Integer nowpage;
//总共有多少页
private Integer totalpage;
//总共有多少数据
private Integer count;
//每一页有多少个
private Integer eachpage;
//每一页的数据信息
private List<T> lists;
//路径
private String url;
public PageBean() {
}
public PageBean(Integer nowpage, Integer totalpage, Integer count, Integer eachpage, List<T> lists, String url) {
this.nowpage = nowpage;
this.totalpage = totalpage;
this.count = count;
this.eachpage = eachpage;
this.lists = lists;
this.url = url;
}
@Override
public String toString() {
return "PageBean{" +
"nowpage=" + nowpage +
", totalpage=" + totalpage +
", count=" + count +
", eachpage=" + eachpage +
", lists=" + lists +
", url='" + url + '\'' +
'}';
}
public Integer getNowpage() {
return nowpage;
}
public void setNowpage(Integer nowpage) {
this.nowpage = nowpage;
}
public Integer getTotalpage() {
return count%eachpage==0?count/eachpage:count/eachpage+1;
}
public void setTotalpage(Integer totalpage) {
this.totalpage = totalpage;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getEachpage() {
return eachpage;
}
public void setEachpage(Integer eachpage) {
this.eachpage = eachpage;
}
public List<T> getLists() {
return lists;
}
public void setLists(List<T> lists) {
this.lists = lists;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
2.PageBean (service)
public PageBean<Emp> pageFind(Integer nowpage, Integer eachpage);
service实现类
@Override
public PageBean<Emp> pageFind(Integer nowpage, Integer eachpage) {
PageBean<Emp> pb = new PageBean<>();
Integer count = empDao.getcount();
List<Emp> each = empDao.getEach(nowpage, eachpage);
pb.setCount(count);
pb.setLists(each);
pb.setNowpage(nowpage);
pb.setEachpage(eachpage);
return pb;
}
3.PageBean (Servlet)
@WebServlet(urlPatterns = "/findpage.do",initParams = {
@WebInitParam(name = "request.setCharacterEncoding",value ="utf-8")})
public class PageServlet extends HttpServlet {
EmpService empService=new EmpServiceImpl();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding(this.getServletConfig().getInitParameter("request.setCharacterEncoding"));
String nowpage = request.getParameter("nowpage");
if(nowpage==null||"".equals(nowpage)){
nowpage="1";
}
Integer eachpage=5;
PageBean<Emp> pb = empService.pageFind(Integer.valueOf(nowpage), eachpage);
//url
//请求参数
//S0521_Web_exploded/test.do
//System.out.println(request.getRequestURI());
String url = request.getRequestURI() + "?";
//返回包含在路径后的请求 URL 中的查询字符串。
String queryString = request.getQueryString();
if (!(queryString==null||"".equals(queryString))){
url+=queryString;
}
// http://localhost:8080/S0521_Web_exploded/findpage.do?nowpage=1
if (url.indexOf("nowpage")>-1){
// indexOf() 返回指定字符在字符串中第一次出现处的索引,
// 如果此字符串中没有这样的字符,则返回 -1。
url = url.substring(0, url.indexOf("nowpage"));
// substring(int beginIndex, int endIndex)
// beginIndex -- 起始索引(包括), 索引从 0 开始。
// endIndex -- 结束索引(不包括)。
}
pb.setUrl(url);
request.setAttribute("pb",pb);
request.getRequestDispatcher("/jsp/se.jsp").forward(request,response);
}
}
4.PageBean (jsp)
<%--
Created by IntelliJ IDEA.
User: mazhic
Date: 2020/5/21
Time: 17:44
To change this template use File | Settings | File Templates.
--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>emp分页+模糊查询</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/emp.css" type="text/css" />
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css" >
<h4 class="sign" >欢迎${user.username}登录</h4>
</head>
<body>
<div class="findemplike">
<form action="${pageContext.request.contextPath}/findemplike.do" method="get">
请输入关键词:<input type="text" style="width: 200px" name="key"
value="${key}">
<input type="submit" value="搜索"><br>
</form>
</div>
<form>
<table class="findemp">
<thead>
<tr>
<th>员工编号</th>
<th>员工姓名</th>
<th>职位</th>
<th>领导员工编号</th>
<th>入职时间</th>
<th>薪资</th>
<th>奖金</th>
<th>部门编号</th>
</tr>
</thead>
<tbody>
<%-- items="${pb.lists}--%>
<c:forEach items="${pb.lists}" var="empArrays">
<tr>
<td>${empArrays.EMPNO}</td>
<td>${empArrays.ENAME}</td>
<td>${empArrays.JOB}</td>
<td>${empArrays.MGR}</td>
<td>${empArrays.HIREDATE}</td>
<td>${empArrays.SAL}</td>
<td>${empArrays.COMM}</td>
<td>${empArrays.DEPTNO}</td>
</tr>
</c:forEach>
</tbody>
</table>
</form>
<center>
<nav aria-label="Page navigation">
<ul class="pagination">
<c:choose>
<c:when test="${pb.nowpage eq 1}">
<li>
<a class="disabled" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:when>
<c:otherwise>
<li>
<a href="${pb.url}nowpage=${pb.nowpage-1}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:otherwise>
</c:choose>
<c:forEach begin="1" end="${pb.totalpage}" step="1" var="i">
<li><a href="${pb.url}nowpage=${i}">${i}</a></li>
</c:forEach>
<c:choose>
<c:when test="${pb.nowpage eq pb.totalpage}">
<li>
<a class="disabled" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:when>
<c:otherwise>
<li>
<a href="${pb.url}nowpage=${pb.nowpage+1}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:otherwise>
</c:choose>
</ul>
</nav>
</center>
</body>
</html>
5.资源列表