/**
* @author zhangshuling E-mail:
[email protected]
* @version createtime:Apr 2, 2009 9:20:07 AM 此类用于整合 ajax 进行分页 返回一个
* java.util.Hashtable 里面分别有两个对象 一个为分页的字符串 ( < << 1 | 2 | 3 | 4 |5 |
* ..... >> >) 一个为分页的数据(java.util.List) 暂时没做Hibernate的相关的查询,有需要就自己添吧
* ^_^
*/
public class AjaxPage {
private int pageSize = 10; // 分页刻度 (多少条记录为一页)
private int pageTotal = 0; // 分页总数 (一共有多少页)
// 分页所需图片
private String firstPageImage = "http://www.kaogoo.com/english/images/back2.gif";
private String previousPageImage = "http://www.kaogoo.com/english/images/backing.gif";
private String nextPageImage = "http://www.kaogoo.com/english/images/next.gif";
private String lastPageImage = "http://www.kaogoo.com/english/images/next2.gif";
public static final String KEY_PAGELIST = "PAGELIST";
public static final String KEY_PAGELINK = "PAGELINK";
public AjaxPage() {
}
public AjaxPage(int pageSize, int pageTotal, String firstPageImage,
String previousPageImage, String nextPageImage, String lastPageImage) {
setPageSize(pageSize);
setPageTotal(pageTotal);
setFirstPageImage(firstPageImage);
setPreviousPageImage(previousPageImage);
setNextPageImage(nextPageImage);
setLastPageImage(lastPageImage);
}
public String getFirstPageImage() {
return firstPageImage;
}
public void setFirstPageImage(String firstPageImage) {
this.firstPageImage = firstPageImage;
}
public String getLastPageImage() {
return lastPageImage;
}
public void setLastPageImage(String lastPageImage) {
this.lastPageImage = lastPageImage;
}
public String getNextPageImage() {
return nextPageImage;
}
public void setNextPageImage(String nextPageImage) {
this.nextPageImage = nextPageImage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageTotal() {
return pageTotal;
}
public void setPageTotal(int pageTotal) {
this.pageTotal = pageTotal;
}
public String getPreviousPageImage() {
return previousPageImage;
}
public void setPreviousPageImage(String previousPageImage) {
this.previousPageImage = previousPageImage;
}
/**
*
* @param sqlTotal
* //查询所有记录条数
* @param sql
* //查询所需记录
* @param currentPage
* //当前页码
* @param parameter
* //参数() parameter所带的& 必须用&转义 不然在会在传递的时候出错 ¶meter=1&...
* 应该写成 &parameter=1&...
* @param ajaxMethod
* //ajaxMethod 指的是ajax调用的javascript方法 格式为
* ajaxMethod(parameter,page);
* @param connection
* @param preparedStatement
* @param resultSet
* @return
*/
public java.util.Hashtable ajaxPage(String sqlTotal, String sql,
int currentPage, String parameter, String ajaxMethod,
java.sql.Connection connection,
java.sql.PreparedStatement preparedStatement,
java.sql.ResultSet resultSet) {
java.util.Hashtable pageContent = new java.util.Hashtable();
java.util.List pageList = new java.util.ArrayList();
StringBuffer pageLink = new StringBuffer(
"<table width="100%" align="center" cellspacing="0" cellpadding="0">");
try {
preparedStatement = connection.prepareStatement(sqlTotal);
resultSet = preparedStatement.executeQuery();
resultSet.next();
setPageTotal(resultSet.getInt(1));
} catch (java.sql.SQLException e) {
setPageTotal(0);
} catch (Exception e) {
setPageTotal(0);
}
if (getPageTotal() > 0) {
int pageCount = (int) Math.ceil((double) getPageTotal()
/ getPageSize()); // 总共该分多少页
int start = currentPage - 4;
int end = currentPage + 4;
if (start <= 1) {
start = 1;
end = start + 9;
}
if (end >= pageCount) {
end = pageCount;
start = end - 9;
if (start <= 0) {
start = 1;
}
}
int previous = (currentPage - 1 > 0 ? (currentPage - 1) : 1);
int next = ((currentPage + 1) < pageCount ? (currentPage + 1)
: pageCount);
pageLink
.append("<tr><td align="center" height="30" width="100%">");
pageLink.append("<a href="javascript:").append(ajaxMethod)
.append("('").append(parameter).append("',1)").append(
"" title="转到第1页" >");
pageLink.append("<img boder="0" src="");
pageLink.append(getFirstPageImage())
.append(""/></a>");
pageLink.append("&nbsp;&nbsp;");
pageLink.append("<a href="javascript:").append(ajaxMethod)
.append("('").append(parameter).append("',").append(
previous).append(")" title="转到第").append(
previous).append("页" >");
pageLink.append("<img boder="0" src="");
pageLink.append(getPreviousPageImage()).append(
""/></a>");
pageLink.append("&nbsp;&nbsp;");
for (int i = start; i <= end; i++) {
if (i != currentPage) {
pageLink.append("<a href="javascript:").append(
ajaxMethod).append("('").append(parameter).append(
"',").append(i).append(")" title="转到第")
.append(i).append("页">").append(i).append(
"</a>");
} else {
pageLink
.append("<span title="当前第")
.append(i)
.append(
"页"><font color="red">")
.append(i).append("</font></span>");
}
if (i < end) {
pageLink.append("&nbsp;|&nbsp;");
}
}
pageLink.append("&nbsp;&nbsp;");
pageLink.append("<a href="javascript:").append(ajaxMethod)
.append("('").append(parameter).append("',").append(next)
.append(")" title="转到第").append(next).append(
"页" >");
pageLink.append("<img boder="0" src="");
pageLink.append(getNextPageImage()).append(""/></a>");
pageLink.append("&nbsp;&nbsp;");
pageLink.append("<a href="javascript:").append(ajaxMethod)
.append("('").append(parameter).append("',").append(
pageCount).append(")" title="转到第")
.append(pageCount).append("页" >");
pageLink.append("<img boder="0" src="");
pageLink.append(getLastPageImage()).append(""/></a>");
pageLink.append("&nbsp;&nbsp;");
pageLink.append("</td></tr>");
if (currentPage - 1 < previous) {
currentPage = previous;
}
if (currentPage + 1 > next) {
currentPage = next;
}
int focus = ((currentPage - 1) * getPageSize()) + 1;
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
pageList = parseResultSet(pageList, resultSet, this
.getPageSize(), focus);
} catch (java.sql.SQLException e) {
setPageTotal(0);
} catch (Exception e) {
setPageTotal(0);
}
} else { // 总记录为0 无记录
pageLink
.append("<tr><td align="center" width="100%">");
pageLink.append("没有符合条件的数据!");
pageLink.append("</td></tr>");
}
pageLink.append("</table>");
pageContent.put(AjaxPage.KEY_PAGELIST, pageList);
pageContent.put(AjaxPage.KEY_PAGELINK, pageLink.toString());
return pageContent;
}
/**
* 将一个记录集ResultSet转化成一个存放Object[]的List
*
* @param pageList
* @param resultSet
* @param size
* //取多少条记录
* @param focus
* //从第几条开始去
* @return
*/
public java.util.List parseResultSet(java.util.List pageList,
java.sql.ResultSet resultSet, int size, int focus) {
try {
resultSet.absolute(focus); // 定位到第多少格
resultSet.previous(); // 前移一格
int col = resultSet.getMetaData().getColumnCount();
Object[] obj;
int index = 1;
while (resultSet.next()) {
if (index > size)
break;
obj = new Object[col];
for (int i = 1; i <= col; i++) {
obj[(i - 1)] = resultSet.getString(i);
}
pageList.add(obj);
index++;
}
} catch (java.sql.SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return pageList;
}
}