1 5 package com.opensymphony.webwork.views.jsp; 6 7 import com.opensymphony.webwork.util.MakeIterator; 8 import com.opensymphony.xwork.util.OgnlValueStack; 9 10 import javax.servlet.jsp.JspException ; 11 import javax.servlet.jsp.PageContext ; 12 import java.util.Iterator ; 13 14 15 59 public class IteratorTag extends WebWorkBodyTagSupport { 60 62 protected Iterator iterator; 63 protected IteratorStatus status; 64 protected Object oldStatus; 65 protected IteratorStatus.StatusState statusState; 66 protected String statusAttr; 67 protected String value; 68 69 71 public void setStatus(String name) { 72 this.statusAttr = name; 73 } 74 75 public void setValue(String value) { 76 this.value = value; 77 } 78 79 public int doAfterBody() throws JspException { 80 OgnlValueStack stack = getStack(); 81 stack.pop(); 82 83 if (iterator.hasNext()) { 84 Object currentValue = iterator.next(); 85 stack.push(currentValue); 86 87 String id = getId(); 88 89 if ((id != null) && (currentValue != null)) { 90 pageContext.setAttribute(id, currentValue); 91 pageContext.setAttribute(id, currentValue, PageContext.REQUEST_SCOPE); 92 } 93 94 if (status != null) { 96 statusState.next(); statusState.setLast(!iterator.hasNext()); 98 } 99 100 return EVAL_BODY_AGAIN; 101 } else { 102 if (status != null) { 104 if (oldStatus == null) { 105 stack.getContext().put(statusAttr, null); 106 } else { 107 stack.getContext().put(statusAttr, oldStatus); 108 } 109 } 110 111 iterator = null; 113 status = null; 114 115 try { 116 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 117 } catch (Exception e) { 118 throw new JspException (e.getMessage()); 119 } 120 121 return SKIP_BODY; 122 } 123 } 124 125 public int doStartTag() throws JspException { 126 if (statusAttr != null) { 128 statusState = new IteratorStatus.StatusState(); 129 status = new IteratorStatus(statusState); 130 } 131 132 OgnlValueStack stack = getStack(); 133 134 if (value == null) { 135 value = "top"; 136 } 137 138 iterator = MakeIterator.convert(findValue(value)); 139 140 if ((iterator != null) && iterator.hasNext()) { 142 Object currentValue = iterator.next(); 143 stack.push(currentValue); 144 145 String id = getId(); 146 147 if ((id != null) && (currentValue != null)) { 148 pageContext.setAttribute(id, currentValue); 149 pageContext.setAttribute(id, currentValue, PageContext.REQUEST_SCOPE); 150 } 151 152 if (statusAttr != null) { 154 statusState.setLast(!iterator.hasNext()); 155 oldStatus = stack.getContext().get(statusAttr); 156 stack.getContext().put(statusAttr, status); 157 } 158 159 return EVAL_BODY_AGAIN; 160 } else { 161 return SKIP_BODY; 162 } 163 } 164 } 165 | Popular Tags |