/**
* 此文件描述的是:PageBean.java
*/
package cn.com.sohocat.util;
import cn.com.sohocat.common.Common;
/**
* @author blaiu
*
*/
public class PageBean {
private String keyword; //关键字
private String urlAction; //超链action路径
private int pageNo; //pageNo:页号表示当前是第几页
private int totalRecords; //totalRecords:查询的总记录数
private int pageSize = 10; //pageSize:页面可以显示多少条记录:初始化为10
private int totalPages; //totalPages:总共的页数
private int startRecord; //startRecord:起始记录
private int endRecord; //endRecord:末记录
private int firstPage = 1; //firstPage:第一页
private int lastPage; //lastPage:最后一页
private int nextPage; //nextPage:下一页
private int prevPage; //prevPage:上一页
private int navStartNo; //navStartNo:当前显示的起始页
private int navEndNo; //navEndNo:当前显示的最后一页
private int navSize = 10; //页面上要显示的页数
/**
*
* @param pageNo 页号
* @param totalRecords 总记录数
* @param keyword 关键字
* @param urlAction 路径
* @param pageSize 页面显示的记录数 默认10(当pageSize等于0时)
* @param navSize 页面显示的页数 默认10(当navSize等于0时)
*/
public PageBean(int pageNo, int totalRecords, String keyword, String urlAction, int pageSize, int navSize) {
this.keyword = keyword;
this.pageNo = pageNo;
this.totalRecords = totalRecords;
this.urlAction = urlAction;
if(pageSize != 0) {
this.pageSize = pageSize;
}
if(navSize != 0) {
this.navSize = navSize;
}
/**
* Math.ceil((double) (totalRecords / pageSize)):返回最小的(最接近负无穷大)double 值,该值大于或等于参数,并且等于某个整数
* totalPages:总页数
*/
this.totalPages = this.totalRecords / this.pageSize;
if((this.totalRecords % this.pageSize)>0) {
this.totalPages = this.totalPages + 1;
}
this.startRecord = (this.pageNo - 1) * this.pageSize;
this.endRecord = Math.min(this.startRecord + this.pageSize, this.totalRecords);
this.lastPage = this.totalPages;
/**
* Math.min(pageNo + 1, lastPage):比较两个参数的值,返回较小的一个
*/
this.nextPage = Math.min(this.pageNo + 1, lastPage);
/**
* Math.max:比较两个参数的值,返回较大的一个
*/
this.prevPage = Math.max(1, this.pageNo - 1);
this.navStartNo = Math.max(1, this.pageNo - this.pageSize / 2);
this.navEndNo = Math.min(this.navStartNo + this.navSize, this.lastPage);
if (this.navEndNo - this.navStartNo < this.navSize) {
this.navStartNo = Math.max(this.navEndNo - this.navSize, 1);
}
}
public int getFirstPage() {
return firstPage;
}
public void setFirstPage(int firstPage) {
this.firstPage = firstPage;
}
public int getLastPage() {
return lastPage;
}
public void setLastPage(int lastPage) {
this.lastPage = lastPage;
}
public int getNavEndNo() {
return navEndNo;
}
public void setNavEndNo(int navEndNo) {
this.navEndNo = navEndNo;
}
public int getNavSize() {
return navSize;
}
public void setNavSize(int navSize) {
this.navSize = navSize;
}
public int getNavStartNo() {
return navStartNo;
}
public void setNavStartNo(int navStartNo) {
this.navStartNo = navStartNo;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPrevPage() {
return prevPage;
}
public void setPrevPage(int prevPage) {
this.prevPage = prevPage;
}
public int getStartRecord() {
return startRecord;
}
public void setStartRecord(int startRecord) {
this.startRecord = startRecord;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String toString() {
StringBuffer sb = new StringBuffer();
/**
* pageNo不等于1表示为:
*/
sb.append("共"+this.totalPages+"页 ");
String pageNoStr = "document.getElementById(\"pageNo\").value";
if (null != this.keyword) {
if (this.pageNo != 1 && this.totalPages > 0) {
sb.append("<a href=\"'"+urlAction+"'&pageNo=" + this.firstPage + "&keyword="
+ Common.encode(keyword) + "\">首页</a>");
sb.append(" ");
sb.append("<a href=\""+this.urlAction+"&pageNo=" + this.prevPage + "&keyword="
+ Common.encode(this.keyword) + "\">上页</a>");
sb.append(" ");
}
// for (int i = this.navStartNo; i <= this.navEndNo; i++) {
// if (this.pageNo != i) {
// sb.append("[<a href=\""+this.urlAction+"&pageNo=" + i
// + "&keyword=" + Common.encode(this.keyword) + "\">" + i + "</a>]");
// sb.append(" ");
//
// } else {
// sb.append("[" + i + "]");
// sb.append(" ");
// }
// }
if (this.pageNo != this.lastPage && this.totalPages > 0) {
sb.append("<a href=\""+this.urlAction+"&pageNo=" + this.nextPage + "&keyword="
+ Common.encode(this.keyword) + "\">下页</a>");
sb.append(" ");
sb.append("<a href=\"search.action?pageNo=" + this.lastPage + "&keyword="
+ Common.encode(keyword) + "\">末页</a>");
sb.append(" ");
}
} else {
if (this.pageNo != 1 && this.totalPages > 0) {
sb.append("<a href=\""+urlAction+"&pageNo=" + this.firstPage + "\">首页</a>");
sb.append(" ");
sb.append("<a href=\""+this.urlAction+"&pageNo=" + this.prevPage + "\">上页</a>");
sb.append(" ");
}
// for (int i = this.navStartNo; i <= this.navEndNo; i++) {
// if (pageNo != i) {
// sb.append("[<a href=\""+this.urlAction+"&pageNo=" + i + "\">" + i + "</a>]");
// sb.append(" ");
// } else {
// sb.append("[" + i + "]");
// sb.append(" ");
// }
// }
if (this.pageNo != this.lastPage && this.totalPages > 0) {
sb.append("<a href=\""+this.urlAction+"&pageNo=" + this.nextPage + "\">下页</a>");
sb.append(" ");
sb.append("<a href=\""+urlAction+"&pageNo=" + this.lastPage + "\">尾页</a>");
}
}
sb.append(" 第<input type=\"text\" size=\"2\" id=\"pageNo\" value=\""+this.pageNo+"\"/>页");
sb.append(" ");
sb.append("<a href=\"javascript:jump()\">跳页</a>");
sb.append("<script>function jump(){var page =document.getElementById(\"pageNo\").value;if(page>"+this.totalPages+"){alert(\"您输入的值太大\");return}else if(page<0){alert(\"您输入的值太小\");return}else{document.location=\""+this.urlAction+"&pageNo=\"+"+pageNoStr+"+\"&keyword="+Common.encode(this.keyword)+"\"}}</script>");
return sb.toString();
}
public String getUrlAction() {
return urlAction;
}
public void setUrlAction(String urlAction) {
this.urlAction = urlAction;
}
public int getEndRecord() {
return endRecord;
}
public void setEndRecord(int endRecord) {
this.endRecord = endRecord;
}
}
action
public ActionForward link(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
log.debug("UserAction--->link--->start");
String pageNoStr = request.getParameter("pageNo");
String userName = request.getParameter("userName");
String isActive = request.getParameter("isActive");
String rank = request.getParameter("rank");
String isAudited = "";
//存放每页用户userid集合
StringBuffer buffer = new StringBuffer();
List<HoUserSohoTeam> sohoTeams = new ArrayList<HoUserSohoTeam>();
List<HoUserSohoVip> sohovips = new ArrayList<HoUserSohoVip>();
//去除重复key,临时存放
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int count = iHoUser.queryALLCount(userName,isActive,isAudited, rank);
int pageNo;
if("".equals(pageNoStr)||pageNoStr == null ) {
pageNo = 1;
}
try {
int i = Integer.parseInt(pageNoStr);
if(i < 0) {
pageNo = 1;
}else {
pageNo = i;
}
} catch(NumberFormatException e) {
pageNo = 1;
}
String url;
if(userName==null && isActive==null && rank==null)
url = "user.do?method=link";
else
url = "user.do?method=link&userName="+userName+"&isActive="+isActive+"&rank="+rank+"&isAudited="+isAudited;
PageBean pageBean = new PageBean(pageNo, count, null,url, 0, 0);
request.setAttribute("page", pageBean);
request.setAttribute("pageStr", pageBean.toString());
request.setAttribute("pageNo", pageBean.getPageNo());
request.setAttribute("userName", userName);
request.setAttribute("isActive", isActive);
request.setAttribute("isAudited", isAudited);
request.setAttribute("rank", rank);
List<HoUser> list = iHoUser.queryHoUserByIf(userName, isActive, rank, isAudited,pageBean.getStartRecord(), pageBean.getPageSize());
for(int i = 0; i < list.size(); i++){
int id = list.get(i).getUserId();
if(!map.containsKey(id)){
map.put(id, id);
if(map.size() != 1){
buffer.append(", ");
}
buffer.append(id);
}
}
// log.debug("11buffer:"+buffer.toString());
if(!Common.isEmpty(buffer.toString())){
sohoTeams = iHoUserSohoTeam.queryByUserIDs(buffer.toString());
}
// for(int i = 0; i < sohoTeams.size(); i++){
// log.debug("-------->"+sohoTeams.get(i).getId()+":"+sohoTeams.get(i).getTeamId());
// }
buffer.delete(0, buffer.length());
map.clear();
for(int i = 0; i < sohoTeams.size(); i++){
int id = sohoTeams.get(i).getTeamId();
if(!map.containsKey(id)){
map.put(id, id);
if(map.size() != 1){
buffer.append(", ");
}
buffer.append(id);
}
}
// log.debug("22buffer:"+buffer.toString());
if(!Common.isEmpty(buffer.toString())){
sohovips = iHoUserSohoVip.queryByTeamIDs(buffer.toString());
}
request.setAttribute("sohovips", sohovips);
log.debug("sohoTeams:"+sohoTeams.size());
request.setAttribute("sohoTeams", sohoTeams);
request.setAttribute("lists", list);
buffer = null;
map = null;
return mapping.findForward("user");
}
在页面上加这个句就可以了
<div class="a">找到<span id="totalRecords">${page.totalRecords }<span>条记录。</div>
<div class="b" style="margin-right:50px;">${pageStr} </div>