关于此文
bootstrap是前端很流行的框架,正在开发的项目,用bootstrap搭建起来的页面,自我感觉很完美,终于告别了苍白无力的白花花的难看的……的页面了。哈哈。
现在遇到了bootstrap的分页与Java后台结合起来的分页封装问题,对于我这个Java菜鸟来说,包装分页还没玩过。故此,在网上找了这个。觉得很不错,所以决定记录到博客里面。
还没有实践,决定写完博客去实践。在上图。祝我成功吧!
pagination
分页接口
* @author: super.wwz@hotmail.com
* @ClassName: Pagination
* @Version: v0.1
* @param
*/
public interface Pagination {
/**
* 判断是否是首页
* @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
*/
ListgetCurrData();
/**
/**
/**
BetweenIndex
*/
public interface BetweenIndex {
/**
*/
int getBeginIndex();
/**
*/
int getEndIndex();
}
DefaultPagination
Pagination接口的默认实现类
/
public class DefaultPagination
private int totalElements;
private int pageSize;
private int totalPages;
private int pageIndex;
private QueryHandler
private List
private int pageLinkNumber;
public DefaultPagination(int pageIndex,int pageSize,QueryHandler
this(pageIndex,pageSize,queryHandler);
setPageLinkNumber(pageLinkNumber);
}
public DefaultPagination(int pageIndex,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
// 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实现类的查询回调接口
*/
public interface QueryHandler
/**
*/
int getTotalElements();
/**
- 获取当前页的数据
- @param pageIndex
- @param pageSize
- @return
*/
ListgetCurrData(int pageIndex,int pageSize);
}
BookDaoImpl
BookDao的实现类(BookDao接口已经省略)
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());
} catch (sqlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
@Override
public List 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.class),pageSize * (pageIndex - 1) + 1,pageSize * pageIndex);
} catch (sqlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
BookServiceImpl
BookService业务逻辑接口的实现类 (BookService已经省略)
*/
public class BookServiceImpl implements BookService {
private BookDao bookDao = new BookDaoImpl();
@Override
public Pagination
// TODO Auto-generated method stub
return new DefaultPagination
@Override
public int getTotalElements() {
// TODO Auto-generated method stub
return bookDao.count();
}
@Override
public List
// TODO Auto-generated method stub
return bookDao.getBooks(pageIndex,pageSize);
}
},pageLinkNumber);
}
}
BookAction
有关图书的Servlet控制器
显示的分页链接个数
Pagination bookPagination = bookService.getBookList(pageIndex,6);
request.setAttribute("bookPagination",bookPagination);
request.getRequestDispatcher("index.jsp").forward(request,response);
}
}
Jsp
index.jsp 将Pagiation应用到bootstrap上的简单示例bootstrap版本: 3.3.5