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

📄 listtag.java

📁 一个用struts tiles的在线影院web系统
💻 JAVA
字号:
package com.eline.common.taglib;

import java.util.Collection;
import java.util.Iterator;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * 
 * @author Lucifer
 *
 */
public abstract class ListTag extends TagSupport {

	private static final long serialVersionUID = 2498336236365140905L;

	protected final String NEXT_PREFIX = "next";
	protected final String PREV_PREFIX = "prev";

	protected Collection coll;
	protected String paramPrefix;
	protected int pageSize;
	protected int pageIndex;
	protected int totalRecords;
	protected int totalPages;

	protected String nextParamValue;
	protected String prevParamValue;
    protected String pageIndexParamValue;

	protected boolean hasNextForm;
	protected boolean hasPreviousForm;

	protected Iterator iterator;

	public ListTag() {
		paramPrefix = null;
		pageSize = -1;
		pageIndex = 0;
		totalRecords = 0;
		totalPages = 0;
		hasNextForm = true;
		hasPreviousForm = false;
		iterator = null;
	}

	/**
	 * 
	 * @throws JspException
	 */
	public int doStartTag() throws JspException {
		if (pageSize < 0)
			throw new JspTagException("ListTag: pageSize out of bound.");
		if (pageIndex < 0)
			throw new JspTagException("ListTag: pageIndex out of bound.");

		initParamPrefix();

		nextParamValue = pageContext.getRequest().getParameter(paramPrefix + NEXT_PREFIX);
		prevParamValue = pageContext.getRequest().getParameter(paramPrefix + PREV_PREFIX);
		pageIndexParamValue = pageContext.getRequest().getParameter(paramPrefix + "pageIndex");

        if((nextParamValue != null || prevParamValue != null) && pageIndexParamValue != null)
            pageIndex = Integer.parseInt(pageIndexParamValue);

        if (pageIndex <= 0) {
        	pageIndex = 0;
        	hasPreviousForm = false;
        } else {
        	hasPreviousForm = true;
        }

        try {
        	coll = findCollection();

            if (coll == null || coll.size() == 0)	// keep body even collection size zero.
            	return SKIP_BODY;

        	iterator = coll.iterator();
        	hasNextForm = needsNextForm();
        } catch (Exception e) {
        	coll = null;
        	return SKIP_BODY;
        }

        return EVAL_BODY_INCLUDE;
	}

    public int doEndTag() {
		coll = null;
		return EVAL_PAGE;
	}

	/**
	 * abstract methods
	 * 
	 * @return
	 */
	protected abstract boolean needsNextForm();

	protected abstract void initParamPrefix();

    protected abstract Collection findCollection() throws Exception;

    /**
     * setter / getter
     * @return
     */
    
    public String getPageIndexParam()
    {
        return "pageIndex";
    }

    public String getNextParam()
    {
        return NEXT_PREFIX;
    }

    public String getPrevParam()
    {
        return PREV_PREFIX;
    }

    public Iterator getIterator() {
		return iterator;
	}

    public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public int getTotalRecords() {
		return totalRecords;
	}

	public void setTotalRecords(int totalRecords) {
		this.totalRecords = totalRecords;
	}

	public boolean hasNextForm() {
		return hasNextForm;
	}

	public boolean hasPreviousForm() {
		return hasPreviousForm;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public String getParamPrefix() {
		return paramPrefix;
	}

	/**
	 * 
	 * Page parameters
	 */
	public void setPageIndex(String pageIndex) {
		this.pageIndex = Integer.parseInt(pageIndex);
	}

	public void setPageSize(String pageSize) {
		this.pageSize = Integer.parseInt(pageSize);
	}
}

⌨️ 快捷键说明

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