java 实现JSP页面上一页及下一页封装

 

import com.coupon.i18n.Messages;


public class PageUtils {

 public PageUtils(){
  
 }
 private int firstPage = 1;

 private int lastPage = 0;

 private int rowsOfPage = 12;

 private int currOfPage = 1;

 // private int totalOfRows = 0;

 private int totalOfPage = 0;

 private int totalRows = 0;

 private String fistLabel = Messages.getString("PageUtil.Home"); //$NON-NLS-1$

 private String preLabel = Messages.getString("PageUtil.Previous"); //$NON-NLS-1$

 private String nextLabel = Messages.getString("PageUtil.Next"); //$NON-NLS-1$

 private String lastLabel = Messages.getString("PageUtil.Last"); //$NON-NLS-1$

 private String pageSpace = "  "; //$NON-NLS-1$

 private String pagePrefix = "javascript:";// //$NON-NLS-1$
 private String sortStr; //$NON-NLS-1$

 public void setTotalPage(int totalRows) {
  this.totalRows = totalRows;
  int rest = totalRows % rowsOfPage, ii = totalRows / rowsOfPage;
  if (rest == 0) {
   totalOfPage = ii;
  } else if (rest > 0) {
   totalOfPage = ii + 1;
  }
  lastPage = totalOfPage;
 }

 public String getPageSum() {
  return Messages.getString("PageUtil.Total") + totalRows + Messages.getString("PageUtil.CounntPage")+pageSpace+ pageSpace+Messages.getString("PageUtil.PageShow") + rowsOfPage //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    + Messages.getString("PageUtil.Count")+pageSpace+ pageSpace+Messages.getString("PageUtil.CurrentPage") + currOfPage + "</b>/<b>" + totalOfPage //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    + "</b>" + pageSpace + pageSpace; //$NON-NLS-1$
 }

 public String packageInput(int num,String url){
  return "<input type='hidden' value='"+num+
  "' name='page' id='page'/><input onChange='"+url+"(1)' type='text' style='width:25px; height:25px;' value='"+rowsOfPage+
  "' id='pagesize' name='pagesize'/><input type='hidden' name='sortStrPre' value='"+sortStr+"' id='sortStrPre'/><input type='hidden' name='profix' value='"+url+"' id='profix'/>";
 }
 
 public String packageSelect(){
  String select="<select id='gotoPage' onchange='"+pagePrefix+"(this.value)'>"; //$NON-NLS-1$ //$NON-NLS-2$
  for(int i=1;i<=totalOfPage;i++){
   if(i==currOfPage)
    select+="<option selected='selected' value='"+i+"'>"+i+"</option>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
   else
    select+="<option value='"+i+"'>"+i+"</option>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  }
  return select+"</select>"; //$NON-NLS-1$
 }
 public String getPageHref(int curr) { // NOPMD by XUPO
  if (curr < firstPage)
   curr = firstPage;
  if (curr > lastPage)
   curr = lastPage;

  int first = firstPage, pre = curr - 1, next = curr + 1, last = lastPage;
  if (pre < first)
   pre = first;
  if (next > last)
   next = last;

  
  String page = null;
  if (curr == 1 && totalOfPage == 1) {
   page = fistLabel + pageSpace + preLabel + pageSpace + nextLabel
     + pageSpace + lastLabel;
  } else if (curr == first) {
   page = fistLabel + pageSpace + preLabel + pageSpace + "<a href=\"" //$NON-NLS-1$
     + pagePrefix+"("+next+")"+ "\">" + nextLabel + "</a>" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
     + pageSpace + "<a href=\"" + pagePrefix+"("+last+")"+ "\">" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
     + lastLabel + "</a>"; //$NON-NLS-1$
  } else if (curr > first && curr < last) {
   page = "<a href=\"" + pagePrefix +"("+ first +")"+ "\">" + fistLabel //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
     + "</a>" + pageSpace + "<a href=\"" +pagePrefix  +"("+ pre+")" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
     + "\">" + preLabel + "</a>" + pageSpace + "<a href=\"" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
     + pagePrefix +"("+ next +")"+ "\">" + nextLabel + "</a>" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
     + pageSpace + "<a href=\"" + pagePrefix +"("+ last +")"+ "\">" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
     + lastLabel + "</a>"; //$NON-NLS-1$
  } else if (curr == last) {
   page = "<a href=\"" + pagePrefix +"("+ first +")"+ "\">" + fistLabel //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
     + "</a>" + pageSpace + "<a href=\"" + pagePrefix + "("+pre+")" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
     + "\">" + preLabel + "</a>" + pageSpace + nextLabel //$NON-NLS-1$ //$NON-NLS-2$
     + pageSpace + lastLabel;
  }
  // System.out.println(page);
  return page;
 }

 public String getPageTool(String pageNo,int pageSize, String actionStr, int totalRows) {
  rowsOfPage=pageSize;
  setTotalPage(totalRows);
  pagePrefix += actionStr;
  // totalOfRows = totalRows;
  int num = 1;
  if (pageNo != null && pageNo.matches("(\\-|\\+)?[1-9]{1}\\d*")) { //$NON-NLS-1$
   num = Integer.parseInt(pageNo.toString());
   if (num < 0)
    num = 1;
   else if (num > totalOfPage)
    num = totalOfPage;
   currOfPage = num;
  }
  return getPageSum() +packageInput(num,actionStr)+ getPageHref(num)+packageSelect();
 }

 public int getStartRow() {
  return (currOfPage - 1) * rowsOfPage;
 }

 public int getRowsOfPage() {
  return rowsOfPage;
 }

 public void setRowsOfPage(int rowsOfPage) {
  this.rowsOfPage = rowsOfPage;
 }

 public String getSortStr() {
  return sortStr;
 }

 public void setSortStr(String sortStr) {
  this.sortStr = sortStr;
 }

 

 

 

翻页实体类:

import java.util.List;

public class PageResult {
    //用于分页
 private String pageTool = null;
 //用于保存查询后的结果
 private List pageList = null;

 public String getPageTool() {
  return pageTool;
 }
 public void setPageTool(String pageTool) {
  this.pageTool = pageTool;
 }

 public List getPageList() {
  return pageList;
 }
 public void setPageList(List pageList) {
  this.pageList = pageList;
 }

}

 

 

调用实现:

PageUtils pageUtil=new PageUtils();
   pageUtil.setSortStr(“排序字段”);
   pageResult.setPageList(对用LIst);
   pageResult.setPageTool(pageUtil.getPageTool(页数+"",每页数, 对应Function, 总数));

你可能感兴趣的:(java,jsp,list,String,REST,import)