1 16 17 package org.springframework.web.servlet.tags.form; 18 19 import java.beans.PropertyEditor ; 20 21 import javax.servlet.jsp.JspException ; 22 import javax.servlet.jsp.PageContext ; 23 24 import org.springframework.beans.PropertyAccessor; 25 import org.springframework.core.Conventions; 26 import org.springframework.util.Assert; 27 import org.springframework.util.ObjectUtils; 28 import org.springframework.util.StringUtils; 29 import org.springframework.web.servlet.support.BindStatus; 30 import org.springframework.web.servlet.tags.NestedPathTag; 31 32 44 public abstract class AbstractDataBoundFormElementTag extends AbstractFormTag { 45 46 49 public static final String ID_ATTRIBUTE = "id"; 50 51 54 public static final String COMMAND_NAME_ATTRIBUTE = "commandName"; 55 56 60 public static final String COMMAND_NAME_VARIABLE_NAME = 61 Conventions.getQualifiedAttributeName(AbstractFormTag.class, COMMAND_NAME_ATTRIBUTE); 62 63 64 67 private String path; 68 69 72 private String id; 73 74 77 private BindStatus bindStatus; 78 79 80 84 public void setPath(String path) { 85 Assert.hasText(path, "'path' must not be empty"); 86 this.path = path; 87 } 88 89 93 protected final String getPath() throws JspException { 94 return (String ) evaluate("path", this.path); 95 } 96 97 102 public void setId(String id) { 103 this.id = id; 104 } 105 106 110 public String getId() { 111 return this.id; 112 } 113 114 115 124 protected void writeDefaultAttributes(TagWriter tagWriter) throws JspException { 125 String id = getId(); 126 if (StringUtils.hasText(id)) { 127 tagWriter.writeAttribute(ID_ATTRIBUTE, ObjectUtils.getDisplayString(evaluate(ID_ATTRIBUTE, id))); 128 } 129 else { 130 writeOptionalAttribute(tagWriter, ID_ATTRIBUTE, autogenerateId()); 131 } 132 writeOptionalAttribute(tagWriter, "name", getName()); 133 } 134 135 139 protected String autogenerateId() throws JspException { 140 return getName(); 141 } 142 143 152 protected String getName() throws JspException { 153 return getCompletePath(); 154 } 155 156 160 protected final Object getBoundValue() throws JspException { 161 return getBindStatus().getValue(); 162 } 163 164 167 protected PropertyEditor getPropertyEditor() throws JspException { 168 return getBindStatus().getEditor(); 169 } 170 171 174 protected BindStatus getBindStatus() throws JspException { 175 if (this.bindStatus == null) { 176 String resolvedPropertyPath = getPath(); 177 String bindPath = getBindPath(resolvedPropertyPath); 178 this.bindStatus = new BindStatus(getRequestContext(), bindPath, false); 180 } 181 return this.bindStatus; 182 } 183 184 188 private String getBindPath(String resolvedSubPath) { 189 StringBuffer sb = new StringBuffer (); 190 addBindPathElement(sb, getCommandName()); 191 addBindPathElement(sb, getNestedPath()); 192 addBindPathElement(sb, resolvedSubPath); 193 return sb.toString(); 194 } 195 196 private void addBindPathElement(StringBuffer sb, String pathElement) { 197 if (StringUtils.hasLength(pathElement)) { 198 int length = sb.length(); 199 if (length > 0 && sb.charAt(length - 1) != PropertyAccessor.NESTED_PROPERTY_SEPARATOR_CHAR) { 200 sb.append(PropertyAccessor.NESTED_PROPERTY_SEPARATOR_CHAR); 201 } 202 sb.append(pathElement); 203 } 204 } 205 206 210 protected String getNestedPath() { 211 return (String ) this.pageContext.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE); 212 } 213 214 219 protected String getCompletePath() throws JspException { 220 String nestedPath = getNestedPath(); 221 return (nestedPath != null ? nestedPath + getPath() : getPath()); 222 } 223 224 private String getCommandName() { 225 return (String ) this.pageContext.getAttribute(COMMAND_NAME_VARIABLE_NAME, PageContext.REQUEST_SCOPE); 226 } 227 228 231 public void doFinally() { 232 super.doFinally(); 233 this.bindStatus = null; 234 } 235 236 } 237 | Popular Tags |