001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.util.tags;
017:
018: import java.io.IOException;
019:
020: import javax.servlet.jsp.JspException;
021: import javax.servlet.jsp.tagext.BodyTagSupport;
022:
023: /**
024: * 用于在页面上执行for循环的标签
025: * @author Liudong
026: */
027: public class ForLoopTag extends BodyTagSupport {
028:
029: String id = "idx";
030: int from = 0;
031: int to = 0;
032: int step = 1;
033: int curIdx = -1; //用于保存当前索引
034:
035: public int doEndTag() throws JspException {
036: release();
037: return EVAL_PAGE;
038: }
039:
040: public void release() {
041: id = "idx";
042: from = 0;
043: to = 0;
044: step = 1;
045: curIdx = -1;
046: }
047:
048: /* (non-Javadoc)
049: * @see javax.servlet.jsp.tagext.BodyTag#doInitBody()
050: */
051: public void doInitBody() throws JspException {
052: if (curIdx == -1)
053: curIdx = from;
054: pageContext.setAttribute(id, new Integer(curIdx));
055: }
056:
057: public int doStartTag() throws JspException {
058: return (curIdx >= to) ? SKIP_BODY : EVAL_BODY_AGAIN;
059: }
060:
061: /* (non-Javadoc)
062: * @see javax.servlet.jsp.tagext.IterationTag#doAfterBody()
063: */
064: public int doAfterBody() throws JspException {
065: try {
066: getBodyContent().writeOut(getPreviousOut());
067: getBodyContent().clear();
068: } catch (IOException e) {
069: throw new JspException(e);
070: }
071: curIdx += step;
072: pageContext.setAttribute(id, new Integer(curIdx));
073: if (curIdx > to)
074: return SKIP_BODY;
075: return EVAL_BODY_AGAIN;
076: }
077:
078: public String getId() {
079: return id;
080: }
081:
082: public void setId(String id) {
083: this .id = id;
084: }
085:
086: public String getFrom() {
087: return String.valueOf(from);
088: }
089:
090: public void setFrom(String from) {
091: try {
092: this .from = Integer.parseInt(from);
093: } catch (NumberFormatException e) {
094: Object obj = pageContext.getAttribute(from);
095: if (obj != null && obj instanceof Integer)
096: this .from = ((Integer) obj).intValue();
097: }
098: }
099:
100: public int getStep() {
101: return step;
102: }
103:
104: public void setStep(String step) {
105: this .step = Integer.parseInt(step);
106: }
107:
108: public String getTo() {
109: return String.valueOf(to);
110: }
111:
112: public void setTo(String to) {
113: try {
114: this .to = Integer.parseInt(to);
115: } catch (NumberFormatException e) {
116: Object obj = pageContext.getAttribute(to);
117: if (obj != null && obj instanceof Integer)
118: this .to = ((Integer) obj).intValue();
119: }
120: }
121: }
|