1 16 17 package org.springframework.web.servlet.tags.form; 18 19 import javax.servlet.jsp.JspException ; 20 import javax.servlet.jsp.tagext.BodyContent ; 21 import javax.servlet.jsp.tagext.BodyTag ; 22 23 import org.springframework.util.Assert; 24 import org.springframework.web.servlet.support.BindStatus; 25 import org.springframework.web.util.TagUtils; 26 27 48 public class OptionTag extends AbstractHtmlElementBodyTag implements BodyTag { 49 50 53 public static final String VALUE_VARIABLE_NAME = "value"; 54 55 58 public static final String DISPLAY_VALUE_VARIABLE_NAME = "displayValue"; 59 60 61 64 private static final String SELECTED_ATTRIBUTE = "selected"; 65 66 69 private static final String VALUE_ATTRIBUTE = VALUE_VARIABLE_NAME; 70 71 74 private static final String DISABLED_ATTRIBUTE = "disabled"; 75 76 77 80 private Object value; 81 82 85 private String label; 86 87 private Object oldValue; 88 89 private Object oldDisplayValue; 90 91 private String disabled; 92 93 94 98 public void setValue(Object value) { 99 this.value = value; 100 } 101 102 106 protected Object getValue() { 107 return this.value; 108 } 109 110 115 public void setDisabled(String disabled) { 116 this.disabled = disabled; 117 } 118 119 124 protected String getDisabled() { 125 return disabled; 126 } 127 128 132 protected boolean isDisabled() { 133 return "true".equals(getDisabled()); 134 } 135 136 141 public void setLabel(String label) { 142 Assert.notNull(label, "'label' must not be null"); 143 this.label = label; 144 } 145 146 150 protected String getLabel() { 151 return this.label; 152 } 153 154 155 protected void renderDefaultContent(TagWriter tagWriter) throws JspException { 156 Object value = this.pageContext.getAttribute(VALUE_VARIABLE_NAME); 157 String label = getLabelValue(value); 158 renderOption(value, label, tagWriter); 159 } 160 161 protected void renderFromBodyContent(BodyContent bodyContent, TagWriter tagWriter) throws JspException { 162 Object value = this.pageContext.getAttribute(VALUE_VARIABLE_NAME); 163 String label = bodyContent.getString(); 164 renderOption(value, label, tagWriter); 165 } 166 167 170 protected void onWriteTagContent() { 171 assertUnderSelectTag(); 172 } 173 174 protected void exposeAttributes() throws JspException { 175 Object value = resolveValue(); 176 this.oldValue = this.pageContext.getAttribute(VALUE_VARIABLE_NAME); 177 this.pageContext.setAttribute(VALUE_VARIABLE_NAME, value); 178 this.oldDisplayValue = this.pageContext.getAttribute(DISPLAY_VALUE_VARIABLE_NAME); 179 this.pageContext.setAttribute(DISPLAY_VALUE_VARIABLE_NAME, getDisplayString(value, getBindStatus().getEditor())); 180 } 181 182 protected BindStatus getBindStatus() { 183 return (BindStatus) this.pageContext.getAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE); 184 } 185 186 protected void removeAttributes() { 187 if (this.oldValue != null) { 188 this.pageContext.setAttribute(VALUE_ATTRIBUTE, this.oldValue); 189 this.oldValue = null; 190 } 191 else { 192 this.pageContext.removeAttribute(VALUE_VARIABLE_NAME); 193 } 194 195 if (this.oldDisplayValue != null) { 196 this.pageContext.setAttribute(DISPLAY_VALUE_VARIABLE_NAME, this.oldDisplayValue); 197 this.oldDisplayValue = null; 198 } 199 else { 200 this.pageContext.removeAttribute(DISPLAY_VALUE_VARIABLE_NAME); 201 } 202 } 203 204 205 private void renderOption(Object value, String label, TagWriter tagWriter) throws JspException { 206 tagWriter.startTag("option"); 207 208 String renderedValue = getDisplayString(value, getBindStatus().getEditor()); 209 210 211 tagWriter.writeAttribute(VALUE_ATTRIBUTE, renderedValue); 212 213 if (isSelected(value)) { 214 tagWriter.writeAttribute(SELECTED_ATTRIBUTE, SELECTED_ATTRIBUTE); 215 } 216 if(isDisabled()) { 217 tagWriter.writeAttribute(DISABLED_ATTRIBUTE, "disabled"); 218 } 219 tagWriter.appendValue(label); 220 221 tagWriter.endTag(); 222 } 223 224 230 private String getLabelValue(Object resolvedValue) throws JspException { 231 String label = getLabel(); 232 Object labelObj = (label == null ? resolvedValue : evaluate("label", label)); 233 return getDisplayString(labelObj, getBindStatus().getEditor()); 234 } 235 236 private void assertUnderSelectTag() { 237 TagUtils.assertHasAncestorOfType(this, SelectTag.class, "option", "select"); 238 } 239 240 private boolean isSelected(Object resolvedValue) { 241 return SelectedValueComparator.isSelected(getBindStatus(), resolvedValue); 242 } 243 244 private Object resolveValue() throws JspException { 245 return evaluate(VALUE_VARIABLE_NAME, getValue()); 246 } 247 248 } 249 | Popular Tags |