📄 basecommandcontroller.java
字号:
* Return the PropertyEditorRegistrars to be applied
* to every DataBinder that this controller uses.
*/
public final PropertyEditorRegistrar[] getPropertyEditorRegistrars() {
return this.propertyEditorRegistrars;
}
protected void initApplicationContext() {
if (this.validators != null) {
for (int i = 0; i < this.validators.length; i++) {
if (this.commandClass != null && !this.validators[i].supports(this.commandClass))
throw new IllegalArgumentException("Validator [" + this.validators[i] +
"] does not support command class [" +
this.commandClass.getName() + "]");
}
}
}
/**
* Retrieve a command object for the given request.
* <p>Default implementation calls <code>createCommand</code>.
* Subclasses can override this.
* @param request current portlet request
* @return object command to bind onto
* @see #createCommand
*/
protected Object getCommand(PortletRequest request) throws Exception {
return createCommand();
}
/**
* Create a new command instance for the command class of this controller.
* <p>This implementation uses <code>BeanUtils.instantiateClass</code>,
* so the command needs to have a no-arg constructor (supposed to be
* public, but not required to).
* @return the new command instance
* @throws Exception if the command object could not be instantiated
* @see org.springframework.beans.BeanUtils#instantiateClass(Class)
*/
protected final Object createCommand() throws Exception {
if (this.commandClass == null) {
throw new IllegalStateException("Cannot create command without commandClass being set - " +
"either set commandClass or (in a form controller) override formBackingObject");
}
if (logger.isDebugEnabled()) {
logger.debug("Creating new command of class [" + this.commandClass.getName() + "]");
}
return BeanUtils.instantiateClass(this.commandClass);
}
/**
* Check if the given command object is a valid for this controller,
* i.e. its command class.
* @param command the command object to check
* @return if the command object is valid for this controller
*/
protected final boolean checkCommand(Object command) {
return (this.commandClass == null || this.commandClass.isInstance(command));
}
/**
* Bind the parameters of the given request to the given command object.
* @param request current portlet request
* @param command the command to bind onto
* @return the PortletRequestDataBinder instance for additional custom validation
* @throws Exception in case of invalid state or arguments
*/
protected final PortletRequestDataBinder bindAndValidate(PortletRequest request, Object command)
throws Exception {
PortletRequestDataBinder binder = createBinder(request, command);
if (!suppressBinding(request)) {
binder.bind(request);
BindException errors = new BindException(binder.getBindingResult());
onBind(request, command, errors);
if (this.validators != null && isValidateOnBinding() && !suppressValidation(request)) {
for (int i = 0; i < this.validators.length; i++) {
ValidationUtils.invokeValidator(this.validators[i], command, errors);
}
}
onBindAndValidate(request, command, errors);
}
return binder;
}
/**
* Return whether to suppress binding for the given request.
* <p>Default implementation always returns "false". Can be overridden
* in subclasses to suppress validation, for example, if a special
* request parameter is set.
* @param request current portlet request
* @return whether to suppress binding for the given request
* @see #suppressValidation
*/
protected boolean suppressBinding(PortletRequest request) {
return false;
}
/**
* Create a new binder instance for the given command and request.
* <p>Called by <code>bindAndValidate</code>. Can be overridden to plug in
* custom PortletRequestDataBinder instances.
* <p>The default implementation creates a standard PortletRequestDataBinder and
* invokes <code>prepareBinder</code> and <code>initBinder</code>.
* <p>Note that neither <code>prepareBinder</code> nor <code>initBinder</code>
* will be invoked automatically if you override this method! Call those methods
* at appropriate points of your overridden method.
* @param request current portlet request
* @param command the command to bind onto
* @return the new binder instance
* @throws Exception in case of invalid state or arguments
* @see #bindAndValidate
* @see #prepareBinder
* @see #initBinder
*/
protected PortletRequestDataBinder createBinder(PortletRequest request, Object command)
throws Exception {
PortletRequestDataBinder binder = new PortletRequestDataBinder(command, getCommandName());
prepareBinder(binder);
initBinder(request, binder);
return binder;
}
/**
* Prepare the given binder, applying the specified MessageCodesResolver,
* BindingErrorProcessor and PropertyEditorRegistrars (if any).
* Called by <code>createBinder</code>.
* @param binder the new binder instance
* @see #createBinder
* @see #setMessageCodesResolver
* @see #setBindingErrorProcessor
*/
protected final void prepareBinder(PortletRequestDataBinder binder) {
if (useDirectFieldAccess()) {
binder.initDirectFieldAccess();
}
if (this.messageCodesResolver != null) {
binder.setMessageCodesResolver(this.messageCodesResolver);
}
if (this.bindingErrorProcessor != null) {
binder.setBindingErrorProcessor(this.bindingErrorProcessor);
}
if (this.propertyEditorRegistrars != null) {
for (int i = 0; i < this.propertyEditorRegistrars.length; i++) {
this.propertyEditorRegistrars[i].registerCustomEditors(binder);
}
}
}
/**
* Determine whether to use direct field access instead of bean property access.
* Applied by <code>prepareBinder</code>.
* <p>Default is "false". Can be overridden in subclasses.
* @see #prepareBinder
* @see org.springframework.validation.DataBinder#initDirectFieldAccess()
*/
protected boolean useDirectFieldAccess() {
return false;
}
/**
* Initialize the given binder instance, for example with custom editors.
* Called by <code>createBinder</code>.
* <p>This method allows you to register custom editors for certain fields of your
* command class. For instance, you will be able to transform Date objects into a
* String pattern and back, in order to allow your JavaBeans to have Date properties
* and still be able to set and display them in an HTML interface.
* <p>Default implementation is empty.
* @param request current portlet request
* @param binder new binder instance
* @throws Exception in case of invalid state or arguments
* @see #createBinder
* @see org.springframework.validation.DataBinder#registerCustomEditor
* @see org.springframework.beans.propertyeditors.CustomDateEditor
*/
protected void initBinder(PortletRequest request, PortletRequestDataBinder binder)
throws Exception {
}
/**
* Callback for custom post-processing in terms of binding.
* Called on each submit, after standard binding but before validation.
* <p>Default implementation delegates to <code>onBind(request, command)</code>.
* @param request current portlet request
* @param command the command object to perform further binding on
* @param errors validation errors holder, allowing for additional
* custom registration of binding errors
* @throws Exception in case of invalid state or arguments
* @see #bindAndValidate
* @see #onBind(PortletRequest, Object)
*/
protected void onBind(PortletRequest request, Object command, BindException errors)
throws Exception {
onBind(request, command);
}
/**
* Callback for custom post-processing in terms of binding.
* Called by the default implementation of the <code>onBind</code> version with
* all parameters, after standard binding but before validation.
* <p>Default implementation is empty.
* @param request current portlet request
* @param command the command object to perform further binding on
* @throws Exception in case of invalid state or arguments
* @see #onBind(PortletRequest, Object, BindException)
*/
protected void onBind(PortletRequest request, Object command) throws Exception {
}
/**
* Return whether to suppress validation for the given request.
* <p>Default implementation always returns "false". Can be overridden
* in subclasses to suppress validation, for example, if a special
* request parameter is set.
* @param request current portlet request
* @return whether to suppress validation for the given request
*/
protected boolean suppressValidation(PortletRequest request) {
return false;
}
/**
* Callback for custom post-processing in terms of binding and validation.
* Called on each submit, after standard binding and validation,
* but before error evaluation.
* <p>Default implementation is empty.
* @param request current portlet request
* @param command the command object, still allowing for further binding
* @param errors validation errors holder, allowing for additional
* custom validation
* @throws Exception in case of invalid state or arguments
* @see #bindAndValidate
* @see org.springframework.validation.Errors
*/
protected void onBindAndValidate(PortletRequest request, Object command, BindException errors)
throws Exception {
}
/**
* Return the name of the session attribute that holds
* the render phase command object for this form controller.
* @return the name of the render phase command object session attribute
* @see javax.portlet.PortletSession#getAttribute
*/
protected String getRenderCommandSessionAttributeName() {
return RENDER_COMMAND_SESSION_ATTRIBUTE;
}
/**
* Return the name of the session attribute that holds
* the render phase command object for this form controller.
* @return the name of the render phase command object session attribute
* @see javax.portlet.PortletSession#getAttribute
*/
protected String getRenderErrorsSessionAttributeName() {
return RENDER_ERRORS_SESSION_ATTRIBUTE;
}
/**
* Get the command object cached for the render phase.
* @see #getRenderErrors
* @see #getRenderCommandSessionAttributeName
* @see #setRenderCommandAndErrors
*/
protected final Object getRenderCommand(RenderRequest request) throws PortletException {
PortletSession session = request.getPortletSession(false);
if (session == null) {
throw new PortletSessionRequiredException("Could not obtain portlet session");
}
Object command = session.getAttribute(getRenderCommandSessionAttributeName());
if (command == null) {
throw new PortletSessionRequiredException("Could not obtain command object from portlet session");
}
return command;
}
/**
* Get the bind and validation errors cached for the render phase.
* @see #getRenderCommand
* @see #getRenderErrorsSessionAttributeName
* @see #setRenderCommandAndErrors
*/
protected final BindException getRenderErrors(RenderRequest request) throws PortletException {
PortletSession session = request.getPortletSession(false);
if (session == null) {
throw new PortletSessionRequiredException("Could not obtain portlet session");
}
BindException errors = (BindException) session.getAttribute(getRenderErrorsSessionAttributeName());
if (errors == null) {
throw new PortletSessionRequiredException("Could not obtain errors object from portlet session");
}
return errors;
}
/**
* Set the command object and errors object for the render phase.
* @param request the current action request
* @param command the command object to preserve for the render phase
* @param errors the errors from binding and validation to preserve for the render phase
* @see #getRenderCommand
* @see #getRenderErrors
* @see #getRenderCommandSessionAttributeName
* @see #getRenderErrorsSessionAttributeName
*/
protected final void setRenderCommandAndErrors(
ActionRequest request, Object command, BindException errors) throws Exception {
logger.debug("Storing command and error objects in session for render phase");
PortletSession session = request.getPortletSession();
session.setAttribute(getRenderCommandSessionAttributeName(), command);
session.setAttribute(getRenderErrorsSessionAttributeName(), errors);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -