目录

关于此文

  bootstrap是前端很流行的框架,正在开发的项目,用bootstrap搭建起来的页面,自我感觉很完美,终于告别了苦逼坑爹苍白无力的白花花的难看的……的页面了。哈哈。

现在遇到了bootstrap的分页与Java后台结合起来的分页封装问题,对于我这个Java菜鸟来说,包装分页还没玩过。故此,在网上找了这个。觉得很不错,所以决定记录到博客里面。

还没有实践,决定写完博客去实践。在上图。祝我成功吧!

此人的博客没找到,代码中有email地址。此文转自:super.wwz@hotmail.com

pagination

定义了分页常用的属性,方法

package com.app.pagination;
import java.util.List;
/**
* 通用分页接口
* @author: super.wwz@hotmail.com
* @ClassName: Pagination
* @Version: v0.1
* @param <T>
*/
public interface Pagination<T> {
/**
* 判断是否是首页
* @return
*/
boolean isFirst();
/**
* 判断是否是尾页
* @return
*/
boolean isLast();
/**
* 判断是否有上一页
* @return
*/
boolean isPrevious();
/**
* 判断是否有下一页
* @return
*/
boolean isNext(); /**
* 获取上一页的页码
* @return
*/
int getPreviousIndex();
/**
* 获取下一页的页码
* @return
*/
int getNextIndex();
/**
* 获取当前页码
* @return
*/
int getPageIndex();
/**
* 获取当前页大小
* @return
*/
int getPageSize();
/**
* 获取总页数
* @return
*/
int getTotalPages();
/**
* 获取数据总行数
* @return
*/
int getTotalElements(); /**
* 获取当前页的数据
* @return
*/
List<T> getCurrData(); /**
* 获取数字分页链接对象
* @return
*/
BetweenIndex getBetweenIndex(); /**
* 获取每页显示的分页链接数
* @return
*/
int getPageLinkNumber(); /**
* 设置每页的分页链接数量
* @param pageLinkNumber
*/
void setPageLinkNumber(int pageLinkNumber);
}

BetweenIndex

该接口负责获取分页链接的开始和结尾索引

package com.app.pagination;
/**
* 开始链接-结束链接
* @author: super.wwz@hotmail.com
* @ClassName: BetweenIndex
* @Version: v0.1
*/
public interface BetweenIndex {
/**
* 获取开始分页链接索引
* @return
*/
int getBeginIndex();
/**
* 获取结束分页链接索引
* @return
*/
int getEndIndex();
}

  

DefaultPagination

Pagination接口的默认实现类

