关于列表分页

来源:互联网 发布:好看的战争小说 知乎 编辑:程序博客网 时间:2024/05/16 23:40

//action里写的函数
private List getMenuList(HttpServletRequest request, List organList) {

  List retList = new ArrayList();
  // 分页显示数据 begin
  String pageNo = (String) request.getParameter("pageNo");
  if (pageNo == null)
   pageNo = "1";

  String pageHTML = PaginationHelper.getPaginationHTML(organList,
    PaginationHelper.PAGE_SIZE, pageNo, request.getContextPath()
      + "/system/url.do?method=searchMenuAdmin");
  retList = PaginationHelper.getPageDataList(organList,
    PaginationHelper.PAGE_SIZE, pageNo);
  request.setAttribute("pageHTML", pageHTML);
  // 分页显示数据 end
  return retList;
 }
//action 调用函数
menuList = this.getMenuList(request, ds.getMenuList(menu, conn)); // 获得分页信息

//jsp里的代码
 <!-- 分页显示数据 BEGIN -->
 <tr align="right">
  <td colspan="11" align="right" bgcolor="#0271B9" class="table_1">
   <%=request.getAttribute("pageHTML")%>
  </td>
 </tr>
 <!-- 分页显示数据 END -->

//原文件代码

/*
 * Created on Oct 18, 2007
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.aegon_cnooc.common.pagination;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangyonggang
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class PaginationHelper {

 public static int PAGE_SIZE = 10;

 /**
  * 返回页面数据
  *
  * @param srcList
  *            -数据源
  * @param pageSize
  *            -每页面记录条数
  * @param pageNo
  *            -第几页
  * @return
  */
 public static List getPageDataList(List srcList, int pageSize, String pageNo) {
  /**
   * pageNo: first/last/1...n
   */
  List pageDataList = new ArrayList();
  if ("first".equals(pageNo)) {// 如果是第一页
   int size = pageSize >= srcList.size() ? srcList.size() : pageSize;// 如果数据源LIST小于每页显示的条数,
   // 返回list.size();

   for (int i = 0; i < size; i++) {
    pageDataList.add(srcList.get(i));// 将要显示的条数从源程序传承过来
   }
  } else if ("last".equals(pageNo)) {// 如果是最后页
   
   //int pos = pageSize >= srcList.size() ? 0
   //  : (srcList.size() / pageSize) * pageSize;// pos是个起始点,标志着最后页应该从数据源里面第pos+1个开始拿数据
   // 如果数据源LIST小于每页显示的条数,最后页没有东西
   // 否则,srcList.size()/pageSize=一个整数n,n*pageSize=前面所有页已经用过的数据条数
   
//    modified by chenfeng 2008.7.10 begin
   int pos = 0;
   if(pageSize < srcList.size()) {
    if(srcList.size() % pageSize ==0)
     pos = (srcList.size() / pageSize-1) * pageSize;
    else
     pos =  (srcList.size() / pageSize) * pageSize;
   }
//    modified by chenfeng 2008.7.10 end
   
   for (int i = pos; i < srcList.size(); i++) {

    pageDataList.add(srcList.get(i));// 将要显示的条数从源程序传承过来
   }
  } else if(!"0".equals(pageNo)){
   /*
    *
    */
   int last = (Integer.parseInt(pageNo)) * pageSize >= srcList.size() ? srcList
     .size()
     : (Integer.parseInt(pageNo)) * pageSize;
   /*
    * 当前页的起始点:Integer.parseInt(pageNo) - 1) * pageSize
    */
   for (int i = (Integer.parseInt(pageNo) - 1) * pageSize; i < last; i++) {
    pageDataList.add(srcList.get(i));
   }
  }
  return pageDataList;
 }

 /**
  *
  * @param srcList
  *            -数据源
  * @param pageSize
  *            -每页面记录条数
  * @param pageNo
  *            -第几页
  * @param action
  *            -查询的action URL
  * @return
  */
 public static String getPaginationHTML(List srcList, int pageSize,
   String str_pageNo, String action) {
  StringBuffer html = new StringBuffer("");
  int pageNo = 1;

  int pages = (srcList.size() % pageSize) == 0 ? (srcList.size() / pageSize)
    : ((srcList.size() / pageSize) + 1);

  if ("first".equals(str_pageNo)) {
   pageNo = 1;
  } else if ("last".equals(str_pageNo)) {
   pageNo = pages;
  } else {
   pageNo = Integer.parseInt(str_pageNo);
  }
  html.append("<script language=javascript>/n");
  html.append("function onPage(page_no){/n");
  html
    .append(" if(page_no==''){/n page_no = document.forms[0].goto_page.value;/n}/n");

  // modified by lijie 2008.1.9 begin
  if (action.indexOf("?") >= 0) {// 当传过来的action中包含参数。
   html.append(" document.forms[0].action=/"" + action
     + "&pageNo=/"+page_no;/n");
  } else {
   html.append(" document.forms[0].action=/"" + action
     + "?pageNo=/"+page_no;/n");
  }
  // html.append("
  // document.forms[0].action=/""+action+"?pageNo=/"+page_no;/n");
  // modified by lijie 2008.1.9 end
  html.append(" document.forms[0].submit();/n");
  html.append("}/n");
  html.append("</script>/n");
  int cur_cnt = 0;
  if(pageNo<=(srcList.size()/pageSize)&&pageNo!=0){
   cur_cnt = pageSize;
  }else{
   cur_cnt = srcList.size()%pageSize;
  }
  html.append("共").append(srcList.size()).append("条记录,本页共").append(cur_cnt).append("条记录");
  html.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
  html.append("<a href=/"#/" onclick=onPage('first')>第一页</a>&nbsp;");
  html.append("<a href=/"#/" onclick=onPage('"
    + ((pageNo - 1) > 0 ? (pageNo - 1) : pageNo) + "')"
    + ">上一页</a>&nbsp;");
  html.append("<a href=/"#/" onclick=onPage('"
    + ((pageNo == pages||srcList.size()==0) ? pageNo : pageNo + 1) + "')"
    + ">下一页</a>&nbsp;");
  html.append("<a href=/"#/" onclick=onPage('last')>最后一页</a>&nbsp;/n");

  html.append("<select name=/"goto_page/" onchange=onPage('') class=text3>/n");
  for (int i = 1; i <= pages; i++) {
   html.append("<option value=/"" + i + "/"");
   if (i == pageNo) {
    html.append(" selected=/"selected/"");
   }
   html.append(">第" + i + "页</option>/n");
  }
  html.append("</select>/n");

//  //System.out.println(html.toString());
  return html.toString();
 }

 public static void main(String[] args) {
  System.out.println(15 % 3);
 }

}

原创粉丝点击