1 16 17 package org.springframework.web.servlet.support; 18 19 import java.beans.PropertyEditor ; 20 import java.util.Arrays ; 21 import java.util.List ; 22 23 import org.springframework.beans.BeanWrapperImpl; 24 import org.springframework.context.NoSuchMessageException; 25 import org.springframework.util.StringUtils; 26 import org.springframework.validation.AbstractPropertyBindingResult; 27 import org.springframework.validation.BindException; 28 import org.springframework.validation.Errors; 29 import org.springframework.validation.ObjectError; 30 import org.springframework.web.util.HtmlUtils; 31 32 48 public class BindStatus { 49 50 private final RequestContext requestContext; 51 52 private final String path; 53 54 private final boolean htmlEscape; 55 56 private final String expression; 57 58 private final Errors errors; 59 60 private Object value; 61 62 private Class valueType; 63 64 private PropertyEditor editor; 65 66 private List objectErrors; 67 68 private String [] errorCodes; 69 70 private String [] errorMessages; 71 72 73 81 public BindStatus(RequestContext requestContext, String path, boolean htmlEscape) 82 throws IllegalStateException { 83 84 this.requestContext = requestContext; 85 this.path = path; 86 this.htmlEscape = htmlEscape; 87 88 String beanName = null; 90 int dotPos = path.indexOf('.'); 91 if (dotPos == -1) { 92 beanName = path; 94 this.expression = null; 95 } 96 else { 97 beanName = path.substring(0, dotPos); 98 this.expression = path.substring(dotPos + 1); 99 } 100 101 this.errors = requestContext.getErrors(beanName, false); 102 103 if (this.errors != null) { 104 108 if (this.expression != null) { 109 if ("*".equals(this.expression)) { 110 this.objectErrors = this.errors.getAllErrors(); 111 } 112 else if (this.expression.endsWith("*")) { 113 this.objectErrors = this.errors.getFieldErrors(this.expression); 114 } 115 else { 116 this.objectErrors = this.errors.getFieldErrors(this.expression); 117 this.value = this.errors.getFieldValue(this.expression); 118 this.valueType = this.errors.getFieldType(this.expression); 119 this.editor = getCustomEditor(this.errors, this.expression); 120 } 121 } 122 123 else { 124 this.objectErrors = this.errors.getGlobalErrors(); 125 } 126 127 initErrorCodes(); 128 } 129 130 else { 131 135 Object target = requestContext.getModelObject(beanName); 136 if (target == null) { 137 throw new IllegalStateException ("Neither BindingResult nor plain target object for bean name '" + 138 beanName + "' available as request attribute"); 139 } 140 141 if (this.expression != null && !"*".equals(this.expression) && !this.expression.endsWith("*")) { 142 BeanWrapperImpl bw = new BeanWrapperImpl(target); 143 this.valueType = bw.getPropertyType(this.expression); 144 this.value = bw.getPropertyValue(this.expression); 145 } 146 147 this.errorCodes = new String [0]; 148 this.errorMessages = new String [0]; 149 } 150 151 if (htmlEscape && this.value instanceof String ) { 152 this.value = HtmlUtils.htmlEscape((String ) this.value); 153 } 154 } 155 156 160 private PropertyEditor getCustomEditor(Errors errors, String field) { 161 Errors bindingResult = errors; 162 if (errors instanceof BindException) { 164 bindingResult = ((BindException) errors).getBindingResult(); 165 } 166 if (bindingResult instanceof AbstractPropertyBindingResult) { 167 return ((AbstractPropertyBindingResult) bindingResult).getCustomEditor(field); 168 } 169 return null; 170 } 171 172 175 private void initErrorCodes() { 176 this.errorCodes = new String [this.objectErrors.size()]; 177 for (int i = 0; i < this.objectErrors.size(); i++) { 178 ObjectError error = (ObjectError) this.objectErrors.get(i); 179 this.errorCodes[i] = error.getCode(); 180 } 181 } 182 183 186 private void initErrorMessages() throws NoSuchMessageException { 187 if (this.errorMessages == null) { 188 this.errorMessages = new String [this.objectErrors.size()]; 189 for (int i = 0; i < this.objectErrors.size(); i++) { 190 ObjectError error = (ObjectError) this.objectErrors.get(i); 191 this.errorMessages[i] = this.requestContext.getMessage(error, this.htmlEscape); 192 } 193 } 194 } 195 196 197 201 public String getPath() { 202 return this.path; 203 } 204 205 212 public String getExpression() { 213 return this.expression; 214 } 215 216 222 public Object getValue() { 223 return this.value; 224 } 225 226 231 public Class getValueType() { 232 return this.valueType; 233 } 234 235 242 public String getDisplayValue() { 243 if (this.value instanceof String ) { 244 return (String ) this.value; 245 } 246 if (this.value != null) { 247 return (this.htmlEscape ? HtmlUtils.htmlEscape(this.value.toString()) : this.value.toString()); 248 } 249 return ""; 250 } 251 252 255 public boolean isError() { 256 return (this.errorCodes != null && this.errorCodes.length > 0); 257 } 258 259 263 public String [] getErrorCodes() { 264 return this.errorCodes; 265 } 266 267 270 public String getErrorCode() { 271 return (this.errorCodes.length > 0 ? this.errorCodes[0] : ""); 272 } 273 274 278 public String [] getErrorMessages() { 279 initErrorMessages(); 280 return this.errorMessages; 281 } 282 283 286 public String getErrorMessage() { 287 initErrorMessages(); 288 return (this.errorMessages.length > 0 ? this.errorMessages[0] : ""); 289 } 290 291 297 public String getErrorMessagesAsString(String delimiter) { 298 initErrorMessages(); 299 return StringUtils.arrayToDelimitedString(this.errorMessages, delimiter); 300 } 301 302 308 public Errors getErrors() { 309 return this.errors; 310 } 311 312 317 public PropertyEditor getEditor() { 318 return this.editor; 319 } 320 321 322 public String toString() { 323 StringBuffer sb = new StringBuffer ("BindStatus: "); 324 sb.append("expression=[").append(this.expression).append("]; "); 325 sb.append("value=[").append(this.value).append("]"); 326 if (isError()) { 327 sb.append("; errorCodes=" + Arrays.asList(this.errorCodes)); 328 } 329 return sb.toString(); 330 } 331 332 } 333 | Popular Tags |