KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > util > tags > ForLoopTag


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.util.tags;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
22
23 /**
24  * 用于在页面上执行for循环的标签
25  * @author Liudong
26  */

27 public class ForLoopTag extends BodyTagSupport JavaDoc {
28     
29     String JavaDoc id = "idx";
30     int from = 0;
31     int to = 0;
32     int step = 1;
33     int curIdx = -1; //用于保存当前索引
34

35     public int doEndTag() throws JspException JavaDoc {
36         release();
37         return EVAL_PAGE;
38     }
39     public void release() {
40         id = "idx";
41         from = 0;
42         to = 0;
43         step = 1;
44         curIdx = -1;
45     }
46     /* (non-Javadoc)
47      * @see javax.servlet.jsp.tagext.BodyTag#doInitBody()
48      */

49     public void doInitBody() throws JspException JavaDoc {
50         if(curIdx==-1)
51             curIdx = from;
52         pageContext.setAttribute(id, new Integer JavaDoc(curIdx));
53     }
54     public int doStartTag() throws JspException JavaDoc {
55         return (curIdx>=to)?SKIP_BODY:EVAL_BODY_AGAIN;
56     }
57     /* (non-Javadoc)
58      * @see javax.servlet.jsp.tagext.IterationTag#doAfterBody()
59      */

60     public int doAfterBody() throws JspException JavaDoc {
61         try {
62             getBodyContent().writeOut(getPreviousOut());
63             getBodyContent().clear();
64         }catch(IOException JavaDoc e) {
65             throw new JspException JavaDoc(e);
66         }
67         curIdx += step;
68         pageContext.setAttribute(id, new Integer JavaDoc(curIdx));
69         if(curIdx>to)
70             return SKIP_BODY;
71         return EVAL_BODY_AGAIN;
72     }
73     public String JavaDoc getId() {
74         return id;
75     }
76     public void setId(String JavaDoc id) {
77         this.id = id;
78     }
79     public String JavaDoc getFrom() {
80         return String.valueOf(from);
81     }
82     public void setFrom(String JavaDoc from) {
83         try {
84             this.from = Integer.parseInt(from);
85         }catch(NumberFormatException JavaDoc e) {
86             Object JavaDoc obj = pageContext.getAttribute(from);
87             if(obj!=null&&obj instanceof Integer JavaDoc)
88                 this.from = ((Integer JavaDoc)obj).intValue();
89         }
90     }
91     public int getStep() {
92         return step;
93     }
94     public void setStep(String JavaDoc step) {
95         this.step = Integer.parseInt(step);
96     }
97     public String JavaDoc getTo() {
98         return String.valueOf(to);
99     }
100     public void setTo(String JavaDoc to) {
101         try {
102             this.to = Integer.parseInt(to);
103         }catch(NumberFormatException JavaDoc e) {
104             Object JavaDoc obj = pageContext.getAttribute(to);
105             if(obj!=null&&obj instanceof Integer JavaDoc)
106                 this.to = ((Integer JavaDoc)obj).intValue();
107         }
108     }
109 }
110
Popular Tags