1 16 17 package org.springframework.web.portlet.mvc; 18 19 import javax.portlet.ActionRequest; 20 import javax.portlet.PortletException; 21 import javax.portlet.PortletRequest; 22 import javax.portlet.PortletSession; 23 import javax.portlet.RenderRequest; 24 25 import org.springframework.beans.BeanUtils; 26 import org.springframework.beans.PropertyEditorRegistrar; 27 import org.springframework.validation.BindException; 28 import org.springframework.validation.BindingErrorProcessor; 29 import org.springframework.validation.MessageCodesResolver; 30 import org.springframework.validation.ValidationUtils; 31 import org.springframework.validation.Validator; 32 import org.springframework.web.portlet.bind.PortletRequestDataBinder; 33 import org.springframework.web.portlet.handler.PortletSessionRequiredException; 34 35 139 public abstract class BaseCommandController extends AbstractController { 140 141 160 private static final String RENDER_COMMAND_SESSION_ATTRIBUTE = 161 "org.springframework.web.portlet.mvc.RenderCommand"; 162 163 private static final String RENDER_ERRORS_SESSION_ATTRIBUTE = 164 "org.springframework.web.portlet.mvc.RenderErrors"; 165 166 public static final String DEFAULT_COMMAND_NAME = "command"; 167 168 169 private String commandName = DEFAULT_COMMAND_NAME; 170 171 private Class commandClass; 172 173 private Validator[] validators; 174 175 private boolean validateOnBinding = true; 176 177 private MessageCodesResolver messageCodesResolver; 178 179 private BindingErrorProcessor bindingErrorProcessor; 180 181 private PropertyEditorRegistrar[] propertyEditorRegistrars; 182 183 184 188 public final void setCommandName(String commandName) { 189 this.commandName = commandName; 190 } 191 192 195 public final String getCommandName() { 196 return this.commandName; 197 } 198 199 203 public final void setCommandClass(Class commandClass) { 204 this.commandClass = commandClass; 205 } 206 207 210 public final Class getCommandClass() { 211 return this.commandClass; 212 } 213 214 221 public final void setValidator(Validator validator) { 222 this.validators = new Validator[] {validator}; 223 } 224 225 228 public final Validator getValidator() { 229 return (validators != null && validators.length > 0 ? validators[0] : null); 230 } 231 232 236 public final void setValidators(Validator[] validators) { 237 this.validators = validators; 238 } 239 240 243 public final Validator[] getValidators() { 244 return validators; 245 } 246 247 250 public final void setValidateOnBinding(boolean validateOnBinding) { 251 this.validateOnBinding = validateOnBinding; 252 } 253 254 257 public final boolean isValidateOnBinding() { 258 return validateOnBinding; 259 } 260 261 268 public final void setMessageCodesResolver(MessageCodesResolver messageCodesResolver) { 269 this.messageCodesResolver = messageCodesResolver; 270 } 271 272 275 public final MessageCodesResolver getMessageCodesResolver() { 276 return messageCodesResolver; 277 } 278 279 287 public final void setBindingErrorProcessor(BindingErrorProcessor bindingErrorProcessor) { 288 this.bindingErrorProcessor = bindingErrorProcessor; 289 } 290 291 294 public final BindingErrorProcessor getBindingErrorProcessor() { 295 return bindingErrorProcessor; 296 } 297 298 305 public final void setPropertyEditorRegistrar(PropertyEditorRegistrar propertyEditorRegistrar) { 306 this.propertyEditorRegistrars = new PropertyEditorRegistrar[] {propertyEditorRegistrar}; 307 } 308 309 316 public final void setPropertyEditorRegistrars(PropertyEditorRegistrar[] propertyEditorRegistrars) { 317 this.propertyEditorRegistrars = propertyEditorRegistrars; 318 } 319 320 324 public final PropertyEditorRegistrar[] getPropertyEditorRegistrars() { 325 return propertyEditorRegistrars; 326 } 327 328 329 protected void initApplicationContext() { 330 if (this.validators != null) { 331 for (int i = 0; i < this.validators.length; i++) { 332 if (this.commandClass != null && !this.validators[i].supports(this.commandClass)) 333 throw new IllegalArgumentException ("Validator [" + this.validators[i] + 334 "] does not support command class [" + 335 this.commandClass.getName() + "]"); 336 } 337 } 338 } 339 340 341 349 protected Object getCommand(PortletRequest request) throws Exception { 350 return createCommand(); 351 } 352 353 362 protected final Object createCommand() throws Exception { 363 if (this.commandClass == null) { 364 throw new IllegalStateException ("Cannot create command without commandClass being set - " + 365 "either set commandClass or (in a form controller) override formBackingObject"); 366 } 367 if (logger.isDebugEnabled()) { 368 logger.debug("Creating new command of class [" + this.commandClass.getName() + "]"); 369 } 370 return BeanUtils.instantiateClass(this.commandClass); 371 } 372 373 379 protected final boolean checkCommand(Object command) { 380 return (this.commandClass == null || this.commandClass.isInstance(command)); 381 } 382 383 384 391 protected final PortletRequestDataBinder bindAndValidate(PortletRequest request, Object command) 392 throws Exception { 393 394 PortletRequestDataBinder binder = createBinder(request, command); 395 if (!suppressBinding(request)) { 396 binder.bind(request); 397 BindException errors = new BindException(binder.getBindingResult()); 398 onBind(request, command, errors); 399 if (this.validators != null && isValidateOnBinding() && !suppressValidation(request)) { 400 for (int i = 0; i < this.validators.length; i++) { 401 ValidationUtils.invokeValidator(this.validators[i], command, errors); 402 } 403 } 404 onBindAndValidate(request, command, errors); 405 } 406 return binder; 407 } 408 409 418 protected boolean suppressBinding(PortletRequest request) { 419 return false; 420 } 421 422 439 protected PortletRequestDataBinder createBinder(PortletRequest request, Object command) 440 throws Exception { 441 442 PortletRequestDataBinder binder = new PortletRequestDataBinder(command, getCommandName()); 443 prepareBinder(binder); 444 initBinder(request, binder); 445 return binder; 446 } 447 448 457 protected final void prepareBinder(PortletRequestDataBinder binder) { 458 if (useDirectFieldAccess()) { 459 binder.initDirectFieldAccess(); 460 } 461 if (this.messageCodesResolver != null) { 462 binder.setMessageCodesResolver(this.messageCodesResolver); 463 } 464 if (this.bindingErrorProcessor != null) { 465 binder.setBindingErrorProcessor(this.bindingErrorProcessor); 466 } 467 if (this.propertyEditorRegistrars != null) { 468 for (int i = 0; i < this.propertyEditorRegistrars.length; i++) { 469 this.propertyEditorRegistrars[i].registerCustomEditors(binder); 470 } 471 } 472 } 473 474 481 protected boolean useDirectFieldAccess() { 482 return false; 483 } 484 485 500 protected void initBinder(PortletRequest request, PortletRequestDataBinder binder) 501 throws Exception { 502 } 503 504 516 protected void onBind(PortletRequest request, Object command, BindException errors) 517 throws Exception { 518 519 onBind(request, command); 520 } 521 522 532 protected void onBind(PortletRequest request, Object command) throws Exception { 533 } 534 535 543 protected boolean suppressValidation(PortletRequest request) { 544 return false; 545 } 546 547 560 protected void onBindAndValidate(PortletRequest request, Object command, BindException errors) 561 throws Exception { 562 } 563 564 565 571 protected String getRenderCommandSessionAttributeName() { 572 return RENDER_COMMAND_SESSION_ATTRIBUTE; 573 } 574 575 581 protected String getRenderErrorsSessionAttributeName() { 582 return RENDER_ERRORS_SESSION_ATTRIBUTE; 583 } 584 585 591 protected final Object getRenderCommand(RenderRequest request) throws PortletException { 592 PortletSession session = request.getPortletSession(false); 593 if (session == null) { 594 throw new PortletSessionRequiredException("Could not obtain portlet session"); 595 } 596 Object command = session.getAttribute(getRenderCommandSessionAttributeName()); 597 if (command == null) { 598 throw new PortletSessionRequiredException("Could not obtain command object from portlet session"); 599 } 600 return command; 601 } 602 603 609 protected final BindException getRenderErrors(RenderRequest request) throws PortletException { 610 PortletSession session = request.getPortletSession(false); 611 if (session == null) { 612 throw new PortletSessionRequiredException("Could not obtain portlet session"); 613 } 614 BindException errors = (BindException) session.getAttribute(getRenderErrorsSessionAttributeName()); 615 if (errors == null) { 616 throw new PortletSessionRequiredException("Could not obtain errors object from portlet session"); 617 } 618 return errors; 619 } 620 621 631 protected final void setRenderCommandAndErrors( 632 ActionRequest request, Object command, BindException errors) throws Exception { 633 634 logger.debug("Storing command and error objects in session for render phase"); 635 PortletSession session = request.getPortletSession(); 636 session.setAttribute(getRenderCommandSessionAttributeName(), command); 637 session.setAttribute(getRenderErrorsSessionAttributeName(), errors); 638 } 639 640 } 641 | Popular Tags |