1 16 17 package org.springframework.web.servlet.mvc; 18 19 import javax.servlet.http.HttpServletRequest ; 20 21 import org.springframework.beans.BeanUtils; 22 import org.springframework.beans.PropertyEditorRegistrar; 23 import org.springframework.validation.BindException; 24 import org.springframework.validation.BindingErrorProcessor; 25 import org.springframework.validation.MessageCodesResolver; 26 import org.springframework.validation.ValidationUtils; 27 import org.springframework.validation.Validator; 28 import org.springframework.web.bind.ServletRequestDataBinder; 29 30 133 public abstract class BaseCommandController extends AbstractController { 134 135 136 public static final String DEFAULT_COMMAND_NAME = "command"; 137 138 139 private String commandName = DEFAULT_COMMAND_NAME; 140 141 private Class commandClass; 142 143 private Validator[] validators; 144 145 private boolean validateOnBinding = true; 146 147 private MessageCodesResolver messageCodesResolver; 148 149 private BindingErrorProcessor bindingErrorProcessor; 150 151 private PropertyEditorRegistrar[] propertyEditorRegistrars; 152 153 154 158 public final void setCommandName(String commandName) { 159 this.commandName = commandName; 160 } 161 162 165 public final String getCommandName() { 166 return this.commandName; 167 } 168 169 173 public final void setCommandClass(Class commandClass) { 174 this.commandClass = commandClass; 175 } 176 177 180 public final Class getCommandClass() { 181 return this.commandClass; 182 } 183 184 191 public final void setValidator(Validator validator) { 192 this.validators = new Validator[] {validator}; 193 } 194 195 198 public final Validator getValidator() { 199 return (this.validators != null && this.validators.length > 0 ? this.validators[0] : null); 200 } 201 202 206 public final void setValidators(Validator[] validators) { 207 this.validators = validators; 208 } 209 210 213 public final Validator[] getValidators() { 214 return validators; 215 } 216 217 220 public final void setValidateOnBinding(boolean validateOnBinding) { 221 this.validateOnBinding = validateOnBinding; 222 } 223 224 227 public final boolean isValidateOnBinding() { 228 return validateOnBinding; 229 } 230 231 239 public final void setMessageCodesResolver(MessageCodesResolver messageCodesResolver) { 240 this.messageCodesResolver = messageCodesResolver; 241 } 242 243 246 public final MessageCodesResolver getMessageCodesResolver() { 247 return messageCodesResolver; 248 } 249 250 258 public final void setBindingErrorProcessor(BindingErrorProcessor bindingErrorProcessor) { 259 this.bindingErrorProcessor = bindingErrorProcessor; 260 } 261 262 265 public final BindingErrorProcessor getBindingErrorProcessor() { 266 return bindingErrorProcessor; 267 } 268 269 276 public final void setPropertyEditorRegistrar(PropertyEditorRegistrar propertyEditorRegistrar) { 277 this.propertyEditorRegistrars = new PropertyEditorRegistrar[] {propertyEditorRegistrar}; 278 } 279 280 287 public final void setPropertyEditorRegistrars(PropertyEditorRegistrar[] propertyEditorRegistrars) { 288 this.propertyEditorRegistrars = propertyEditorRegistrars; 289 } 290 291 295 public final PropertyEditorRegistrar[] getPropertyEditorRegistrars() { 296 return propertyEditorRegistrars; 297 } 298 299 300 protected void initApplicationContext() { 301 if (this.validators != null) { 302 for (int i = 0; i < this.validators.length; i++) { 303 if (this.commandClass != null && !this.validators[i].supports(this.commandClass)) 304 throw new IllegalArgumentException ("Validator [" + this.validators[i] + 305 "] does not support command class [" + 306 this.commandClass.getName() + "]"); 307 } 308 } 309 } 310 311 312 321 protected Object getCommand(HttpServletRequest request) throws Exception { 322 return createCommand(); 323 } 324 325 334 protected final Object createCommand() throws Exception { 335 if (this.commandClass == null) { 336 throw new IllegalStateException ("Cannot create command without commandClass being set - " + 337 "either set commandClass or (in a form controller) override formBackingObject"); 338 } 339 if (logger.isDebugEnabled()) { 340 logger.debug("Creating new command of class [" + this.commandClass.getName() + "]"); 341 } 342 return BeanUtils.instantiateClass(this.commandClass); 343 } 344 345 351 protected final boolean checkCommand(Object command) { 352 return (this.commandClass == null || this.commandClass.isInstance(command)); 353 } 354 355 356 363 protected final ServletRequestDataBinder bindAndValidate(HttpServletRequest request, Object command) 364 throws Exception { 365 366 ServletRequestDataBinder binder = createBinder(request, command); 367 BindException errors = new BindException(binder.getBindingResult()); 368 if (!suppressBinding(request)) { 369 binder.bind(request); 370 onBind(request, command, errors); 371 if (this.validators != null && isValidateOnBinding() && !suppressValidation(request, command)) { 372 for (int i = 0; i < this.validators.length; i++) { 373 ValidationUtils.invokeValidator(this.validators[i], command, errors); 374 } 375 } 376 onBindAndValidate(request, command, errors); 377 } 378 return binder; 379 } 380 381 390 protected boolean suppressBinding(HttpServletRequest request) { 391 return false; 392 } 393 394 411 protected ServletRequestDataBinder createBinder(HttpServletRequest request, Object command) 412 throws Exception { 413 414 ServletRequestDataBinder binder = new ServletRequestDataBinder(command, getCommandName()); 415 prepareBinder(binder); 416 initBinder(request, binder); 417 return binder; 418 } 419 420 429 protected final void prepareBinder(ServletRequestDataBinder binder) { 430 if (useDirectFieldAccess()) { 431 binder.initDirectFieldAccess(); 432 } 433 if (this.messageCodesResolver != null) { 434 binder.setMessageCodesResolver(this.messageCodesResolver); 435 } 436 if (this.bindingErrorProcessor != null) { 437 binder.setBindingErrorProcessor(this.bindingErrorProcessor); 438 } 439 if (this.propertyEditorRegistrars != null) { 440 for (int i = 0; i < this.propertyEditorRegistrars.length; i++) { 441 this.propertyEditorRegistrars[i].registerCustomEditors(binder); 442 } 443 } 444 } 445 446 453 protected boolean useDirectFieldAccess() { 454 return false; 455 } 456 457 472 protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) 473 throws Exception { 474 } 475 476 488 protected void onBind(HttpServletRequest request, Object command, BindException errors) 489 throws Exception { 490 491 onBind(request, command); 492 } 493 494 505 protected void onBind(HttpServletRequest request, Object command) throws Exception { 506 } 507 508 515 protected boolean suppressValidation(HttpServletRequest request, Object command) { 516 return suppressValidation(request); 517 } 518 519 528 protected boolean suppressValidation(HttpServletRequest request) { 529 return false; 530 } 531 532 545 protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) 546 throws Exception { 547 } 548 549 } 550 | Popular Tags |