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

📄 varhellotag.java

📁 介绍了j2ee开发常用的学习知识,如servlet,javamail,EJB等知识,并有项目源码.
💻 JAVA
字号:
/*
 * VarHelloTag.java
 *
 * Created on 2007年11月28日, 下午7:31
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.tag;

import java.util.ArrayList;
import java.io.IOException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class VarHelloTag extends BodyTagSupport {
    /** List of names passed in */
    private ArrayList			names;
    private ArrayList			positions;
    /** Where we're up to in iterating over the body content */
    private int				index;
    /** Output we're building up while iterating over the body content */
    private StringBuffer	output = new StringBuffer();
    /**
     *	Getter for the names property/attribute
     */
    public ArrayList getNames() {
        return names;
    }
    /**
     *	Setter for the names property/attribute
     */
    public void setNames(ArrayList names) {
        this.names = names;
    }
    public int doStartTag() throws JspTagException {
        //控制循环次数
        if(names.size()>0){
            setLoopVariables();
            return EVAL_BODY_TAG;
        }
        return SKIP_BODY;
    }
    /**
     *	The JSP engine will call this method each time the body
     *	content of this tag has been processed. If it returns
     *	SKIP_BODY, the body content will have been processed for the
     *	last time. If it returns EVAL_BODY_TAG, the body will be processed
     *	and this method called at least once more.
     *	<p/>We store content in a StringBuffer, rather than write output directly.
     */
    public int doAfterBody() throws JspTagException {
        BodyContent bodyContent = getBodyContent();
        if (bodyContent != null) {
            output.append(bodyContent.getString());
            try {
                bodyContent.clear();
            } catch (IOException ex) {
                throw new JspTagException("Fatal IO error");
            }
        }
        // If we still haven't got to the end of the list,
        // continue processing
        if (++index < names.size()) {
            setLoopVariables();
            return EVAL_BODY_TAG;
        }
        // If we get to here, we've finished processing the list
        return SKIP_BODY;
    }
    /**
     *	Called after processing of body content is complete.
     *	We use it to output the content we built up during processing
     *	of the body content.
     */
    public int doEndTag() throws JspTagException {
        BodyContent bodyContent = getBodyContent();
        try {
            bodyContent.getEnclosingWriter().write(output.toString());
        } catch (IOException ex) {
            throw new JspTagException("Fatal IO error");
        }
        // We've finished processing.
        // Set variables for the rest of the page
        // Process the rest of the page
        return EVAL_PAGE;
    }
    /**
     *	Make variable available for each iteration
     */
    private void setLoopVariables() {
        pageContext.setAttribute("name", names.get(index).toString());
        pageContext.setAttribute("position", positions.get(index).toString());
        pageContext.setAttribute("index", new Integer(index));
    }
    /**
     *	Method from NameContext interface required to provide context to
     *	nested tags
     */
    public String getName() {
        return names.get(index).toString();
    }
    /**
     * @return Returns the positions.
     */
    public ArrayList getPositions() {
        return positions;
    }
    /**
     * @param positions The positions to set.
     */
    public void setPositions(ArrayList positions) {
        this.positions = positions;
    }
    
}

⌨️ 快捷键说明

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