package com.app.pagination.impl;
import java.util.List;
import com.app.pagination.BetweenIndex;
import com.app.pagination.Pagination;
/**
* Pagination接口默认实现
* @author: super.wwz@hotmail.com
* @ClassName: DefaultPagination
* @Version: v0.1
* @param <T>
*/
public class DefaultPagination<T> implements Pagination<T> {
private int totalElements;
private int pageSize;
private int totalPages;
private int pageIndex;
private QueryHandler<T> queryHandler;
private List<T> currData;
private int pageLinkNumber;
public DefaultPagination(int pageIndex, int pageSize, QueryHandler<T> queryHandler, int pageLinkNumber) {
this(pageIndex, pageSize, queryHandler);
setPageLinkNumber(pageLinkNumber);
}
public DefaultPagination(int pageIndex, int pageSize, QueryHandler<T> queryHandler){
//初始化数据访问回调接口
this.queryHandler = queryHandler;
//查询总行数
setTotalElements();
//修正页大小
setPageSize(pageSize);
//计算总页数:
setTotalPages();
//修正页码
setPageIndex(pageIndex);
//查询当前页数据
setCurrData();
}
private void setCurrData() {
// TODO Auto-generated method stub
this.currData = queryHandler.getCurrData(pageIndex, pageSize);
}
private void setPageIndex(int pageIndex) {
// TODO Auto-generated method stub
if(pageIndex < 1) {
this.pageIndex = 1;
} else if(pageIndex > totalPages) {
this.pageIndex = totalPages;
} else {
this.pageIndex = pageIndex;
}
}
private void setTotalPages() {
// TODO Auto-generated method stub
if(pageSize > 0) {
/*//普通算法:
this.totalPages = totalElements % pageSize == 0 ?
totalElements / pageSize : (totalElements / pageSize) + 1;*/
//减一公式:
this.totalPages = (totalElements + pageSize - 1) / pageSize;
}
}
private void setPageSize(int pageSize) {
// TODO Auto-generated method stub
if(pageSize < 1) {
this.pageSize = 1;
} else if(pageSize > totalElements) {
this.pageSize = totalElements;
} else {
this.pageSize = pageSize;
}
}
private void setTotalElements() {
// TODO Auto-generated method stub
this.totalElements = queryHandler.getTotalElements();
}
@Override
public boolean isFirst() {
// TODO Auto-generated method stub
return pageIndex == 1;
}
@Override
public boolean isLast() {
// TODO Auto-generated method stub
return pageIndex == totalPages;
}
@Override
public boolean isPrevious() {
// TODO Auto-generated method stub
return pageIndex > 1;
}
@Override
public boolean isNext() {
// TODO Auto-generated method stub
return pageIndex < totalPages;
}
@Override
public int getPreviousIndex() {
// TODO Auto-generated method stub
return isPrevious() ? pageIndex - 1 : 1;
}
@Override
public int getNextIndex() {
// TODO Auto-generated method stub
return isNext() ? pageIndex + 1 : totalPages;
}
@Override
public int getPageIndex() {
// TODO Auto-generated method stub
return pageIndex;
}
@Override
public int getPageSize() {
// TODO Auto-generated method stub
return pageSize;
}
@Override
public int getTotalPages() {
// TODO Auto-generated method stub
return totalPages;
}
@Override
public int getTotalElements() {
// TODO Auto-generated method stub
return totalElements;
}
@Override
public List<T> getCurrData() {
// TODO Auto-generated method stub
return currData;
}
@Override
public BetweenIndex getBetweenIndex() {
// TODO Auto-generated method stub
return new BetweenIndex() {
private int beginIndex;
private int endIndex;
{
boolean isOdd = pageLinkNumber % 2 == 0;
int val = pageLinkNumber / 2;
beginIndex = pageIndex - (isOdd ? val - 1: val);
endIndex = pageIndex + val;
if(beginIndex < 1) {
beginIndex = 1;
endIndex = pageLinkNumber;
}
if(endIndex > totalPages) {
endIndex = totalPages;
beginIndex = endIndex - pageLinkNumber + 1;
}
}
@Override
public int getEndIndex() {
// TODO Auto-generated method stub
return endIndex;
}
@Override
public int getBeginIndex() {
// TODO Auto-generated method stub
return beginIndex;
}
};
}
@Override
public int getPageLinkNumber() {
// TODO Auto-generated method stub
return pageLinkNumber;
}
@Override
public void setPageLinkNumber(int pageLinkNumber) {
// TODO Auto-generated method stub
if (pageLinkNumber < 0) {
this.pageLinkNumber = 0;
} else if (pageLinkNumber > totalPages) {
this.pageLinkNumber = totalPages;
} else {
this.pageLinkNumber = pageLinkNumber;
}
}
}

  

QueryHandler

用于DefaultPagination实现类的查询回调接口

package com.app.pagination.impl;
import java.util.List;
/**
* 分页查询回调接口
* @author: super.wwz@hotmail.com
* @ClassName: QueryHandler
* @Version: v0.1
* @param <T>
*/
public interface QueryHandler<T> {
/**
* 获取数据总行数
* @return
*/
int getTotalElements(); /**
* 获取当前页的数据
* @param pageIndex
* @param pageSize
* @return
*/
List<T> getCurrData(int pageIndex, int pageSize);
}

  

BookDaoImpl

BookDao的实现类(BookDao接口已经省略)

