1 16 17 package org.springframework.web.servlet.tags.form; 18 19 import java.util.Collection ; 20 import java.util.Map ; 21 22 import javax.servlet.jsp.JspException ; 23 24 import org.springframework.util.Assert; 25 import org.springframework.util.ObjectUtils; 26 import org.springframework.web.servlet.support.BindStatus; 27 28 42 public class SelectTag extends AbstractHtmlInputElementTag { 43 44 48 public static final String LIST_VALUE_PAGE_ATTRIBUTE = 49 "org.springframework.web.servlet.tags.form.SelectTag.listValue"; 50 51 52 56 private Object items; 57 58 62 private String itemValue; 63 64 68 private String itemLabel; 69 70 74 private String size; 75 76 80 private Object multiple = Boolean.FALSE; 81 82 86 private TagWriter tagWriter; 87 88 89 97 public void setItems(Object items) { 98 Assert.notNull(items, "'items' must not be null"); 99 this.items = items; 100 } 101 102 106 protected Object getItems() { 107 return this.items; 108 } 109 110 117 public void setItemValue(String itemValue) { 118 Assert.hasText(itemValue, "'itemValue' must not be empty"); 119 this.itemValue = itemValue; 120 } 121 122 126 protected String getItemValue() { 127 return this.itemValue; 128 } 129 130 135 public void setItemLabel(String itemLabel) { 136 Assert.hasText(itemLabel, "'itemLabel' must not be empty"); 137 this.itemLabel = itemLabel; 138 } 139 140 144 protected String getItemLabel() { 145 return this.itemLabel; 146 } 147 148 154 public void setSize(String size) { 155 Assert.hasText(size, "'size' must not be empty"); 156 this.size = size; 157 } 158 159 163 protected String getSize() { 164 return this.size; 165 } 166 167 172 public void setMultiple(Object multiple) { 173 this.multiple = multiple; 174 } 175 176 181 protected Object getMultiple() { 182 return this.multiple; 183 } 184 185 186 193 protected int writeTagContent(TagWriter tagWriter) throws JspException { 194 tagWriter.startTag("select"); 195 writeDefaultAttributes(tagWriter); 196 197 if (isMultiple()) { 198 tagWriter.writeAttribute("multiple", "multiple"); 199 } 200 201 tagWriter.writeOptionalAttributeValue("size", getDisplayString(evaluate("size", getSize()))); 202 203 Object items = getItems(); 204 if (items != null) { 205 Object itemsObject = (items instanceof String ? evaluate("items", (String ) items) : items); 206 207 String valueProperty = (getItemValue() == null ? null : 208 ObjectUtils.getDisplayString(evaluate("itemValue", getItemValue()))); 209 String labelProperty = (getItemLabel() == null ? null : 210 ObjectUtils.getDisplayString(evaluate("itemLabel", getItemLabel()))); 211 212 OptionWriter optionWriter = new OptionWriter(itemsObject, getBindStatus(), valueProperty, labelProperty, isHtmlEscape()); 213 optionWriter.writeOptions(tagWriter); 214 215 tagWriter.endTag(); 216 writeHiddenTagIfNecessary(tagWriter); 217 return EVAL_PAGE; 218 } 219 else { 220 tagWriter.forceBlock(); 222 this.tagWriter = tagWriter; 223 this.pageContext.setAttribute(LIST_VALUE_PAGE_ATTRIBUTE, getBindStatus()); 224 return EVAL_BODY_INCLUDE; 225 } 226 } 227 228 233 private void writeHiddenTagIfNecessary(TagWriter tagWriter) throws JspException { 234 if (isMultiple()) { 235 tagWriter.startTag("input"); 236 tagWriter.writeAttribute("type", "hidden"); 237 tagWriter.writeAttribute("name", "_" + getName()); 238 tagWriter.writeAttribute("value", "1"); 239 tagWriter.endTag(); 240 } 241 } 242 243 private boolean isMultiple() throws JspException { 244 Object multiple = getMultiple(); 245 if (Boolean.TRUE.equals(multiple) 246 || "true".equals(multiple) 247 || "multiple".equals(multiple)) { 248 return true; 249 } 250 else if (this.multiple instanceof String ) { 251 Object evaluatedValue = evaluate("multiple", multiple); 252 return Boolean.TRUE.equals(evaluatedValue); 253 } 254 return forceMultiple(); 255 } 256 257 261 private boolean forceMultiple() throws JspException { 262 BindStatus bindStatus = getBindStatus(); 263 Class valueType = bindStatus.getValueType(); 264 if (valueType != null && typeRequiresMultiple(valueType)) { 265 return true; 266 } 267 else if (bindStatus.getEditor() != null) { 268 Object editorValue = bindStatus.getEditor().getValue(); 269 if (editorValue != null && typeRequiresMultiple(editorValue.getClass())) { 270 return true; 271 } 272 } 273 return false; 274 } 275 276 280 private static boolean typeRequiresMultiple(Class type) { 281 return (type.isArray() || Collection .class.isAssignableFrom(type) || Map .class.isAssignableFrom(type)); 282 } 283 284 288 public int doEndTag() throws JspException { 289 if (this.tagWriter != null) { 290 this.tagWriter.endTag(); 291 writeHiddenTagIfNecessary(tagWriter); 292 } 293 return EVAL_PAGE; 294 } 295 296 300 public void doFinally() { 301 super.doFinally(); 302 this.tagWriter = null; 303 this.pageContext.removeAttribute(LIST_VALUE_PAGE_ATTRIBUTE); 304 } 305 306 } 307 | Popular Tags |