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

15 篇文章 0 订阅

package com.avantouch.common.web.struts.taglib;

import javax.servlet.jsp.JspException;

import org.apache.struts.util.ResponseUtils;
/**
 * <p>Title ListTag.java</p>
 * <p>Description  </p>
 * <p>Copyright:  Copyright (c) 2004 AvanTouch,Inc</p>
 * <p>Company:  AvanTouch,Inc</p>
 * @author:  david 2004-11-30
 * @modifier:
 * @version  1.0
 */
public class ListTag extends PaginationTag {

 /**
 * The name of the scripting variable to be exposed.
 */
 private String id = null;

 /**
 * The number of elements we have already rendered.
 */
 protected int lengthCount = 0;

 /**
 * Has this tag instance been started?
 */
 protected boolean started = false;

 /**
  * The name of the scripting variable to be exposed as the current index.
  */
 protected String indexId = null;

 /**
 * <p>Return the zero-relative index of the current iteration through the
 * loop.  If you specify an <code>offset</code>, the first iteration
 * through the loop will have that value; otherwise, the first iteration
 * will return zero.</p>
 *
 * <p>This property is read-only, and gives nested custom tags access to
 * this information.  Therefore, it is <strong>only</strong> valid in
 * between calls to <code>doStartTag()</code> and <code>doEndTag()</code>.
 * </p>
 */

 public int getIndex() {
  if (started)
   return (offsetValue + lengthCount - 1);
  else
   return (0);
 }

 /**
 * The Java class of each exposed element of the collection.
 */
 protected String type = null;

 /**
  * Make the next collection element available and loop, or
  * finish the iterations if there are no more elements.
  * p><code>doStartTag</code></p>
  * @return
  * @throws javax.servlet.jsp.JspException if a JSP exception has occurred
  * @author david 2004-11-30
  * @Since 1.0
  */
 public int doStartTag() throws JspException {
  //init all attributes's value
  initAllAttribute();
  lengthCount = 0;
  // Store the first value and evaluate, or skip the body if none
  if (iterator.hasNext()) {
   Object element = iterator.next();
   if (element == null) {
    pageContext.removeAttribute(id);
   } else {
    pageContext.setAttribute(id, element);
   }
   lengthCount++;
   started = true;
   if (indexId != null) {
    pageContext.setAttribute(indexId, new Integer(getIndex()));
   }
   //write pagination information
   writePaginationInTable();
   // write table
   writeTableHead();
   return (EVAL_BODY_BUFFERED);
  } else {
   return (SKIP_BODY);
  }

 }

 public void writeTableHead() throws JspException {

  StringBuffer buf = new StringBuffer("<TABLE");
  setT_ID(buf);
  setTableCommonAttributes(buf);
  prepareEvents_T(buf);
  buf.append(">/n");
  buf = setTableCaption(buf);
  buf = wirteTableHeader(buf);
  ResponseUtils.write(pageContext, buf.toString());
 }
 public StringBuffer wirteTableHeader(StringBuffer buf) {
  if (tableheaders == null || tableheaders.length == 0)
   return buf;
  buf.append(" <TR nowrap>/n");  
  for(int i=0;i<tableheaders.length;i++){
   buf.append("  <TH>"+tableheaders[i]+"</TH>/n"); 
  }
  buf.append(" </TR>/n");
  return buf;
 }
 /**
  *
  * p><code>doAfterBody</code></p>
  * @return
  * @throws javax.servlet.jsp.JspException
  * @author david 2004-11-30
  * @Since 1.0
  */
 public int doAfterBody() throws JspException {
  // Render the output from this iteration to the output stream
  if (bodyContent != null) {
   ResponseUtils.writePrevious(pageContext, bodyContent.getString());
   bodyContent.clearBody();
  }

  // Decide whether to iterate or quit
  if ((getTotalSize() > 0) && (lengthCount >= getPerPageCount())) {
   return (SKIP_BODY);
  }

  if (iterator.hasNext()) {
   Object element = iterator.next();
   if (element == null) {
    pageContext.removeAttribute(id);
   } else {
    pageContext.setAttribute(id, element);
   }
   lengthCount++;
   if (indexId != null) {
    pageContext.setAttribute(indexId, new Integer(getIndex()));
   }
   return (EVAL_BODY_BUFFERED);
  } else {
   return (SKIP_BODY);
  }

 }

 /**
  *
  * p><code>doEndTag</code></p>
  * @return
  * @throws javax.servlet.jsp.JspException
  * @author david 2004-11-30
  * @Since 1.0
  */
 public int doEndTag() throws JspException {
  // Clean up our started state
  started = false;
  ResponseUtils.write(pageContext, "</TABLE>/n");
  pageContext.removeAttribute(id);
  if (indexId != null)
   pageContext.removeAttribute(indexId);
  release();
  // Continue processing this page
  return (EVAL_PAGE);

 }

 /**
 * Release all allocated resources.
 */
 public void release() {
  super.release();
  id = null;
  started = false;
  type = null;
  lengthCount = 0;
 }

 public String getId() {
  return id;
 }

 public String getIndexId() {
  return indexId;
 }

 public String getType() {
  return type;
 }

 public void setId(String id) {
  this.id = id;
 }

 public void setIndexId(String string) {
  indexId = string;
 }

 public void setType(String type) {
  this.type = type;
 }

} // end ListTag
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值