You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
6.8 KiB

package util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import dao.CommDAO;
public class PageManager {
private PageManager() {
}
// Ĭ<><C4AC>һҳ<D2BB><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC>
public static final int DEFAULTPAGESIZE = 20;
// <20><>ҳ<EFBFBD><D2B3>
public static final int segment = 10;
// <20><>ǰҳ<C7B0><D2B3>
protected int currentPage;
// һҳ<D2BB><D2B3><EFBFBD><EFBFBD>
protected int pageSize;
// <20><>ҳ<EFBFBD><D2B3>
protected long pageNumber;
// <20>ܼ<EFBFBD>¼<EFBFBD><C2BC>
protected long count;
// <20><><EFBFBD><EFBFBD>
protected Collection collection;
// <20><><EFBFBD>ݲ<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
protected CommDAO dao = new CommDAO();
// <20><><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD>
protected String info;
// <20><><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
protected String path;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
protected HttpServletRequest request;
/*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD>Ǽӵ<EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ
*/
protected String parameter = "";
/**
*
* @param <EFBFBD><EFBFBD>һҳ<EFBFBD>ķ<EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param һҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD>
* @param <EFBFBD><EFBFBD>ǰHttpServletRequest<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
protected PageManager(String path, int pageSize, HttpServletRequest request) {
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>dao<61><6F><EFBFBD><EFBFBD>
this.currentPage = 1;
this.pageNumber = 1;
this.count = 0;
this.pageSize = pageSize <= 0 ? DEFAULTPAGESIZE : pageSize;
this.request = request;
this.path = path;
request.setAttribute("page", this);
try {
this.currentPage = Integer.parseInt(request
.getParameter("currentPage")) <= 0 ? 1 : Integer
.parseInt(request.getParameter("currentPage"));
} catch (Exception e) {
try {
this.currentPage = Integer.parseInt((String) request
.getSession().getAttribute("currentPage"));
} catch (Exception e1) {
this.currentPage = 1;
}
}
}
/**
*
* @param <EFBFBD><EFBFBD>һҳ<EFBFBD>ķ<EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param һҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD>
* @param <EFBFBD><EFBFBD>ǰHttpServletRequest<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
public static PageManager getPage(String path, int pageSize,
HttpServletRequest request) {
return new PageManager(path, pageSize, request);
}
/**
*
*
* @param hql<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
*/
public void doList(String hql) {
this.count = this.dao.select(hql).size();
if (this.count != 0) {
this.pageNumber = count % this.pageSize == 0 ? this.count
/ this.pageSize : this.count / this.pageSize + 1;
if (this.currentPage > this.pageNumber)
this.currentPage = (int) this.pageNumber;
}
this.request.getSession().setAttribute("currentPage",
String.valueOf(this.currentPage));
this.collection = this.dao.select(hql,
this.currentPage , this.pageSize);
this.refreshUrl();
}
/**
*
* @param <EFBFBD><EFBFBD>ѯ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD>Ͳ<EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
public void addParameter(List parameter) {
StringBuffer para = new StringBuffer("");
if (parameter != null && parameter.size() != 0) {
Iterator iterator = parameter.iterator();
while (iterator.hasNext()) {
para.append("&").append(iterator.next().toString());
}
}
this.parameter = para.toString();
}
/**
* ˢ<EFBFBD>·<EFBFBD>ҳ·<EFBFBD><EFBFBD>
*
*/
protected void refreshUrl() {
StringBuffer buf = new StringBuffer();
buf.append("<font color='#1157B7'><3E><>").append(count);
buf.append("<22><>");
buf.append("&nbsp;&nbsp;");
buf.append("<22><>").append(this.currentPage).append("/").append(
this.pageNumber).append("ҳ");
buf.append("&nbsp;&nbsp;&nbsp;&nbsp;");
if (this.currentPage == 1)
buf.append("<22><>ҳ");
else
buf.append("<a href='").append(this.path).append("&currentPage=1")
.append(parameter)
.append("' class='ls'>").append("<22><>ҳ")
.append("</a>");
// ////////////////////////#1157B7
buf.append("&nbsp;&nbsp;&nbsp;&nbsp;");
if (this.currentPage > 1) {
buf.append("<a href='").append(this.path).append("&currentPage=")
.append(currentPage - 1).append(parameter).append(
"' class='ls'>").append("<22><>ҳ")
.append("</a>");
} else {
buf.append("<22><>ҳ");
}
buf.append("&nbsp;&nbsp;");
int currentSegment = this.currentPage % segment == 0 ? this.currentPage
/ segment : this.currentPage / segment + 1;
/*for (int i = 1; i <= this.pageNumber; i++) {
if (this.currentPage == i)
buf.append("<font color='red'>").append(i).append("</font>");
else
buf.append("<a href='").append(this.path).append(
"&currentPage=").append(i).append(parameter).append(
"' class='ls'>[").append(i).append(
"]</a>");
}*/
buf.append("&nbsp;&nbsp;");
if (this.currentPage < this.pageNumber) {
buf.append("<a href='").append(this.path).append("&currentPage=")
.append(currentPage + 1).append(parameter).append(
"' class='ls'>").append("<22><>ҳ")
.append("</a>");
} else {
buf.append("<22><>ҳ");
}
buf.append("&nbsp;&nbsp;&nbsp;&nbsp;");
if (this.currentPage == this.pageNumber)
buf.append("ĩҳ&nbsp;&nbsp;");
else
buf.append("<a href='").append(this.path).append("&currentPage=")
.append(this.pageNumber).append(parameter).append(
"' class='ls'>").append("ĩҳ")
.append("</a></font>&nbsp;&nbsp;");
// ////////////////////
// for (int i = 0; i < this.pageNumber; i++) {
// if (this.currentPage == i + 1) {
// buf.append("<font color=red>[" + (i + 1) + "]</font>").append(
// "&nbsp;");
// } else {
// buf.append("<a href='").append(this.path).append(
// "&currentPage=").append(i + 1).append(parameter)
// .append("' style='TEXT-DECORATION:none'>").append(
// "[" + (i + 1) + "]").append("</a>&nbsp;");
// }
// }
buf.append("<select onchange=\"javascript:window.location='").append(
this.path).append("&currentPage='+").append(
"this.options[this.selectedIndex].value").append(parameter)
.append("\">");
for (int i = 0; i < this.pageNumber; i++) {
if (this.currentPage == i + 1)
buf.append("<option value=" + (i + 1)
+ " selected=\"selected\">" + (i + 1) + "</option>");
else
buf.append("<option value=" + (i + 1) + ">" + (i + 1)
+ "</option>");
}
buf.append("</select>");
this.info = buf.toString();
}
public Collection getCollection() {
return collection;
}
public long getCount() {
return count;
}
public int getCurrentPage() {
return currentPage;
}
public long getPageNumber() {
return pageNumber;
}
public int getPageSize() {
return pageSize;
}
public String getInfo() {
return info;
}
public static ArrayList<HashMap> getPages(String url,int pagesize,String sql,HttpServletRequest request )
{
PageManager pageManager = PageManager.getPage(url, pagesize, request);
pageManager.doList(sql);
PageManager bean = (PageManager) request.getAttribute("page");
ArrayList<HashMap> nlist = (ArrayList) bean.getCollection();
return nlist;
}
}