分页控件:
一.实体类:
在com.cdc.rili.console.util.PageTag下实现类page(分页控件的属性),和pageTag(分页标签)
/**
* 翻页对象Page
*/
public class Page
{
/**
* 默认的记录数
*/
public final static int DEFAULT_PAGE_SIZE = 15;
/**
* 默认显示的页数数量
*/
public final static int DEFAULT_SHOW_PAGE_NUMBER_COUNT = 11;
/*
* 当前页码
*/
private int currentPage = 1;
/*
* 页面记录数
*/
private int pageSize = DEFAULT_PAGE_SIZE;
/*
* 记录总数
*/
private int totalCount;
/*
* 记录对象集合
*/
//private List results;
/*
* 显示的页码集合范围
*/
private int[] showPageNumbers;
public Page()
{
}
public Page(int currentPage, int pageSize, int totalCount) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
}
public int getPageSize()
{
return pageSize;
}
public void setPageSize(int pageSize)
{
this.pageSize = pageSize;
}
public int getTotalCount()
{
if(totalCount==-1)
{
totalCount = 0;
}
return totalCount;
}
public void setTotalCount(int totalCount)
{
this.totalCount = totalCount;
}
public int getCurrentPage()
{
if (this.currentPage < 0)
{
this.currentPage = 1;
}
if (this.currentPage > this.getTotalPage())
{
this.currentPage = this.getTotalPage();
}
return this.currentPage;
}
public void setCurrentPage(int currentPage)
{
this.currentPage = currentPage;
}
/**
* 获得总页数
*
* @return
*/
public int getTotalPage()
{
return (totalCount % pageSize == 0) ? totalCount / pageSize
: totalCount / pageSize + 1;
}
/**
* 获得数据库中记录集的索引位置
*
* @return
*/
public int getStartIndex()
{
return (getCurrentPage() - 1) * getPageSize();
}
/**
* 是否有上一页
*
* @return
*/
public boolean hasLastPage()
{
if (this.getCurrentPage() > 1) { return true; }
return false;
}
/**
* 是否有下一页
*
* @return
*/
public boolean hasNextPage()
{
if (this.getCurrentPage() < this.getTotalPage()) { return true; }
return false;
}
/**
* 默认显示的页码范围
*
* @return
*/
public int[] getShowPageNumbers()
{
return getShowPageNumbers(DEFAULT_SHOW_PAGE_NUMBER_COUNT);
}
/**
* 自定义的显示页码范围
*
* @param showCount
* 页码范围值
* @return
*/
public int[] getShowPageNumbers(int showCount)
{
if (showPageNumbers == null)
{
int totalPageCount = getTotalPage();
showPageNumbers = new int[totalPageCount > showCount ? showCount
: totalPageCount];
if (totalPageCount > showCount)
{
int firstShowPage = currentPage - (showCount + 1) / 2;
int endShowPage = currentPage + showCount / 2;
if (firstShowPage > 0 && endShowPage < totalPageCount)
{
for (int i = 0, max = showPageNumbers.length; i < max; i++)
{
showPageNumbers[i] = firstShowPage + i + 1;
}
}
else if (firstShowPage > 0)
{
for (int i = 0, max = showPageNumbers.length; i < max; i++)
{
showPageNumbers[i] = totalPageCount - showCount + i;
}
}
else
{
for (int i = 0, max = showPageNumbers.length; i < max; i++)
{
showPageNumbers[i] = i;
}
}
}
else
{
for (int i = 1, max = showPageNumbers.length; i <= max; i++)
{
showPageNumbers[i - 1] = i;
}
}
}
return showPageNumbers;
}
}
分页标签类PageTag:所有的标签类继承SimpleTagSupport类,并且重写 doTag() 方法
public class PageTag extends SimpleTagSupport (jsp-api.jar包中自带的类)
{
private enum PageBarStyle {
Normal, ShowPageNumbers, WapPage,printNormalPageBar
}
private PageBarStyle tagStyle = PageBarStyle.Normal;
private String id;
private String pageBarStyle = PageBarStyle.Normal.toString();
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getPageBarStyle() {
return pageBarStyle;
}
public void setPageBarStyle(String style) {
if(!"".equals(style)) {
this.pageBarStyle = style;
this.tagStyle = PageBarStyle.valueOf(style);
}
}
public void doTag() throws JspException, IOException
{
this.getJspContext().getOut().write(printPageNumbersBar());
}
private String printPageNumbersBar() throws UnsupportedEncodingException
{
try{
//从上下文中取分页控件的参数内容
PageListHolder pageListHolder = (PageListHolder)this.getJspContext().findAttribute("pageListHolder");
int page =pageListHolder.getCurrentPage();
int totalCount =pageListHolder.getTotal();
int totalPage = pageListHolder.getPageCount();
int pageSize =pageListHolder.getPageSize();
Page pager = new Page();
pager.setCurrentPage(page);
pager.setPageSize(pageSize);
pager.setTotalCount(totalCount);
int[] showPageNumbers = pager.getShowPageNumbers();
String url = this.getUrl();
StringBuffer pageBar = new StringBuffer();
pageBar.append("<div class='page'>");
if (totalCount > 0) {
// 上一页
if(page<=1) {
pageBar.append("<span class='disabled'> < </span>");
}
else {
pageBar.append("<a href='" + url + "page=" + (page - 1) + "'>< </a>");
}
// 中间页码
if(showPageNumbers[0] > 2) {
pageBar.append("<a href='" + url + "page=1'>1</a>");
pageBar.append("<a href='" + url + "page=1'>2</a>");
if(showPageNumbers[0] > 3)
pageBar.append("...");
}
else if(showPageNumbers[0] == 2){
pageBar.append("<a href='" + url + "page=1'>1</a>");
}
for(int i = 0; i < showPageNumbers.length; i++) {
if(showPageNumbers[i] > 0) {
if(page == showPageNumbers[i]) {
pageBar.append("<span class='current'>" + showPageNumbers[i] + "</span>");
}else {
pageBar.append("<a href='" + url + "page=" + showPageNumbers[i] + "'>" + showPageNumbers[i] + "</a>");
}
}
}
if((totalPage - showPageNumbers[showPageNumbers.length - 1]) > 2) {
pageBar.append("...");
pageBar.append("<a href='" + url + "page=" + (totalPage - 1) + "'>" + (totalPage - 1) + "</a>");
pageBar.append("<a href='" + url + "page=" + totalPage + "'>" + totalPage + "</a>");
}
else {
for(int i = showPageNumbers[showPageNumbers.length - 1] + 1; i <= totalPage; i++) {
pageBar.append("<a href='" + url + "page=" + i + "'>" + i + "</a>");
}
}
// 下一页
if (page < totalPage) {
pageBar.append("<a href='" + url + "page=" + (page + 1) + "'>> </a>");
}
else {
pageBar.append("<span class='disabled'> > </span>");
}
}
pageBar.append("</div>");
return pageBar.toString();
}
catch(Exception e)
{
return null;
}
}
@SuppressWarnings("static-access")
public String getUrl() throws UnsupportedEncodingException
{
Set keys = ActionContext.getContext().getParameters().keySet();
String parameterUrl = "";
Iterator it = keys.iterator();
while(it.hasNext())
{
String key = (String)it.next();
if(!key.equals("page"))
{
String ss = this.getStringValue(key);
parameterUrl += key + "=" + ss + "&";
}
}
String action = (String)ActionContext.getContext().get(ActionContext.getContext().ACTION_NAME)+".action";
String url = action +"?" + parameterUrl;
return url;
}
public String getStringValue(String key)
{
return ((String[])ActionContext.getContext().getParameters().get(key))[0];
}
}
控件类与action类都会调用的类PageListHolder
public class PageListHolder<T> {
// 默认每页记录
public static final int DEFAULT_PAGESIZE = 20;
// 总记录数
private int total;
// 当前页面记录起始,从0开始
private int pageStart;
// 页面记录大小
private int pageSize;
// 当前页面的记录
private List<T> list;
// 当前页前后保留数
private int intervalCount = 2;
public PageListHolder(int pageStart, int pageSize) {
super();
this.pageStart = pageStart;
this.pageSize = pageSize;
}
public PageListHolder() {
super();
this.pageSize = DEFAULT_PAGESIZE;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPageStart() {
return pageStart;
}
public void setPageStart(int pageStart) {
this.pageStart = pageStart;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* 数据库查询的start
*
* @return
*/
public int getStart() {
return pageSize * pageStart;
}
/**
* 数据库查询的limit
*
* @return
*/
public int getLimit() {
return pageSize;
}
/**
* @return 当前页,从1开始
*/
public int getCurrentPage() {
return pageStart + 1;
}
/**
* @return 总页数
*/
public int getPageCount() {
int mod = total % pageSize;
if (mod == 0) {
return total / pageSize;
} else {
return total / pageSize + 1;
}
}
/**
* 是否最后一页
*/
public boolean isLastPage() {
return getCurrentPage() >= getPageCount();
}
/**
* 是否首页 对于页面来说,序号是从1开始
*/
public boolean isFirstPage() {
return getCurrentPage() == 1;
}
/**
* @return the intervalCount
*/
public int getIntervalCount() {
return intervalCount;
}
/**
* @param intervalCount
* the intervalCount to set
*/
public void setIntervalCount(int intervalCount) {
this.intervalCount = intervalCount;
}
}
二.tld文件:
在WebContent/WEB-INF/tlds/commons.tld中设置分页标签的内容如下:
三.jsp文件:
1.jsp页面,首部的引用导入
<%@ taglib prefix="page" uri="/page-tags"%>
2.jsp页面,中间的控件引用
<page:pageBar id="p1" pageBarStyle="ShowPageNumbers" />
四.对应的action设置:
1.分页控件参数的赋值:
App model=new App();
int currentpage = getIntValue("page");
if(currentpage<=0)
currentpage=1;
PageListHolder<App> pageListHolder = new PageListHolder<App>();
pageListHolder.setPageStart(currentpage-1);
pageListHolder.setPageSize(pageSize);
appService.getListByPaging(model, pageListHolder);
//设置分页标签
this.setPageTagParam(pageListHolder);
setAttribute("appList", pageListHolder.getList());
return SUCCESS;
2.分页控件的参数内容存放到上下文中函数定义
/**
* 设置分页控件参数
* @param page Page对象
*/
public void setPageTagParam(PageListHolder PageListHolder) {
ActionContext.getContext().put("pageListHolder", PageListHolder);
}