package com.app.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.app.bean.Book;
import com.app.dao.BaseDao;
import com.app.dao.BookDao;
public class BookDaoImpl extends BaseDao implements BookDao {
@Override
public int count() {
// 查询数据总行数
String sql = "select count(1) from t_book";
try {
return getQueryRunner().query(sql, new ScalarHandler<Integer>());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
@Override
public List<Book> getBooks(int pageIndex, int pageSize) {
// 关于SQLServer的查询分页sql
StringBuffer sql = new StringBuffer();
sql.append("select * from (");
sql.append(" select row_number() over(order by(id)) new_id,* from t_book");
sql.append(") t where new_id between ? and ?");
try {
return getQueryRunner().query(sql.toString(),
new BeanListHandler<Book>(Book.class),
pageSize * (pageIndex - 1) + 1,pageSize * pageIndex);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

  

BookServiceImpl

BookService业务逻辑接口的实现类 (BookService已经省略)

package com.app.service.impl;
import java.util.List;
import com.app.bean.Book;
import com.app.dao.BookDao;
import com.app.dao.impl.BookDaoImpl;
import com.app.pagination.Pagination;
import com.app.pagination.impl.DefaultPagination;
import com.app.pagination.impl.QueryHandler;
import com.app.service.BookService;
/**
* 业务逻辑层查询分页数据示例
* @author: super.wwz@hotmail.com
* @ClassName: BookServiceImpl
* @Version: v0.1
*/
public class BookServiceImpl implements BookService {
private BookDao bookDao = new BookDaoImpl();
@Override
public Pagination<Book> getBookList(int pageIndex, int pageSize,int pageLinkNumber) {
// TODO Auto-generated method stub
return new DefaultPagination<Book>(pageIndex, pageSize, new QueryHandler<Book>() {
@Override
public int getTotalElements() {
// TODO Auto-generated method stub
return bookDao.count();
}
@Override
public List<Book> getCurrData(int pageIndex, int pageSize) {
// TODO Auto-generated method stub
return bookDao.getBooks(pageIndex, pageSize);
}
},pageLinkNumber);
}
}

  

BookAction

有关图书的Servlet控制器

package com.app.web.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.app.bean.Book;
import com.app.pagination.Pagination;
import com.app.service.BookService;
import com.app.service.impl.BookServiceImpl;
public class BookAction extends HttpServlet {
private static final long serialVersionUID = 5275929408058702210L;
private BookService bookService = new BookServiceImpl();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
int pageIndex = 1;
int pageSize = 10;
try {
pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
pageSize = Integer.parseInt(request.getParameter("pageSize"));
} catch (NumberFormatException e) {
e.printStackTrace();
}
//6: 显示的分页链接个数
Pagination<Book> bookPagination = bookService.getBookList(pageIndex, pageSize,6);
request.setAttribute("bookPagination", bookPagination);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}

  

Jsp

index.jsp

将Pagiation应用到bootstrap上的简单示例bootstrap版本: 3.3.5

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:if test="${requestScope.bookPagination == null }">
<c:redirect url="bookAction?pageIndex=1&pageSize=4"></c:redirect>
</c:if>
<!DOCTYPE html">
<html>
<head>
<title>图书信息列表</title>
<!-- Bootstrap v3.3.5 -->
<link href="${pageContext.request.contextPath}/bootstrap-3.3.5-dist/css/bootstrap.min.css"
type="text/css" rel="stylesheet" charset="utf-8" />
<link id="bootstrapTheme"
href="${pageContext.request.contextPath}/bootstrap-3.3.5-dist/css/bootstrap-theme.min.css"
type="text/css" rel="stylesheet" charset="utf-8" />
<script src="${pageContext.request.contextPath}/bootstrap-3.3.5-dist/js/jquery.min.js"
type="text/javascript" charset="utf-8"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.5-dist/js/bootstrap.min.js"
type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="container">
<h2 class="page-header">图书信息</h2>
<table class="table table-striped table-bordered table-hover">
<tr>
<th>#</th>
<th>图书名</th>
<th>单价</th>
</tr>
<c:set var="bookPagination" value="${requestScope.bookPagination}"></c:set>
<c:choose>
<c:when test="${bookPagination.totalElements gt 0}">
<c:forEach var="book" items="${bookPagination.currData}">
<tr>
<td>${book.id }</td>
<td>${book.name }</td>
<td>${book.price }</td>
</tr>
</c:forEach>
<td colspan="3" align="center">
<div class="btn-group" role="group">
<c:if test="${bookPagination.first}" var="isFirst">
<a class="btn btn-primary btn-sm" disabled="disabled" href="#">首页</a>
<a class="btn btn-success btn-sm" disabled="disabled" href="#">上一页</a>
</c:if>
<c:if test="${not isFirst}">
<a class="btn btn-primary btn-sm" href="${pageContext.request.contextPath}/bookAction?pageIndex=1&pageSize=${bookPagination.pageSize}">首页</a>
<a class="btn btn-success btn-sm" href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.previousIndex }&pageSize=${bookPagination.pageSize}">上一页</a>
</c:if>
<c:if test="${bookPagination.last }" var="isLast">
<a class="btn btn-success btn-sm" disabled="disabled" href="#">下一页</a>
<a class="btn btn-primary btn-sm" disabled="disabled" href="#">尾页</a>
</c:if>
<c:if test="${not isLast}">
<a class="btn btn-success btn-sm" href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.nextIndex }&pageSize=${bookPagination.pageSize}">下一页</a>
<a class="btn btn-primary btn-sm" href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.totalPages }&pageSize=${bookPagination.pageSize}">尾页</a>
</c:if>
</div>
</td>
</c:when>
<c:otherwise>
<tr><td colspan="3">没有更多数据!</td></tr>
</c:otherwise>
</c:choose>
</table>
<center>
<nav>
<ul class="pagination">
<c:if test="${isFirst }">
<li class="disabled">
<a href="#" aria-label="Previous">
<span aria-hidden="true">»上一页</span>
</a>
</li>
</c:if>
<c:if test="${not isFirst }">
<li>
<a href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.previousIndex }&pageSize=${bookPagination.pageSize}" aria-label="Previous">
<span aria-hidden="true">»上一页</span>
</a>
</li>
</c:if>
<c:if test="${bookPagination.pageLinkNumber gt 0}">
<c:set var="betweenIndex" value="${bookPagination.betweenIndex}"></c:set>
<c:forEach var="linkIndex" begin="${betweenIndex.beginIndex}" end="${betweenIndex.endIndex}">
<c:if test="${linkIndex eq bookPagination.pageIndex}" var="isCurr">
<li class="active"><a href="#">${linkIndex}</a></li>
</c:if>
<c:if test="${not isCurr}">
<li><a href="${pageContext.request.contextPath}/bookAction?pageIndex=${linkIndex}&pageSize=${bookPagination.pageSize}" >${linkIndex}</a></li>
</c:if>
</c:forEach>
</c:if>
<c:if test="${isLast }">
<li class="disabled">
<a href="#" aria-label="Next">
<span aria-hidden="true">下一页 »</span>
</a>
</li>
</c:if>
<c:if test="${not isLast }">
<li>
<a href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.nextIndex }&pageSize=${bookPagination.pageSize}" aria-label="Next">
<span aria-hidden="true">下一页 »</span>
</a>
</li>
</c:if>
</ul>
</nav>
</center>
</div>
</body>
</html>

  

