Bootstrap和Java分页实例第一篇
关于此文
bootstrap是前端很流行的框架,正在开发的项目,用bootstrap搭建起来的页面,自我感觉很完美,终于告别了苍白无力的白花花的难看的……的页面了。哈哈。
现在遇到了bootstrap的分页与Java后台结合起来的分页封装问题,对于我这个Java菜鸟来说,包装分页还没玩过。故此,在网上找了这个。觉得很不错,所以决定记录到博客里面。
还没有实践,决定写完博客去实践。在上图。祝我成功吧!
此人的博客没找到,代码中有email地址。
pagination
定义了分页常用的属性,方法
packagecom.app.pagination;
importjava.util.List;
/**
*通用分页接口
*@author:super.wwz@hotmail.com
*@ClassName:Pagination
*@Version:v0.1
*@param<T>
*/
publicinterfacePagination<T>{
/**
*判断是否是首页
*@return
*/
booleanisFirst();
/**
*判断是否是尾页
*@return
*/
booleanisLast();
/**
*判断是否有上一页
*@return
*/
booleanisPrevious();
/**
*判断是否有下一页
*@return
*/
booleanisNext();
/**
*获取上一页的页码
*@return
*/
intgetPreviousIndex();
/**
*获取下一页的页码
*@return
*/
intgetNextIndex();
/**
*获取当前页码
*@return
*/
intgetPageIndex();
/**
*获取当前页大小
*@return
*/
intgetPageSize();
/**
*获取总页数
*@return
*/
intgetTotalPages();
/**
*获取数据总行数
*@return
*/
intgetTotalElements();
/**
*获取当前页的数据
*@return
*/
List<T>getCurrData();
/**
*获取数字分页链接对象
*@return
*/
BetweenIndexgetBetweenIndex();
/**
*获取每页显示的分页链接数
*@return
*/
intgetPageLinkNumber();
/**
*设置每页的分页链接数量
*@parampageLinkNumber
*/
voidsetPageLinkNumber(intpageLinkNumber);
}
BetweenIndex
该接口负责获取分页链接的开始和结尾索引
packagecom.app.pagination;
/**
*开始链接-结束链接
*@author:super.wwz@hotmail.com
*@ClassName:BetweenIndex
*@Version:v0.1
*/
publicinterfaceBetweenIndex{
/**
*获取开始分页链接索引
*@return
*/
intgetBeginIndex();
/**
*获取结束分页链接索引
*@return
*/
intgetEndIndex();
}
DefaultPagination
Pagination接口的默认实现类
packagecom.app.pagination.impl;
importjava.util.List;
importcom.app.pagination.BetweenIndex;
importcom.app.pagination.Pagination;
/**
*Pagination接口默认实现
*@author:super.wwz@hotmail.com
*@ClassName:DefaultPagination
*@Version:v0.1
*@param<T>
*/
publicclassDefaultPagination<T>implementsPagination<T>{
privateinttotalElements;
privateintpageSize;
privateinttotalPages;
privateintpageIndex;
privateQueryHandler<T>queryHandler;
privateList<T>currData;
privateintpageLinkNumber;
publicDefaultPagination(intpageIndex,intpageSize,QueryHandler<T>queryHandler,intpageLinkNumber){
this(pageIndex,pageSize,queryHandler);
setPageLinkNumber(pageLinkNumber);
}
publicDefaultPagination(intpageIndex,intpageSize,QueryHandler<T>queryHandler){
//初始化数据访问回调接口
this.queryHandler=queryHandler;
//查询总行数
setTotalElements();
//修正页大小
setPageSize(pageSize);
//计算总页数:
setTotalPages();
//修正页码
setPageIndex(pageIndex);
//查询当前页数据
setCurrData();
}
privatevoidsetCurrData(){
//TODOAuto-generatedmethodstub
this.currData=queryHandler.getCurrData(pageIndex,pageSize);
}
privatevoidsetPageIndex(intpageIndex){
//TODOAuto-generatedmethodstub
if(pageIndex<1){
this.pageIndex=1;
}elseif(pageIndex>totalPages){
this.pageIndex=totalPages;
}else{
this.pageIndex=pageIndex;
}
}
privatevoidsetTotalPages(){
//TODOAuto-generatedmethodstub
if(pageSize>0){
/*//普通算法:
this.totalPages=totalElements%pageSize==0?
totalElements/pageSize:(totalElements/pageSize)+1;*/
//减一公式:
this.totalPages=(totalElements+pageSize-1)/pageSize;
}
}
privatevoidsetPageSize(intpageSize){
//TODOAuto-generatedmethodstub
if(pageSize<1){
this.pageSize=1;
}elseif(pageSize>totalElements){
this.pageSize=totalElements;
}else{
this.pageSize=pageSize;
}
}
privatevoidsetTotalElements(){
//TODOAuto-generatedmethodstub
this.totalElements=queryHandler.getTotalElements();
}
@Override
publicbooleanisFirst(){
//TODOAuto-generatedmethodstub
returnpageIndex==1;
}
@Override
publicbooleanisLast(){
//TODOAuto-generatedmethodstub
returnpageIndex==totalPages;
}
@Override
publicbooleanisPrevious(){
//TODOAuto-generatedmethodstub
returnpageIndex>1;
}
@Override
publicbooleanisNext(){
//TODOAuto-generatedmethodstub
returnpageIndex<totalPages;
}
@Override
publicintgetPreviousIndex(){
//TODOAuto-generatedmethodstub
returnisPrevious()?pageIndex-1:1;
}
@Override
publicintgetNextIndex(){
//TODOAuto-generatedmethodstub
returnisNext()?pageIndex+1:totalPages;
}
@Override
publicintgetPageIndex(){
//TODOAuto-generatedmethodstub
returnpageIndex;
}
@Override
publicintgetPageSize(){
//TODOAuto-generatedmethodstub
returnpageSize;
}
@Override
publicintgetTotalPages(){
//TODOAuto-generatedmethodstub
returntotalPages;
}
@Override
publicintgetTotalElements(){
//TODOAuto-generatedmethodstub
returntotalElements;
}
@Override
publicList<T>getCurrData(){
//TODOAuto-generatedmethodstub
returncurrData;
}
@Override
publicBetweenIndexgetBetweenIndex(){
//TODOAuto-generatedmethodstub
returnnewBetweenIndex(){
privateintbeginIndex;
privateintendIndex;
{
booleanisOdd=pageLinkNumber%2==0;
intval=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
publicintgetEndIndex(){
//TODOAuto-generatedmethodstub
returnendIndex;
}
@Override
publicintgetBeginIndex(){
//TODOAuto-generatedmethodstub
returnbeginIndex;
}
};
}
@Override
publicintgetPageLinkNumber(){
//TODOAuto-generatedmethodstub
returnpageLinkNumber;
}
@Override
publicvoidsetPageLinkNumber(intpageLinkNumber){
//TODOAuto-generatedmethodstub
if(pageLinkNumber<0){
this.pageLinkNumber=0;
}elseif(pageLinkNumber>totalPages){
this.pageLinkNumber=totalPages;
}else{
this.pageLinkNumber=pageLinkNumber;
}
}
}
QueryHandler
用于DefaultPagination实现类的查询回调接口
packagecom.app.pagination.impl;
importjava.util.List;
/**
*分页查询回调接口
*@author:super.wwz@hotmail.com
*@ClassName:QueryHandler
*@Version:v0.1
*@param<T>
*/
publicinterfaceQueryHandler<T>{
/**
*获取数据总行数
*@return
*/
intgetTotalElements();
/**
*获取当前页的数据
*@parampageIndex
*@parampageSize
*@return
*/
List<T>getCurrData(intpageIndex,intpageSize);
}
BookDaoImpl
BookDao的实现类(BookDao接口已经省略)
packagecom.app.dao.impl;
importjava.sql.SQLException;
importjava.util.List;
importorg.apache.commons.dbutils.handlers.BeanListHandler;
importorg.apache.commons.dbutils.handlers.ScalarHandler;
importcom.app.bean.Book;
importcom.app.dao.BaseDao;
importcom.app.dao.BookDao;
publicclassBookDaoImplextendsBaseDaoimplementsBookDao{
@Override
publicintcount(){
//查询数据总行数
Stringsql="selectcount(1)fromt_book";
try{
returngetQueryRunner().query(sql,newScalarHandler<Integer>());
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
return0;
}
@Override
publicList<Book>getBooks(intpageIndex,intpageSize){
//关于SQLServer的查询分页sql
StringBuffersql=newStringBuffer();
sql.append("select*from(");
sql.append("selectrow_number()over(orderby(id))new_id,*fromt_book");
sql.append(")twherenew_idbetween?and?");
try{
returngetQueryRunner().query(sql.toString(),
newBeanListHandler<Book>(Book.class),
pageSize*(pageIndex-1)+1,pageSize*pageIndex);
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnnull;
}
}
BookServiceImpl
BookService业务逻辑接口的实现类(BookService已经省略)
packagecom.app.service.impl;
importjava.util.List;
importcom.app.bean.Book;
importcom.app.dao.BookDao;
importcom.app.dao.impl.BookDaoImpl;
importcom.app.pagination.Pagination;
importcom.app.pagination.impl.DefaultPagination;
importcom.app.pagination.impl.QueryHandler;
importcom.app.service.BookService;
/**
*业务逻辑层查询分页数据示例
*@author:super.wwz@hotmail.com
*@ClassName:BookServiceImpl
*@Version:v0.1
*/
publicclassBookServiceImplimplementsBookService{
privateBookDaobookDao=newBookDaoImpl();
@Override
publicPagination<Book>getBookList(intpageIndex,intpageSize,intpageLinkNumber){
//TODOAuto-generatedmethodstub
returnnewDefaultPagination<Book>(pageIndex,pageSize,newQueryHandler<Book>(){
@Override
publicintgetTotalElements(){
//TODOAuto-generatedmethodstub
returnbookDao.count();
}
@Override
publicList<Book>getCurrData(intpageIndex,intpageSize){
//TODOAuto-generatedmethodstub
returnbookDao.getBooks(pageIndex,pageSize);
}
},pageLinkNumber);
}
}
BookAction
有关图书的Servlet控制器
packagecom.app.web.action;
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.app.bean.Book;
importcom.app.pagination.Pagination;
importcom.app.service.BookService;
importcom.app.service.impl.BookServiceImpl;
publicclassBookActionextendsHttpServlet{
privatestaticfinallongserialVersionUID=5275929408058702210L;
privateBookServicebookService=newBookServiceImpl();
@Override
protectedvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
intpageIndex=1;
intpageSize=10;
try{
pageIndex=Integer.parseInt(request.getParameter("pageIndex"));
pageSize=Integer.parseInt(request.getParameter("pageSize"));
}catch(NumberFormatExceptione){
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
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>
<c:iftest="${requestScope.bookPagination==null}">
<c:redirecturl="bookAction?pageIndex=1&pageSize=4"></c:redirect>
</c:if>
<!DOCTYPEhtml">
<html>
<head>
<title>图书信息列表</title>
<!--Bootstrapv3.3.5-->
<linkhref="${pageContext.request.contextPath}/bootstrap-3.3.5-dist/css/bootstrap.min.css"
type="text/css"rel="stylesheet"charset="utf-8"/>
<linkid="bootstrapTheme"
href="${pageContext.request.contextPath}/bootstrap-3.3.5-dist/css/bootstrap-theme.min.css"
type="text/css"rel="stylesheet"charset="utf-8"/>
<scriptsrc="${pageContext.request.contextPath}/bootstrap-3.3.5-dist/js/jquery.min.js"
type="text/javascript"charset="utf-8"></script>
<scriptsrc="${pageContext.request.contextPath}/bootstrap-3.3.5-dist/js/bootstrap.min.js"
type="text/javascript"charset="utf-8"></script>
</head>
<body>
<divclass="container">
<h2class="page-header">图书信息</h2>
<tableclass="tabletable-stripedtable-borderedtable-hover">
<tr>
<th>#</th>
<th>图书名</th>
<th>单价</th>
</tr>
<c:setvar="bookPagination"value="${requestScope.bookPagination}"></c:set>
<c:choose>
<c:whentest="${bookPagination.totalElementsgt0}">
<c:forEachvar="book"items="${bookPagination.currData}">
<tr>
<td>${book.id}</td>
<td>${book.name}</td>
<td>${book.price}</td>
</tr>
</c:forEach>
<tdcolspan="3"align="center">
<divclass="btn-group"role="group">
<c:iftest="${bookPagination.first}"var="isFirst">
<aclass="btnbtn-primarybtn-sm"disabled="disabled"href="#">首页</a>
<aclass="btnbtn-successbtn-sm"disabled="disabled"href="#">上一页</a>
</c:if>
<c:iftest="${notisFirst}">
<aclass="btnbtn-primarybtn-sm"href="${pageContext.request.contextPath}/bookAction?pageIndex=1&pageSize=${bookPagination.pageSize}">首页</a>
<aclass="btnbtn-successbtn-sm"href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.previousIndex}&pageSize=${bookPagination.pageSize}">上一页</a>
</c:if>
<c:iftest="${bookPagination.last}"var="isLast">
<aclass="btnbtn-successbtn-sm"disabled="disabled"href="#">下一页</a>
<aclass="btnbtn-primarybtn-sm"disabled="disabled"href="#">尾页</a>
</c:if>
<c:iftest="${notisLast}">
<aclass="btnbtn-successbtn-sm"href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.nextIndex}&pageSize=${bookPagination.pageSize}">下一页</a>
<aclass="btnbtn-primarybtn-sm"href="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.totalPages}&pageSize=${bookPagination.pageSize}">尾页</a>
</c:if>
</div>
</td>
</c:when>
<c:otherwise>
<tr><tdcolspan="3">没有更多数据!</td></tr>
</c:otherwise>
</c:choose>
</table>
<center>
<nav>
<ulclass="pagination">
<c:iftest="${isFirst}">
<liclass="disabled">
<ahref="#"aria-label="Previous">
<spanaria-hidden="true">»上一页</span>
</a>
</li>
</c:if>
<c:iftest="${notisFirst}">
<li>
<ahref="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.previousIndex}&pageSize=${bookPagination.pageSize}"aria-label="Previous">
<spanaria-hidden="true">»上一页</span>
</a>
</li>
</c:if>
<c:iftest="${bookPagination.pageLinkNumbergt0}">
<c:setvar="betweenIndex"value="${bookPagination.betweenIndex}"></c:set>
<c:forEachvar="linkIndex"begin="${betweenIndex.beginIndex}"end="${betweenIndex.endIndex}">
<c:iftest="${linkIndexeqbookPagination.pageIndex}"var="isCurr">
<liclass="active"><ahref="#">${linkIndex}</a></li>
</c:if>
<c:iftest="${notisCurr}">
<li><ahref="${pageContext.request.contextPath}/bookAction?pageIndex=${linkIndex}&pageSize=${bookPagination.pageSize}">${linkIndex}</a></li>
</c:if>
</c:forEach>
</c:if>
<c:iftest="${isLast}">
<liclass="disabled">
<ahref="#"aria-label="Next">
<spanaria-hidden="true">下一页»</span>
</a>
</li>
</c:if>
<c:iftest="${notisLast}">
<li>
<ahref="${pageContext.request.contextPath}/bookAction?pageIndex=${bookPagination.nextIndex}&pageSize=${bookPagination.pageSize}"aria-label="Next">
<spanaria-hidden="true">下一页»</span>
</a>
</li>
</c:if>
</ul>
</nav>
</center>
</div>
</body>
</html>
实例数据
说明:
- 如果需要扩展分页功能,请扩展Pagiation接口以及其余实现类;
- 此外,此分页不依赖任何数据库(重新实现QueryHandler查询回调接口即可),可适用于任何web项目中;
使用List集合模拟数据库的使用示例:
packagecom.app.service.impl;
importjava.util.ArrayList;
importjava.util.List;
importcom.app.bean.Book;
importcom.app.pagination.Pagination;
importcom.app.pagination.impl.DefaultPagination;
importcom.app.service.BookService;
/**
*使用List<T>集合模拟数据库
*@author:super.wwz@hotmail.com
*@ClassName:BookServiceImpl2
*@Version:v0.1
*/
publicclassBookServiceImpl2implementsBookService{
//privateBookDaobookDao=newBookDaoImpl();
privatestaticList<Book>list=newArrayList<Book>();
//初始化List<Book>数据
static{
list.add(newBook(1,"书名1",18));
list.add(newBook(2,"书名2",13));
list.add(newBook(3,"书名3",18));
list.add(newBook(4,"书名4",38));
list.add(newBook(5,"书名5",18));
list.add(newBook(6,"书名6",58));
list.add(newBook(7,"书名7",12));
list.add(newBook(8,"书名8",11));
list.add(newBook(9,"书名9",13));
list.add(newBook(10,"书名10",22));
list.add(newBook(11,"书名11",19));
list.add(newBook(12,"书名12",13));
list.add(newBook(13,"书名13",19));
list.add(newBook(14,"书名14",32));
}
@Override
publicPagination<Book>getBookList(intpageIndex,intpageSize,intpageLinkNumber){
returnnewDefaultPagination<Book>(pageIndex,pageSize,newQueryHandler<Book>(){
@Override
publicintgetTotalElements(){
//returnbookDao.count();
returnlist.size();
}
@Override
publicList<Book>getCurrData(intpageIndex,intpageSize){
//returnbookDao.list(pageIndex,pageSize);
intfromIndex=(pageIndex-1)*pageSize;
intendIndex=fromIndex+pageSize;
endIndex=endIndex>list.size()?list.size():endIndex;
returnlist.subList(fromIndex,endIndex);
}
},pageLinkNumber);
}
}
下一篇更精彩!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。