1 18 19 package org.apache.struts.taglib.html; 20 21 import javax.servlet.jsp.JspException ; 22 23 import org.apache.struts.taglib.TagUtils; 24 import org.apache.struts.util.MessageResources; 25 26 31 public class CheckboxTag extends BaseHandlerTag { 32 33 35 38 protected static MessageResources messages = 39 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 40 41 44 protected String name = Constants.BEAN_KEY; 45 46 public String getName() { 47 return (this.name); 48 } 49 50 public void setName(String name) { 51 this.name = name; 52 } 53 54 57 protected String property = null; 58 59 62 protected String text = null; 63 64 67 protected String value = null; 68 69 71 74 public String getProperty() { 75 76 return (this.property); 77 78 } 79 80 85 public void setProperty(String property) { 86 87 this.property = property; 88 89 } 90 91 94 public String getValue() { 95 96 return value == null ? "on" : value; 97 98 } 99 100 105 public void setValue(String value) { 106 107 this.value = value; 108 109 } 110 111 113 120 public int doStartTag() throws JspException { 121 122 StringBuffer results = new StringBuffer ("<input type=\"checkbox\""); 124 prepareAttribute(results, "name", prepareName()); 125 prepareAttribute(results, "accesskey", getAccesskey()); 126 prepareAttribute(results, "tabindex", getTabindex()); 127 128 prepareAttribute(results, "value", getValue()); 129 if (isChecked()) { 130 results.append(" checked=\"checked\""); 131 } 132 133 results.append(prepareEventHandlers()); 134 results.append(prepareStyles()); 135 prepareOtherAttributes(results); 136 results.append(getElementClose()); 137 138 TagUtils.getInstance().write(pageContext, results.toString()); 140 141 this.text = null; 143 return (EVAL_BODY_TAG); 144 145 } 146 147 153 protected boolean isChecked() throws JspException { 154 Object result = 155 TagUtils.getInstance().lookup(pageContext, name, property, null); 156 157 if (result == null) { 158 result = ""; 159 } 160 161 result = result.toString(); 162 163 String checked = (String ) result; 164 return ( 165 checked.equalsIgnoreCase(this.value) 166 || checked.equalsIgnoreCase("true") 167 || checked.equalsIgnoreCase("yes") 168 || checked.equalsIgnoreCase("on")); 169 170 } 171 172 177 public int doAfterBody() throws JspException { 178 179 if (bodyContent != null) { 180 String value = bodyContent.getString().trim(); 181 if (value.length() > 0) { 182 text = value; 183 } 184 } 185 return (SKIP_BODY); 186 187 } 188 189 194 public int doEndTag() throws JspException { 195 196 if (text != null) { 198 TagUtils.getInstance().write(pageContext, text); 199 } 200 201 return (EVAL_PAGE); 203 204 } 205 206 207 211 protected String prepareName() throws JspException { 212 213 if (property == null) { 214 return null; 215 } 216 217 if(indexed) { 219 StringBuffer results = new StringBuffer (); 220 prepareIndex(results, name); 221 results.append(property); 222 return results.toString(); 223 } 224 225 return property; 226 227 } 228 229 232 public void release() { 233 234 super.release(); 235 name = Constants.BEAN_KEY; 236 property = null; 237 text = null; 238 value = null; 239 240 } 241 242 } 243 | Popular Tags |