实例数据

说明:

  1. 如果需要扩展分页功能, 请扩展Pagiation接口以及其余实现类;
  2. 此外, 此分页不依赖任何数据库(重新实现QueryHandler查询回调接口即可), 可适用于任何web项目中;

使用List 集合模拟数据库的使用示例:

package com.app.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.app.bean.Book;
import com.app.pagination.Pagination;
import com.app.pagination.impl.DefaultPagination;
import com.app.service.BookService;
/**
* 使用List<T>集合模拟数据库
* @author: super.wwz@hotmail.com
* @ClassName: BookServiceImpl2
* @Version: v0.1
*/
public class BookServiceImpl2 implements BookService {
// private BookDao bookDao = new BookDaoImpl();
private static List<Book> list = new ArrayList<Book>();
//初始化List<Book>数据
static {
list.add(new Book(1, "书名1", 18));
list.add(new Book(2, "书名2", 13));
list.add(new Book(3, "书名3", 18));
list.add(new Book(4, "书名4", 38));
list.add(new Book(5, "书名5", 18));
list.add(new Book(6, "书名6", 58));
list.add(new Book(7, "书名7", 12));
list.add(new Book(8, "书名8", 11));
list.add(new Book(9, "书名9", 13));
list.add(new Book(10, "书名10", 22));
list.add(new Book(11, "书名11", 19));
list.add(new Book(12, "书名12", 13));
list.add(new Book(13, "书名13", 19));
list.add(new Book(14, "书名14", 32));
}
@Override
public Pagination<Book> getBookList(int pageIndex, int pageSize, int pageLinkNumber) {
return new DefaultPagination<Book>(pageIndex, pageSize, new QueryHandler<Book>() {
@Override
public int getTotalElements() {
//return bookDao.count();
return list.size();
}
@Override
public List<Book> getCurrData(int pageIndex, int pageSize) {
//return bookDao.list(pageIndex, pageSize);
int fromIndex = (pageIndex - 1) * pageSize;
int endIndex = fromIndex + pageSize;
endIndex = endIndex > list.size() ? list.size() : endIndex;
return list.subList(fromIndex, endIndex);
}
}, pageLinkNumber);
}
}

  

运行实例图

暂无

