itemstag.java

来自「一个免费wap站」· Java 代码 · 共 84 行

JAVA
84
字号
package com.eline.wap.common.taglib;

import java.util.Iterator;
import java.io.IOException;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.JspWriter;

/**
 * 
 * @author Lucifer
 *
 */
public class ItemsTag extends BodyTagSupport {

	private static final long serialVersionUID = -3666602956888457545L;

    private Iterator iterator;
    private Object item;

    public ItemsTag() {
    	iterator = null;
    	item = null;
	}

    /**
     * 
     * @throws JspException
     */
	public int doStartTag() throws JspException {

		ListTag listTag = (ListTag) TagSupport.findAncestorWithClass(this, ListTag.class);
		if (listTag == null)
			throw new JspTagException("ItemsTag: items tag not inside in list tag");
		
		iterator = listTag.getIterator();
		
		if (iterator == null || !iterator.hasNext())
			return SKIP_BODY;

		item = iterator.next();
		return EVAL_BODY_AGAIN;
	}

	/**
	 * process the body again with the next item if it exists
     * @throws JspException
	 */
	public int doAfterBody() throws JspException {
		if (iterator.hasNext()) {
			item = iterator.next();
			return EVAL_BODY_AGAIN;
		}
		return SKIP_BODY;
	}

	/**
	 * print out the resulting body content to the JSP page and evaluate the rest of the page
     * @throws JspException
	 */
	public int doEndTag() throws JspException {
		try {
			BodyContent bodyContent = getBodyContent();
			if (bodyContent != null) {
				JspWriter jspWriter = bodyContent.getEnclosingWriter();
				jspWriter.print(bodyContent.getString());
			}
		} catch (IOException e) {
			throw new JspException("Error handling items tag: " + e);
		}
		return EVAL_PAGE;
	}

	/**
	 * getter for inner tags
	 * @return
	 */
    public Object getCurrentItem() {
		return item;
	}
}

⌨️ 快捷键说明

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