1 18 19 package org.apache.struts.taglib.html; 20 21 import java.lang.reflect.InvocationTargetException ; 22 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.PageContext ; 25 26 import org.apache.commons.beanutils.BeanUtils; 27 import org.apache.struts.Globals; 28 import org.apache.struts.taglib.TagUtils; 29 import org.apache.struts.util.MessageResources; 30 31 40 41 public class MultiboxTag extends BaseHandlerTag { 42 43 45 49 protected String constant = null; 50 51 54 protected static MessageResources messages = 55 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 56 57 60 protected String name = Constants.BEAN_KEY; 61 62 public String getName() { 63 return (this.name); 64 } 65 66 public void setName(String name) { 67 this.name = name; 68 } 69 70 73 protected String property = null; 74 75 79 protected String value = null; 80 81 83 86 public String getProperty() { 87 88 return (this.property); 89 90 } 91 92 97 public void setProperty(String property) { 98 99 this.property = property; 100 101 } 102 103 106 public String getValue() { 107 108 return (this.value); 109 110 } 111 112 117 public void setValue(String value) { 118 119 this.value = value; 120 121 } 122 123 125 130 public int doStartTag() throws JspException { 131 132 this.constant = null; 134 return (EVAL_BODY_TAG); 135 136 } 137 138 144 public int doAfterBody() throws JspException { 145 146 if (bodyContent != null) { 147 this.constant = bodyContent.getString().trim(); 148 } 149 150 if ("".equals(this.constant)) { 151 this.constant = null; 152 } 153 154 return SKIP_BODY; 155 } 156 157 162 public int doEndTag() throws JspException { 163 164 StringBuffer results = new StringBuffer ("<input type=\"checkbox\""); 166 167 prepareAttribute(results, "name", prepareName()); 168 prepareAttribute(results, "accesskey", getAccesskey()); 169 prepareAttribute(results, "tabindex", getTabindex()); 170 String value = prepareValue(results); 171 prepareChecked(results, value); 172 results.append(prepareEventHandlers()); 173 results.append(prepareStyles()); 174 prepareOtherAttributes(results); 175 results.append(getElementClose()); 176 177 TagUtils.getInstance().write(pageContext, results.toString()); 178 179 return EVAL_PAGE; 180 } 181 182 183 187 protected String prepareName() throws JspException { 188 189 return property; 190 191 } 192 193 197 protected String prepareValue(StringBuffer results) throws JspException { 198 199 String value = (this.value == null) ? this.constant : this.value; 200 if (value == null) { 201 JspException e = new JspException (messages.getMessage("multiboxTag.value")); 202 pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE); 203 throw e; 204 } 205 206 prepareAttribute(results, "value", TagUtils.getInstance().filter(value)); 207 208 return value; 209 210 } 211 212 216 protected void prepareChecked(StringBuffer results, String value) throws JspException { 217 218 Object bean = TagUtils.getInstance().lookup(pageContext, name, null); 219 String values[] = null; 220 221 if (bean == null) { 222 throw new JspException (messages.getMessage("getter.bean", name)); 223 } 224 225 try { 226 values = BeanUtils.getArrayProperty(bean, property); 227 if (values == null) { 228 values = new String [0]; 229 } 230 231 } catch (IllegalAccessException e) { 232 throw new JspException (messages.getMessage("getter.access", property, name)); 233 } catch (InvocationTargetException e) { 234 Throwable t = e.getTargetException(); 235 throw new JspException (messages.getMessage("getter.result", property, t.toString())); 236 } catch (NoSuchMethodException e) { 237 throw new JspException (messages.getMessage("getter.method", property, name)); 238 } 239 240 for (int i = 0; i < values.length; i++) { 241 if (value.equals(values[i])) { 242 results.append(" checked=\"checked\""); 243 break; 244 } 245 } 246 247 } 248 249 250 253 public void release() { 254 255 super.release(); 256 constant = null; 257 name = Constants.BEAN_KEY; 258 property = null; 259 value = null; 260 261 } 262 263 } 264 | Popular Tags |