1 16 package dlog4j.util.tags; 17 18 import java.io.IOException ; 19 20 import javax.servlet.jsp.JspException ; 21 import javax.servlet.jsp.tagext.BodyTagSupport ; 22 23 27 public class ForLoopTag extends BodyTagSupport { 28 29 String id = "idx"; 30 int from = 0; 31 int to = 0; 32 int step = 1; 33 int curIdx = -1; 35 public int doEndTag() throws JspException { 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 49 public void doInitBody() throws JspException { 50 if(curIdx==-1) 51 curIdx = from; 52 pageContext.setAttribute(id, new Integer (curIdx)); 53 } 54 public int doStartTag() throws JspException { 55 return (curIdx>=to)?SKIP_BODY:EVAL_BODY_AGAIN; 56 } 57 60 public int doAfterBody() throws JspException { 61 try { 62 getBodyContent().writeOut(getPreviousOut()); 63 getBodyContent().clear(); 64 }catch(IOException e) { 65 throw new JspException (e); 66 } 67 curIdx += step; 68 pageContext.setAttribute(id, new Integer (curIdx)); 69 if(curIdx>to) 70 return SKIP_BODY; 71 return EVAL_BODY_AGAIN; 72 } 73 public String getId() { 74 return id; 75 } 76 public void setId(String id) { 77 this.id = id; 78 } 79 public String getFrom() { 80 return String.valueOf(from); 81 } 82 public void setFrom(String from) { 83 try { 84 this.from = Integer.parseInt(from); 85 }catch(NumberFormatException e) { 86 Object obj = pageContext.getAttribute(from); 87 if(obj!=null&&obj instanceof Integer ) 88 this.from = ((Integer )obj).intValue(); 89 } 90 } 91 public int getStep() { 92 return step; 93 } 94 public void setStep(String step) { 95 this.step = Integer.parseInt(step); 96 } 97 public String getTo() { 98 return String.valueOf(to); 99 } 100 public void setTo(String to) { 101 try { 102 this.to = Integer.parseInt(to); 103 }catch(NumberFormatException e) { 104 Object obj = pageContext.getAttribute(to); 105 if(obj!=null&&obj instanceof Integer ) 106 this.to = ((Integer )obj).intValue(); 107 } 108 } 109 } 110 | Popular Tags |