学习分页的时候找到一个好用的分页jar包,在此分享给大家,这个包没找到源码,暂且介绍下使用方法。
jar包地址:http://download.csdn.net/detail/lovejj1994/9132619
上面是分页效果图。
下面是一个“品牌”的javabean ,重要的代码注意注释,要有“页号”,“每行数”,“每页数”,并且setPageSize()方法要计算“开始行”。
/**
* 品牌
* @author pq
*
*/
public class Brand {
private Integer id;
private String name;
private String description;
private String imgUrl;
private String webSite;
private Integer sort;
private Integer isDisplay;
//页号
private Integer pageNo;
//开始行
private Integer startRow;
//每页数
private Integer pageSize;
public Integer getStartRow() {
return startRow;
}
public void setStartRow(Integer startRow) {
this.startRow = startRow;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
//计算开始行
this.startRow = (this.pageNo - 1)*pageSize;
this.pageSize = pageSize;
}
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public Integer getIsDisplay() {
return isDisplay;
}
public void setIsDisplay(Integer isDisplay) {
this.isDisplay = isDisplay;
}
public String getWebSite() {
return webSite;
}
public void setWebSite(String webSite) {
this.webSite = webSite;
}
}
controller包内的 list(String name, Integer isDisplay, Integer pageNo,
Model model)方法是
public String list(String name, Integer isDisplay, Integer pageNo,
Model model) {
StringBuilder params = new StringBuilder();
Brand brand = new Brand();
if (null != name && StringUtils.isNotBlank(name)) {
brand.setName(name);
params.append("name=").append(name).append("&&");
}
// cpn方法:如果页号为空或小于1,默认置为1
brand.setPageNo(Pagination.cpn(pageNo));
brand.setPageSize(5);
//约定isDisplay为3时,显示所有"品牌",此时isDisplay不添加条件
if (null != isDisplay && 3 == isDisplay) {
brand.setIsDisplay(null);
params.append("isDisplay=").append("3");
} else if (null != isDisplay) {
brand.setIsDisplay(isDisplay);
params.append("isDisplay=").append(isDisplay);
} else {
brand.setIsDisplay(1);
params.append("isDisplay=").append(1);
}
Pagination pagination = brandService.getBrandListWithPage(brand);
// 用于分页展示
// http://127.0.0.1:8080/brand/list.do?isDisplay=1&&name=凯速(KANSOON),params为查询时需要加上的附加条件。
pagination.pageView("/brand/list.do", params.toString());
//用于向页面传递分页对象
model.addAttribute("pagination", pagination);
//用于回显
model.addAttribute("name", name);
model.addAttribute("isDisplay", isDisplay);
return "/brand/list";
}
service包内的getBrandListWithPage(Brand brand)方法,用于初始化Pagination 分页对象和设定要分页的集合。
public Pagination getBrandListWithPage(Brand brand) {
/***
* Pagination工具初始化,要传一下参数,切记不可为空。
* 1.当页数
* 2.每页行数
* 3.总记录数
*/
pagination = new Pagination(brand.getPageNo(), brand.getPageSize(), brandDao.getBrandCount(brand));
//将数据库查到的数据加入集合 pagination.setList(brandDao.getBrandListWithPage(brand));
return pagination;
}
在jsp页面页面展示代码
<div class="page pb15">
<span class="r inb_a page_b">
<c:forEach items="${pagination.pageView }" var="page">
${page }
</c:forEach>
</span>
</div>
css样式文件
/*page*/
.page{width:98%;margin:10px auto 0;overflow:hidden;clear:both}
.page var{margin:0 4px}
.page input{vertical-align:middle}
.page .l{padding-left:6px}
.page .l input,.page .l select{margin-right:6px}
.page .r{padding:0 6px}
.page .r .txts{width:40px;margin-right:6px}
.page .l a.inb,.page .r a,.page .r span{margin-right:6px;padding:0 5px;text-align:center;vertical-align:middle}
.pb15{padding-bottom:15px}
.page_b{}
.page_b .txts{margin-left:6px}
.page_b a,.page_b span{min-width:16px;_width:16px;height:24px;line-height:24px}
.page_b a{color:#333;border:1px solid #dfdfdf;background-color:#fff}
.page_b span{color:#666;cursor:default;border:1px solid #ccc;background-color:#fff}
.page_b a:hover,.page_b .current{color:#fff;text-decoration:none;border:1px solid #3c6bc2;background-color:#39c}
.page_b .current{font-weight:bold}
.page_b .break{font-weight:bold;border:0;background:none}
/* other */
.inb,.inb_a a,form .pos{display:-moz-inline-block;display:-moz-inline-box;display:inline-block}
游览器追踪下的分页按钮代码 它会自动生成