您的位置:首页 > 编程语言 > Java开发

jsp使用自定义标签taglib分页系列——PaginationTag.java

2008-03-14 15:26 471 查看
导读:
  import com.avantouch.common.web.struts.taglib.table.TableTag;
  /**
  * Title PaginationTag.java

  * Description

  * Copyright: Copyright (c) 2004 AvanTouch,Inc

  * Company: AvanTouch,Inc

  * @author: david 2004-11-30
  * @modifier:
  * @version 1.0
  */
  public abstract class PaginationTag extends TableTag {
  public static final String[] FrameValue =
  { "above", "below", "border", "box", "hsides", "lhs", "rhs", "void", "vsides" };
  public static final String[] DirValue = { "ltr", "rtl" };
  public static final String[] RulesValue = { "all", "cors", "groups", "none", "rows" };
  /**
  * The name of the collection or owning bean.
  */
  protected String name = null;
  /**
  * The scope of the bean specified by the name property, if any.
  */
  protected String scope = null;
  /**
  * The action is required ,link address(.../XXXXAction.do)
  */
  protected String action = null;
  /**
  * The property name containing the collection.
  */
  protected String property = null;
  /**
  * will jump firsthand page number
  * pageIndexValue=0 don't jump
  */
  private int pageIndexValue = 0;
  /**
  * will jump firsthand page number
  */
  protected String pageCount = null;
  /**
  * total page count
  */
  protected int totalPageCount = 0;
  /**
  * data record's collection count
  */
  private int totalSize = 0;
  /**
  * per page display data record's count(<=0 means no limit).
  */
  private int perPageCount = 10;
  /**
  * The length value or attribute name (<=0 means no limit).
  */
  protected String length = null;
  /**
  * will display page number
  */
  protected String currentPage = null;
  private int currentPageValue = 1;
  /**
  * The collection over which we will be iterating.
  */
  private Object collection = null;
  /**
  * Iterator of the elements of this collection, while we are actually
  * running.
  */
  protected Iterator iterator = null;
  /**
  * The message resources for this package.
  */
  protected static MessageResources messages =
  MessageResources.getMessageResources("org.apache.struts.taglib.logic.LocalStrings");
  /**
  * The starting offset (zero relative).
  */
  protected String offset = null;
  /**
  * The actual offset value (calculated in the start tag).
  */
  protected int offsetValue = 0;
  /**
  * isPageCount=true dispaly text for entering pageCount
  * isPageCount=false don't dispaly any text
  */
  protected String isPageCount = null;
  protected boolean displayPageCount = false;
  protected String tableHead = null;
  protected String[] tableheaders = null;
  public String getName() {
  return name;
  }
  public void setName(String name) {
  this.name = name;
  }
  public String getScope() {
  return scope;
  }
  public void setScope(String scope) {
  this.scope = scope;
  }
  public String getProperty() {
  return property;
  }
  public void setProperty(String property) {
  this.property = property;
  }
  public String getAction() {
  return action;
  }
  public void setAction(String action) {
  this.action = action;
  }
  public String getTableHead() {
  return tableHead;
  }
  public void setTableHead(String string) {
  tableHead = string;
  }
  /**
  *
  * <>  * @return
  * @author david 2004-11-30
  * @Since 1.0
  */
  public Object getCollection() throws JspException {
  if (collection == null) {
  collection = lookupCollection();
  }
  return collection;
  }
  /**
  * find data bean
  *
lookupCollection


  * @return
  * @author david 2004-11-30
  * @Since 1.0
  */
  public Object lookupCollection() throws JspException {
  // Acquire the collection we are going to iterate over
  Object collection = this.collection;
  if (collection == null) {
  collection = RequestUtils.lookup(pageContext, name, property, scope);
  }
  if (collection == null) {
  JspException e = new JspException(messages.getMessage("iterate.collection"));
  RequestUtils.saveException(pageContext, e);
  throw e;
  }
  return collection;
  }
  /**
  *
  *
init


  * @throws JspException
  * @author david 2004-12-2
  * @Since 1.0
  */
  /**
  * init attributes's value
  *
init


  * @throws JspException
  * @author david 2004-12-1
  * @Since 1.0
  */
  private void init() throws JspException {
  Object collection = getCollection();
  // Construct an iterator for this collection
  if (collection.getClass().isArray()) {
  try {
  // If we're lucky, it is an array of objects
  // that we can iterate over with no copying
  totalSize = ((Object[]) collection).length;
  iterator = Arrays.asList((Object[]) collection).iterator();
  } catch (ClassCastException e) {
  // Rats -- it is an array of primitives
  int length = Array.getLength(collection);
  ArrayList c = new ArrayList(length);
  for (int i = 0; i getTotalPageCount() || pageIndexValue <0) {
  pageIndexValue = this.totalPageCount;
  }
  } else {
  this.totalPageCount = getTotalPageCount();
  } // end if
  //compute currentPageValue's value
  if (currentPage != null) {
  try {
  currentPageValue = Integer.parseInt(currentPage);
  } catch (NumberFormatException e) {
  Integer currentObejct = (Integer) RequestUtils.lookup(pageContext, currentPage, null);
  if (currentObejct == null) {
  currentPageValue = 1;
  } else {
  currentPageValue = currentObejct.intValue();
  }
  }
  } // end if
  if (isPageCount != null) {
  displayPageCount = Boolean.valueOf(isPageCount).booleanValue();
  }
  } // end init
  /**
  * init attribute iterator's value
  * <>  * @return
  * @throws JspException
  * @author david 2004-12-1
  * @Since 1.0
  */
  private Iterator initIterator() throws JspException {
  if (pageIndexValue >0) {
  currentPageValue = pageIndexValue;
  }
  if (currentPageValue <1)
  currentPageValue = 1;
  if (currentPageValue >(totalPageCount - offsetValue / perPageCount))
  currentPageValue = totalPageCount - offsetValue / perPageCount;
  this.currentPage = String.valueOf(currentPageValue);
  pageIndexValue = currentPageValue;
  if (tableHead != null &&tableHead.trim().equals("")) {
  if (iterator.hasNext()) {
  tableheaders = (String[]) iterator.next();
  }
  } else if (tableHead != null) {
  tableheaders = tableHead.trim().split(",");
  }
  for (int i = 0; i <((currentPageValue - 1) * this.perPageCount + offsetValue); i++) {
  if (iterator.hasNext()) {
  iterator.next();
  } else {
  break;
  }
  }
  return iterator;
  }
  /**
  * init all attributes's value
  * <>  * @throws JspException
  * @author david 2004-12-1
  * @Since 1.0
  */
  public void initAllAttribute() throws JspException {
  init();
  this.iterator = initIterator();
  if (getStyleClass1_TR() != null
  &&getStyleClass2_TR() != null
  &&!getStyleClass1_TR().trim().equals("")
  &&!getStyleClass2_TR().trim().equals("")) {
  pageContext.setAttribute("TRCALSS1", getStyleClass1_TR());
  pageContext.setAttribute("TRCALSS2", getStyleClass2_TR());
  pageContext.setAttribute("currentTRCLASS", "0");
  }
  }
  public int doStartTag() throws JspException {
  initAllAttribute();
  if (iterator.hasNext()) {
  writePaginationInTable();
  return (EVAL_BODY_BUFFERED);
  } else {
  return (SKIP_BODY);
  }
  }
  public int doEndTag() throws JspException {
  StringBuffer buf = new StringBuffer("/n");
  ResponseUtils.write(pageContext, buf.toString());
  release();
  // Continue processing this page
  return (EVAL_PAGE);
  }
  /**
  * write pagination information
  *
writePaginationInTable


  * @throws JspException
  * @author david 2004-12-1
  * @Since 1.0
  */
  public void writePaginationInTable() throws JspException {
  validateFrame();
  validateDir();
  validateRules();
  String prefixPath = ((HttpServletRequest) pageContext.getRequest()).getContextPath();
  StringBuffer buf = new StringBuffer("  // setT_ID(buf);
  setTablePagintionAttributes(buf);
  // prepareEvents_T(buf);
  buf.append(">/n");
  //buf=setTableCaption(buf);
  buf.append(" /n");
  buf.append(
  " /n");
  buf = writeDisplayPageCount(buf);
  buf = AddPageLink(buf);
  buf.append(
  " /n");
  buf.append("
  buf = AddRecordInfoByNumber(buf);
  //or buf=AddRecordInfoByPageCount(buf);
  buf.append("
   buf.append("
/n");
  String output = buf.toString();
  ResponseUtils.write(pageContext, output);
  } // end writePaginationInTable
  /**
  * set pagintion table attributes
  *
setTablePagintionAttributes


  * @param buffer
  * @return
  * @author david 2004-12-3
  * @Since 1.0
  */
  private void setTablePagintionAttributes(StringBuffer buffer) {
  String tempborder = getBorder();
  String tempcellPadding = getCellPadding();
  String tempcellSpacing = getCellSpacing();
  String tempstyleClass = getStyleClass();
  String tempstyle = getStyle();
  setBorder("0");
  setCellPadding("0");
  setCellSpacing("0");
  setStyleClass("lch");
  setStyle(null);
  setTableCommonAttributes(buffer);
  setBorder(tempborder);
  setCellPadding(tempcellPadding);
  setCellSpacing(tempcellSpacing);
  setStyleClass(tempstyleClass);
  setStyle(tempstyle);
  } // end setTablePagintionAttributes
  /**
  * add record number information
  *
AddRecordInfoByNumber


  * @param buffer
  * @return
  * @author david 2004-12-1
  * @Since 1.0
  */
  public StringBuffer AddRecordInfoByNumber(StringBuffer buffer) {
  StringBuffer buf = buffer;
  // add illustrative worlds
  buf.append(" ");
  if (getTitle() != null &&!getTitle().trim().equals("")) {
  buf.append(getTitle() + "");
  }
  buf.append("current page record : /n");
  buf.append(" ");
  buf.append((currentPageValue - 1) * this.perPageCount + 1 + offsetValue);
  int endIndex = currentPageValue * this.perPageCount + offsetValue;
  if (endIndex >this.totalSize)
  endIndex = this.totalSize;
  buf.append(" to "+ endIndex);
  buf.append(" of "+ this.totalSize);
  buf.append("/n");
  return buf;
  } // end AddRecordInfoByNumber
  /**
  * add record page count information
  *
AddRecordInfoByPageCount


  * @param buffer
  * @return
  * @author david 2004-12-1
  * @Since 1.0
  */
  public StringBuffer AddRecordInfoByPageCount(StringBuffer buffer) {
  StringBuffer buf = buffer;
  // add illustrative worlds
  buf.append(" ");
  if (getTitle() != null &&!getTitle().trim().equals("")) {
  buf.append(getTitle() + "");
  }
  buf.append("current page number : /n");
  buf.append(" ");
  buf.append(currentPageValue);
  buf.append(" of "+ this.totalPageCount);
  buf.append("/n");
  return buf;
  } // end AddRecordInfoByPageCount
  /**
  * add page common link
  *
AddPageLink


  * @param buffer
  * @return
  * @author david 2004-12-1
  * @Since 1.0
  */
  public StringBuffer AddPageLink(StringBuffer buffer) {
  String prefixPath = ((HttpServletRequest) pageContext.getRequest()).getContextPath();
  StringBuffer buf = buffer;
  buf.append(" ");
  buf.append("");
  buf.append(
  "");
  buf.append("");
  buf.append("/n");
  buf.append(" ");
  buf.append("");
  buf.append(
  "  buf.append("");
  buf.append("/n");
  if (currentPageValue >1) {
  buf.append(" ");
  buf.append("");
  buf.append(
  "  buf.append("");
  buf.append("/n");
  buf.append(" ");
  buf.append("");
  buf.append(
  "  buf.append("");
  buf.append("/n");
  } else {
  buf.append(" ");
  buf.append(
  "  buf.append("/n");
  buf.append(" ");
  buf.append(
  "  buf.append("/n");
  }
  if (currentPageValue <(totalPageCount - offsetValue / perPageCount)) {
  buf.append("   buf.append("");
  buf.append(
  "  buf.append("");
  buf.append("/n");
  buf.append(" ");
  buf.append("");
  buf.append(
  "  buf.append("");
  buf.append("/n");
  } else {
  buf.append(" ");
  buf.append(
  "  buf.append("/n");
  buf.append(" ");
  buf.append(
  "  buf.append("/n");
  }
  return buf;
  } // end AddPageLink
  public StringBuffer writeDisplayPageCount(StringBuffer buffer) {
  StringBuffer buf = buffer;
  if (isDisplayPageCount()) {
  buf.append(
  " < Page < >>  }
  return buf;
  }
  /**
  * compute total page count
  *
getTotalPageCount


  * @return
  * @author david 2004-11-30
  * @Since 1.0
  */
  public int getTotalPageCount() {
  if (perPageCount == 0 || totalSize == 0)
  return 0;
  if (totalPageCount >0)
  return totalPageCount;
  totalPageCount = totalSize / perPageCount;
  if (totalSize % perPageCount != 0)
  totalPageCount = totalPageCount + 1;
  return totalPageCount;
  }
  /**
  *
  *
getTotal


  * @param iterator
  * @return
  * @author david 2004-11-30
  * @Since 1.0
  */
  private int getTotal(Iterator iterator) {
  int count = IteratorUtils.toArray(iterator).length;
  return count;
  }
  /**
  *
  *
getCurrentPage


  * @return
  * @author david 2004-11-30
  * @Since 1.0
  */
  public String getCurrentPage() {
  return currentPage;
  }
  /**
  *
  *
getPageIndex


  * @return
  * @author david 2004-11-30
  * @Since 1.0
  */
  public int getPageIndexValue() {
  return pageIndexValue;
  }
  /**
  *
  *
getPerPageCount


  * @return
  * @author david 2004-11-30
  * @Since 1.0
  */
  public int getPerPageCount() {
  return perPageCount;
  }
  /**
  *
  *
setCollection


  * @param object
  * @author david 2004-11-30
  * @Since 1.0
  */
  public void setCollection(Object object) {
  collection = object;
  }
  /**
  *
  *
setCurrentPage


  * @param i
  * @author david 2004-11-30
  * @Since 1.0
  */
  public void setCurrentPage(String currentPage) {
  this.currentPage = currentPage;
  }
  /**
  *
  *
setPerPageCount


  * @param i
  * @author david 2004-11-30
  * @Since 1.0
  */
  public void setPerPageCount(int i) {
  perPageCount = i;
  }
  /**
  *
  *
getIterator


  * @return
  * @author david 2004-11-30
  * @Since 1.0
  */
  public Iterator getIterator() {
  return iterator;
  }
  /**
  *
  *
setIterator


  * @param iterator
  * @author david 2004-11-30
  * @Since 1.0
  */
  public void setIterator(Iterator iterator) {
  this.iterator = iterator;
  }
  /**
  * Release all allocated resources.
  */
  public void release() {
  super.release();
  iterator = null;
  collection = null;
  pageIndexValue = 0;
  totalPageCount = 0;
  perPageCount = 10;
  currentPage = null;
  totalSize = 0;
  pageCount = null;
  length = null;
  currentPageValue = 1;
  offset = null;
  offsetValue = 0;
  isPageCount = null;
  displayPageCount = false;
  tableHead = null;
  tableheaders = null;
  }
  public static MessageResources getMessages() {
  return messages;
  }
  public String getPageCount() {
  return pageCount;
  }
  public static void setMessages(MessageResources resources) {
  messages = resources;
  }
  public void setPageCount(String string) {
  pageCount = string;
  }
  public String getLength() {
  return length;
  }
  public void setLength(String length) {
  this.length = length;
  }
  public int getCurrentPageValue() {
  return currentPageValue;
  }
  public int getTotalSize() {
  return totalSize;
  }
  public void setCurrentPageValue(int i) {
  currentPageValue = i;
  }
  public void setTotalSize(int i) {
  totalSize = i;
  }
  public String getOffset() {
  return offset;
  }
  public int getOffsetValue() {
  return offsetValue;
  }
  public void setOffset(String offset) {
  this.offset = offset;
  }
  public void setOffsetValue(int i) {
  offsetValue = i;
  }
  public Object lookup(String name, String scope) {
  Object bean = null;
  HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
  if (scope == null) {
  bean = request.getParameter(name);
  if (bean == null)
  bean = request.getAttribute(name);
  } else if (scope.equalsIgnoreCase("request")) {
  bean = request.getAttribute(name);
  } else if (scope.equalsIgnoreCase("session")) {
  bean = request.getSession().getAttribute(name);
  } else if (scope.equals("application")) {
  pageContext.getServletContext().getAttribute(name);
  }
  return bean;
  }
  public boolean isDisplayPageCount() {
  return displayPageCount;
  }
  public String getIsPageCount() {
  return isPageCount;
  }
  public void setDisplayPageCount(boolean b) {
  displayPageCount = b;
  }
  public void setIsPageCount(String isPageCount) {
  this.isPageCount = isPageCount;
  }
  } // end PaginationTag
  
  Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1653464
本文转自
http://blog.csdn.net/cao_david/archive/2007/06/15/1653464.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: