⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pagertag.java

📁 这个是一个网站的简单源码
💻 JAVA
字号:
  package com.jewel.tag.pagination;
/**
 * <p>Title: www.entel.com.cn</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: entel</p>
 * @author chendesheng
 * @version 1.0
 */

import java.lang.reflect.*;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import org.apache.commons.collections.*;
import org.apache.struts.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class PagerTag extends BodyTagSupport implements PagerInterface {
  protected Iterator iterator = null;
  protected int lengthCount = 0;
  protected int lengthValue = 0;
  protected MessageResources messages =
      MessageResources.getMessageResources(
      "org.apache.struts.taglib.logic.LocalStrings");
  protected int offsetValue = 0;
  protected boolean started = false;
  protected Object collection = null;

  public Object getCollection() {
    return (this.collection);
  }

  public void setCollection(Object collection) {
    this.collection = collection;
  }

  protected String id = null;

  public String getId() {
    return (this.id);
  }

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

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

  protected String indexId = null;

  public String getIndexId() {
    return (this.indexId);
  }

  public void setIndexId(String indexId) {
    this.indexId = indexId;
  }

  protected String length = null;

  public String getLength() {
    return (this.length);
  }

  public void setLength(String length) {
    this.length = length;
  }

  protected String name = null;

  public String getName() {
    return (this.name);
  }

  public void setName(String name) {
    this.name = name;
  }

  protected String offset = null;

  public String getOffset() {
    return (this.offset);
  }

  public void setOffset(String offset) {
    this.offset = offset;
  }

  protected String property = null;

  public String getProperty() {
    return (this.property);
  }

  public void setProperty(String property) {
    this.property = property;
  }

  protected String scope = null;

  public String getScope() {
    return (this.scope);
  }

  public void setScope(String scope) {
    this.scope = scope;
  }

  protected String type = null;

  public String getType() {
    return (this.type);
  }

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

  public int doStartTag() throws JspException {
    Object collection = this.collection;
    HttpServletRequest hsr = (HttpServletRequest) pageContext.getRequest();
    HttpSession session = hsr.getSession(true);
    String sessionID = session.getId();
    int AllCountElement = 0;
    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;
    }
    if (collection.getClass().isArray()) {
      try {
        iterator = Arrays.asList( (Object[]) collection).iterator();
        //Common.AllElementCount = Array.getLength(collection);
        session.setAttribute(sessionID+"allcount",""+Array.getLength(collection));
        AllCountElement = Array.getLength(collection);
      } catch (ClassCastException e) {
        int length = Array.getLength(collection);
        //Common.AllElementCount = length;
        session.setAttribute(sessionID+"allcount",""+length);
        AllCountElement = length;
        ArrayList c = new ArrayList(length);
        for (int i = 0; i < length; i++) {
          c.add(Array.get(collection, i));
        }
        iterator = c.iterator();
      }
    } else if (collection instanceof Collection) {
      iterator = ( (Collection) collection).iterator();
      //Common.AllElementCount = ( (Collection) collection).size();
      session.setAttribute(sessionID+"allcount",""+( (Collection) collection).size());
      AllCountElement = ( (Collection) collection).size();
    } else if (collection instanceof Iterator) {
      iterator = (Iterator) collection;
      //Common.AllElementCount = Array.getLength(collection);
      session.setAttribute(sessionID+"allcount",""+Array.getLength(collection));
      AllCountElement = Array.getLength(collection);
    } else if (collection instanceof Map) {
      iterator = ( (Map) collection).entrySet().iterator();
      //Common.AllElementCount = ( (Map) collection).size();
      session.setAttribute(sessionID+"allcount",""+( (Map) collection).size());
      AllCountElement = ( (Map) collection).size();
    } else if (collection instanceof Enumeration) {
      iterator = IteratorUtils.asIterator( (Enumeration) collection);
      //Common.AllElementCount = Array.getLength(collection);
      session.setAttribute(sessionID+"allcount",""+Array.getLength(collection));
      AllCountElement = Array.getLength(collection);
    } else {
      JspException e = new JspException(messages.getMessage("iterate.iterator"));
      RequestUtils.saveException(pageContext, e);
      throw e;
    }
    // set Attribute of the "offset"
    String tmpoffset = hsr.getParameter("offset");
    if(tmpoffset != null && !tmpoffset.trim().equalsIgnoreCase("")){
      setOffset(tmpoffset);
    }else{
      try{
        String position = (String)session.getAttribute(sessionID+"offset");
        if(position == null||position.equalsIgnoreCase(""))
          position = "0";
        setOffset(position);
      }catch(Exception ex){
        setOffset("0");
      }
    }
    offsetValue = Integer.parseInt(getOffset());

    if (offsetValue < 0 || offsetValue>AllCountElement ) {
      offsetValue = 0;
    }
    // set out offset Value
    //Common.offset = offsetValue;
    session.setAttribute(sessionID+"offset",""+offsetValue);
    // set Attribute of the "length"
    String tmplength = hsr.getParameter("length");
    if(tmplength != null && !tmplength.trim().equalsIgnoreCase(""))
      length = tmplength;

    if (length == null) {
      lengthValue = 0;
    } else {
      try {
        lengthValue = Integer.parseInt(length);
      } catch (NumberFormatException e) {
        Integer lengthObject = (Integer) RequestUtils.lookup(pageContext,
            sessionID+"length", "session");
        if (lengthObject == null) {
          lengthValue = 0;
        } else {
          lengthValue = lengthObject.intValue();
        }
      }
    }
    if (lengthValue < 0) {
      lengthValue = 0;
    }
    lengthCount = 0;

    // set out the "length" attribute to Common.pageLength
    //Common.pageLength = lengthValue;
    session.setAttribute(sessionID+"length",""+lengthValue);

    for (int i = 0; i < offsetValue; i++) {
      if (iterator.hasNext()) {
        iterator.next();
      }
    }

    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()));
      }
      return (EVAL_BODY_AGAIN);
    } else {
      return (SKIP_BODY);
    }
  }

  public int doAfterBody() throws JspException {
    if (bodyContent != null) {
      ResponseUtils.writePrevious(pageContext, bodyContent.getString());
      bodyContent.clearBody();
    }

    if ( (lengthValue > 0) && (lengthCount >= lengthValue)) {
      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_AGAIN);
    } else {
      return (SKIP_BODY);
    }
  }

  public int doEndTag() throws JspException {
    started = false;
    iterator = null;
    return (EVAL_PAGE);
  }

  public void release() {
    super.release();
    iterator = null;
    lengthCount = 0;
    lengthValue = 0;
    offsetValue = 0;
    id = null;
    collection = null;
    length = null;
    name = null;
    offset = null;
    property = null;
    scope = null;
    started = false;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -