0.导入依赖
一.easyui分页
1.controller
@RequestMapping(value = "/findAllRegion.action",produces = {"application/json;charset=UTF-8"})
public @ResponseBody
Map 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 all = regionService.findAll(region);
PageInfo pi = new PageInfo<>(all);
List lists = pi.getList();
HashMap 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 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 all = staffService.selectAll(staff);
PageInfo pi = new PageInfo<>(all);
List lists = pi.getList();
HashMap map = new HashMap<>();
map.put("count",pi.getTotal());
map.put("data",lists);
map.put("code",0);
map.put("msg","");
return map;
}
2.jsp
ID
姓名
密码
身份证号
年龄
入职时间
联系电话
类型
三. Mybatis分页
1.PageBean (domain实体类)
package com.tjetc.domain;
import java.util.List;
public class PageBean {
private Integer nowpage;
private Integer eachpage;
private Integer totalnews;
private Integer totalpages;
private String url;
private List 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 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 getLists() {
return lists;
}
public void setLists(List 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 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" %>
Title
4.资源列表
四.servlet 原始分页(无需依赖)
1.PageBean (domain实体类)
package domain;
import java.io.Serializable;
import java.util.List;
public class PageBean implements Serializable {
//当前页
private Integer nowpage;
//总共有多少页
private Integer totalpage;
//总共有多少数据
private Integer count;
//每一页有多少个
private Integer eachpage;
//每一页的数据信息
private List lists;
//路径
private String url;
public PageBean() {
}
public PageBean(Integer nowpage, Integer totalpage, Integer count, Integer eachpage, List 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 getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
2.PageBean (service)
public PageBean pageFind(Integer nowpage, Integer eachpage);
service实现类
@Override
public PageBean pageFind(Integer nowpage, Integer eachpage) {
PageBean pb = new PageBean<>();
Integer count = empDao.getcount();
List 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 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" %>
emp分页+模糊查询
欢迎${user.username}登录
5.资源列表