【Bootstrap】Bootstrap和Java分页-第一篇的更多相关文章

  1. 从.Net到Java学习第一篇——开篇

    以前我常说,公司用什么技术我就学什么.可是对于java,我曾经一度以为“学java是不可能的,这辈子不可能学java的.”结果,一遇到公司转java,我就不得不跑路了,于是乎,回头一看N家公司交过社保 ...

  2. 【Bootstrap】Bootstrap和Java分页-第二篇

    目录 关于此文 配置xml-pager.tld 分页控件-Pager 分页action集成类-BaseController 实例-Dao 实例-service 实例-action 实例-JSP 实例- ...

  3. 学习java随笔第一篇:搭建java平台(java se)

    电脑系统:windows8 在这里介绍一下java平台的3个版本: Java SE--Java Standard Edition,Java的标准版,主要用于桌面级的应用和数据库开发. Java EE- ...

  4. 总结一下项目中遇到的分页问题,使用bootstrap-table来做的后台分页,大家可以借鉴一下 (分页第一篇)

    前台进入bootstrap的js和css文件,我就不多少了,另外要引进bootstrap-table的js和css 废话不多说,直接代码.   框架为ssm,代码很清楚 <div class=& ...

  5. [转载] Java高新技术第一篇:类加载器详解

    本文转载自: http://blog.csdn.net/jiangwei0910410003/article/details/17733153 首先来了解一下字节码和class文件的区别: 我们知道, ...

  6. Java高新技术第一篇:类加载器详解

    首先来了解一下字节码和class文件的区别: 我们知道,新建一个Java对象的时候,JVM要将这个对象对应的字节码加载到内存中,这个字节码的原始信息存放在classpath(就是我们新建Java工程的 ...

  7. Java安全第一篇 | 反射看这一篇就够了

    什么是反射? Java安全可以从反序列化漏洞说起,反序列化漏洞又可以从反射说起.反射是⼤多数语⾔⾥都必不可少的组成部分,对象可以通过反射获取他的类,类可以通过反射拿到所有⽅法(包括私有),拿到的⽅法可 ...

  8. Java 学习 第一篇

    1:Java文档注释:使用javadoc工具可以提取程序中文档注释来生成API文档:javadoc命令的基本用法:javadoc 选项 java源文件/包javadoc -d {} -windowti ...

  9. Java【第一篇】基本语法之--关键字、标识符、变量

    关键字 定义:被Java语言赋予了特殊含义,用做专门用途的字符串(单词)特点:关键字中所有字母都为小写 标识符 Java 对各种变量.方法和类等要素命名时使用的字符序列称为标识符凡是自己可以起名字的地 ...

随机推荐

  1. 多线程 thread和Task的用法以及注意事项

    并行 多核线程:Task 首先引用System.Threading; 1:用静态方法:Task.Factory.StartNew()来创建了一个最简单的Task: Task.Factory.Start ...

  2. POJ #2448 A New Operating System

    Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 1165   Accepted: 110 Case Time Limit: ...

  3. 一起买beta版模块单元测试

    一起买beta版模块接口单元测试 测试目的 保证代码质量,对各个模块进行单元测试,不仅可以有效地保证代码的可靠性,让模块在与别的模块整合时出现更少的错误. 而且不用每次启动程序而等待浪费时间. 单元描 ...

  4. BZOJ1088: [SCOI2005]扫雷Mine

    这道题A的好莫名其妙啊2333 传送门 状压DP,枚举上一个雷的分布情况(1<<3)-1,然后和当前的分布相结合,推出下一状态. //BZOJ 1088 //by Cydiater //2 ...

  5. TeamViewer12.0.71503(远程控制软件)精简版单文件企业版介绍

    TeamViewer 是一款能在任何防火墙和 NAT 代理的后台用于远程控制,桌面共享和文件传输的简单且快速的解决方案.为了连接到另一台计算机,只需要在两台计算机上同时运行 TeamViewer 即可 ...

  6. 【原】redux异步操作学习笔记

    摘要: 发觉在学习react的生态链中,react+react-router+webpack+es6+fetch等等这些都基本搞懂的差不多了,可以应用到实战当中,唯独这个redux还不能,学习redu ...

  7. 删除ibus之后导致系统设置进不了

    sudo apt-get instal ubuntu-desktop 快捷键调出sogou拼音,默认为"ctrl+,"

  8. eclipse里怎么用命令行输入args

    eclipse中给java应用传args参数的方法如下:1.先写好Java代码,比如文件名为IntArrqy.java:2.在工具栏或菜单上点run as下边有个Run Configuration:3 ...

  9. 监控web页面的性能指标。

    监控一个web页面的性能也是非常重要的,h5提供了一个非常好的属性来监控:  window.performance 它有两个成员:    navigation (一个叫做performanceNavi ...

  10. linux设置和删除环境变量

    删除环境变量: unset -v PKG_CONFIG_PATH 添加环境变量: export PKG_CONFIG_PATH="你需要设置的路径", 例如: export LAN ...