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

📄 forlooptag.java

📁 一个使用struts+hibernate制作的Blog站点的源码
💻 JAVA
字号:
/*
 * Created on 2004-2-18 By Liudong
 */
package jdlog.util.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * 用于在页面上执行for循环的标签
 * @author Liudong
 */
public class ForLoopTag extends BodyTagSupport {
    
    String id = "idx";
    int from = 0;
    int to = 0;
    int step = 1;
    int curIdx = -1; 	//用于保存当前索引

    public int doEndTag() throws JspException {
        release();
        return EVAL_PAGE;
    }
    public void release() {
        id = "idx";
        from = 0;
        to = 0;
        step = 1;
        curIdx = -1;
    }
    /* (non-Javadoc)
     * @see javax.servlet.jsp.tagext.BodyTag#doInitBody()
     */
    public void doInitBody() throws JspException {
        if(curIdx==-1)
            curIdx = from;
        pageContext.setAttribute(id, new Integer(curIdx));
    }
    public int doStartTag() throws JspException {
        return (curIdx>=to)?SKIP_BODY:EVAL_BODY_AGAIN;
    }
    /* (non-Javadoc)
     * @see javax.servlet.jsp.tagext.IterationTag#doAfterBody()
     */
    public int doAfterBody() throws JspException {
        try {
            getBodyContent().writeOut(getPreviousOut());
            getBodyContent().clear();
        }catch(IOException e) {
            throw new JspException(e);
        }
        curIdx += step;
        pageContext.setAttribute(id, new Integer(curIdx));
        if(curIdx>to)
            return SKIP_BODY;
        return EVAL_BODY_AGAIN;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getFrom() {
        return String.valueOf(from);
    }
    public void setFrom(int from) {
        this.from = from;
    }
    public void setFrom(Integer from) {
        if(from!=null)
            this.from = from.intValue();
    }
    public void setFrom(String from) {
        try {
            this.from = Integer.parseInt(from);
        }catch(NumberFormatException e) {
            Object obj = pageContext.getAttribute(from);
            if(obj!=null&&obj instanceof Integer)
                this.from = ((Integer)obj).intValue();
        }
    }
    public int getStep() {
        return step;
    }
    public void setStep(int step) {
        this.step = step;
    }
    public void setStep(Integer step) {
        if(step!=null)
            this.step = step.intValue();
    }
    public void setStep(String step) {
        this.step = Integer.parseInt(step);
    }
    public String getTo() {
        return String.valueOf(to);
    }
    public void setTo(int to) {
        this.to = to;
    }
    public void setTo(Integer to) {
        if(to!=null)
            this.to = to.intValue();
    }
    public void setTo(String to) {
        try {
            this.to = Integer.parseInt(to);
        }catch(NumberFormatException e) {
            Object obj = pageContext.getAttribute(to);
            if(obj!=null&&obj instanceof Integer)
                this.to = ((Integer)obj).intValue();
        }
    }
}

⌨️ 快捷键说明

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