1 18 19 package org.apache.struts.taglib.html; 20 21 import java.lang.reflect.InvocationTargetException ; 22 23 import javax.servlet.jsp.JspException ; 24 25 import org.apache.commons.beanutils.BeanUtils; 26 import org.apache.struts.taglib.TagUtils; 27 import org.apache.struts.util.MessageResources; 28 29 36 public class SelectTag extends BaseHandlerTag { 37 38 39 41 42 45 protected String match[] = null; 46 47 48 51 protected static MessageResources messages = 52 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 53 54 55 59 protected String multiple = null; 60 61 public String getMultiple() { 62 return (this.multiple); 63 } 64 65 public void setMultiple(String multiple) { 66 this.multiple = multiple; 67 } 68 69 70 73 protected String name = Constants.BEAN_KEY; 74 75 public String getName() { 76 return (this.name); 77 } 78 79 public void setName(String name) { 80 this.name = name; 81 } 82 83 84 87 protected String property = null; 88 89 90 93 protected String saveBody = null; 94 95 96 100 protected String size = null; 101 102 public String getSize() { 103 return (this.size); 104 } 105 106 public void setSize(String size) { 107 this.size = size; 108 } 109 110 111 114 protected String value = null; 115 116 117 119 120 125 public boolean isMatched(String value) { 126 if ((this.match == null) || (value == null)) { 127 return false; 128 } 129 130 for (int i = 0; i < this.match.length; i++) { 131 if (value.equals(this.match[i])) 132 return true; 133 } 134 135 return false; 136 } 137 138 139 142 public String getProperty() { 143 144 return (this.property); 145 146 } 147 148 149 154 public void setProperty(String property) { 155 156 this.property = property; 157 158 } 159 160 161 164 public String getValue() { 165 166 return (this.value); 167 168 } 169 170 171 176 public void setValue(String value) { 177 178 this.value = value; 179 180 } 181 182 183 185 186 193 public int doStartTag() throws JspException { 194 195 TagUtils.getInstance().write(pageContext, renderSelectStartElement()); 196 197 pageContext.setAttribute(Constants.SELECT_KEY, this); 199 200 this.calculateMatchValues(); 201 202 return (EVAL_BODY_TAG); 203 } 204 205 210 protected String renderSelectStartElement() throws JspException { 211 StringBuffer results = new StringBuffer ("<select"); 212 213 prepareAttribute(results, "name", prepareName()); 214 prepareAttribute(results, "accesskey", getAccesskey()); 215 if (multiple != null) { 216 results.append(" multiple=\"multiple\""); 217 } 218 prepareAttribute(results, "size", getSize()); 219 prepareAttribute(results, "tabindex", getTabindex()); 220 results.append(prepareEventHandlers()); 221 results.append(prepareStyles()); 222 prepareOtherAttributes(results); 223 results.append(">"); 224 225 return results.toString(); 226 } 227 228 232 private void calculateMatchValues() throws JspException { 233 if (this.value != null) { 234 this.match = new String [1]; 235 this.match[0] = this.value; 236 237 } else { 238 Object bean = TagUtils.getInstance().lookup(pageContext, name, null); 239 if (bean == null) { 240 JspException e = 241 new JspException (messages.getMessage("getter.bean", name)); 242 243 TagUtils.getInstance().saveException(pageContext, e); 244 throw e; 245 } 246 247 try { 248 this.match = BeanUtils.getArrayProperty(bean, property); 249 if (this.match == null) { 250 this.match = new String [0]; 251 } 252 253 } catch (IllegalAccessException e) { 254 TagUtils.getInstance().saveException(pageContext, e); 255 throw new JspException ( 256 messages.getMessage("getter.access", property, name)); 257 258 } catch (InvocationTargetException e) { 259 Throwable t = e.getTargetException(); 260 TagUtils.getInstance().saveException(pageContext, t); 261 throw new JspException ( 262 messages.getMessage("getter.result", property, t.toString())); 263 264 } catch (NoSuchMethodException e) { 265 TagUtils.getInstance().saveException(pageContext, e); 266 throw new JspException ( 267 messages.getMessage("getter.method", property, name)); 268 } 269 } 270 } 271 272 273 279 public int doAfterBody() throws JspException { 280 281 if (bodyContent != null) { 282 String value = bodyContent.getString(); 283 if (value == null) { 284 value = ""; 285 } 286 287 this.saveBody = value.trim(); 288 } 289 290 return (SKIP_BODY); 291 } 292 293 294 299 public int doEndTag() throws JspException { 300 301 pageContext.removeAttribute(Constants.SELECT_KEY); 303 304 StringBuffer results = new StringBuffer (); 306 if (saveBody != null) { 307 results.append(saveBody); 308 saveBody = null; 309 } 310 results.append("</select>"); 311 312 TagUtils.getInstance().write(pageContext, results.toString()); 313 314 return (EVAL_PAGE); 315 } 316 317 321 protected String prepareName() throws JspException { 322 323 if (property == null) { 324 return null; 325 } 326 327 if(indexed) { 329 StringBuffer results = new StringBuffer (); 330 prepareIndex(results, name); 331 results.append(property); 332 return results.toString(); 333 } 334 335 return property; 336 337 } 338 339 342 public void release() { 343 344 super.release(); 345 match = null; 346 multiple = null; 347 name = Constants.BEAN_KEY; 348 property = null; 349 saveBody = null; 350 size = null; 351 value = null; 352 353 } 354 355 } 356 | Popular Tags |