1 16 package org.apache.taglibs.input; 17 18 import java.util.Map ; 19 import java.util.Vector ; 20 21 import javax.servlet.ServletRequest ; 22 import javax.servlet.jsp.JspException ; 23 import javax.servlet.jsp.JspTagException ; 24 import javax.servlet.jsp.JspWriter ; 25 import javax.servlet.jsp.tagext.TagSupport ; 26 27 37 38 public class Checkbox extends TagSupport { 39 40 private String name; 42 private String value; 44 private String dVal; 46 private String [] dValArray; 48 private Map attributes; 50 private String attributesText; 52 private String beanId; 54 public void release() { 55 super.release(); 56 name = null; 57 dVal = null; 58 dValArray = null; 59 attributes = null; 60 attributesText = null; 61 beanId = null; 62 } 63 64 public int doStartTag() throws JspException { 65 try { 66 if (name == null || name.equals("")) 68 throw new JspTagException ("invalid null or empty 'name'"); 69 70 String beanId = this.beanId; 72 73 if (beanId == null) { 75 beanId = Util.defaultFormBeanId(this); 76 } else if (beanId.length() == 0) { 77 beanId = null; 79 } 80 81 if (value == null) 83 value = "on"; 84 85 ServletRequest req = pageContext.getRequest(); 87 JspWriter out = pageContext.getOut(); 88 89 Vector dVals = new Vector (); 91 if (dVal != null) 92 dVals.add(dVal); 93 if (dValArray != null) 94 for (int i = 0; i < dValArray.length; i++) 95 if (dValArray[i] != null) 96 dVals.add(dValArray[i]); 97 98 out.print("<input type=\"checkbox\" "); 100 out.print("name=\"" + Util.quote(name) + "\" "); 101 out.print("value=\"" + Util.quote(value) + "\" "); 102 103 Util.printAttributes(out, attributes); 105 if (attributesText != null) { 106 out.print(attributesText + " "); 107 } 108 109 125 126 String [] beanValues = (beanId != null ? Util.beanPropertyValues( 129 pageContext.findAttribute(beanId), name) : null); 130 if (beanValues != null) { 131 for (int i = 0; i < beanValues.length; i++) { 132 if (beanValues[i] != null && beanValues[i].equals(value)) { 133 out.print("checked=\"checked\" "); 134 break; 135 } 136 } 137 } 138 else if (!req.getParameterNames().hasMoreElements()) { 141 if (dVals != null) { 143 for (int i = 0; i < dVals.size(); i++) { 144 if (dVals.get(i) == null 145 || !(dVals.get(i) instanceof String )) 146 throw new JspTagException ( 147 "'default' array must only contain non-null " 148 + "Strings"); 149 if ((dVals.get(i)).equals(value)) { 150 out.print("checked=\"checked\" "); 151 break; } 153 } 154 } 155 } else { 156 String [] checked = req.getParameterValues(name); 158 if (checked != null) { 159 for (int i = 0; i < checked.length; i++) { 161 if (checked[i].equals(value)) { 162 out.print("checked=\"checked\" "); 163 break; } 165 } 166 } 167 } 168 169 out.print("/>"); 171 172 } catch (Exception ex) { 173 throw new JspTagException (ex.getMessage()); 174 } 175 return SKIP_BODY; 176 } 177 178 183 public String getName() { 184 return name; 185 } 186 187 public void setName(String x) { 188 name = x; 189 } 190 191 196 public String getValue() { 197 return value; 198 } 199 200 public void setValue(String x) { 201 value = x; 202 } 203 204 209 public String [] getDefaults() { 210 return dValArray; 211 } 212 213 public void setDefaults(String [] x) { 214 dValArray = x; 215 } 216 217 222 public String getDefault() { 223 return dVal; 224 } 225 226 public void setDefault(String x) { 227 dVal = x; 228 } 229 230 235 public String getBean() { 236 return beanId; 237 } 238 239 public void setBean(String x) { 240 beanId = x; 241 } 242 243 248 public String getAttributesText() { 249 return attributesText; 250 } 251 252 public void setAttributesText(String x) { 253 attributesText = x; 254 } 255 256 261 public Map getAttributes() { 262 return attributes; 263 } 264 265 public void setAttributes(Map x) { 266 attributes = x; 267 } 268 269 } | Popular Tags |