com.interface21.web.servlet.mvc
Class BaseCommandController

java.lang.Object
  |
  +--com.interface21.context.support.ApplicationObjectSupport
        |
        +--com.interface21.web.servlet.mvc.WebContentGenerator
              |
              +--com.interface21.web.servlet.mvc.AbstractController
                    |
                    +--com.interface21.web.servlet.mvc.BaseCommandController
All Implemented Interfaces:
ApplicationContextAware, Controller
Direct Known Subclasses:
AbstractCommandController, AbstractFormController

public abstract class BaseCommandController
extends AbstractController

Controller implementation that creates a Command object on receipt of requests, and attempts to populate the command's JavaBean properties with request attributes.

Once created, commands can be validated using a Validator associated with this class. Type mismatches populating a command are treated as validation errors, but caught by the framework, not application code.

Note: This class is the base class for both AbstractFormController and AbstractCommandController (which in turn is the base class for custom controller implementations).

Author:
Rod Johnson, Juergen Hoeller

Field Summary
static java.lang.String DEFAULT_BEAN_NAME
           
 
Fields inherited from class com.interface21.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
BaseCommandController()
           
 
Method Summary
protected  ServletRequestDataBinder bindAndValidate(javax.servlet.http.HttpServletRequest request, java.lang.Object command)
          Bind the parameters of the given request to the given command object.
protected  boolean checkCommand(java.lang.Object command)
          Check if the given command object is a valid for this controller, i.e. its command class.
protected  ServletRequestDataBinder createBinder(javax.servlet.http.HttpServletRequest request, java.lang.Object command)
          Create a new binder instance for the given command and request.
protected  java.lang.Object createCommand()
          Create a new command instance for the command class of this controller.
protected  java.lang.String getBeanName()
          Return the bean name of the command.
protected  java.lang.Class getCommandClass()
          Return the command class for this controller.
protected  Validator getValidator()
          Return the validator for this controller.
protected  void initBinder(javax.servlet.http.HttpServletRequest request, ServletRequestDataBinder binder)
          Initialize the given binder instance, e.g. with custom editors.
protected  boolean isValidateOnBinding()
          Return if the validator should get applied when binding.
protected  void onBindAndValidate(javax.servlet.http.HttpServletRequest request, java.lang.Object command, BindException errors)
          Callback for custom postprocessing in terms of binding and validation.
 void setBeanName(java.lang.String beanName)
          Set the bean name of the command.
 void setCommandClass(java.lang.Class commandClass)
          Set the command class for this controller.
 void setValidateOnBinding(boolean validateOnBinding)
          Set if the validator should get applied when binding.
 void setValidator(Validator validator)
          Set the validator for this controller (can also be null).
protected  java.lang.Object userObject(javax.servlet.http.HttpServletRequest request)
          Retrieve a user's command object for the given request.
 
Methods inherited from class com.interface21.web.servlet.mvc.AbstractController
handleRequest, handleRequestInternal, setCacheSeconds, setRequireSession, setSupportedMethods
 
Methods inherited from class com.interface21.web.servlet.mvc.WebContentGenerator
cacheForSeconds, preventCaching, setUseExpiresHeader
 
Methods inherited from class com.interface21.context.support.ApplicationObjectSupport
getApplicationContext, initApplicationContext, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BEAN_NAME

public static final java.lang.String DEFAULT_BEAN_NAME
Constructor Detail

BaseCommandController

public BaseCommandController()
Method Detail

setBeanName

public final void setBeanName(java.lang.String beanName)
Set the bean name of the command. The command instance will be included in the model under this name.

getBeanName

protected final java.lang.String getBeanName()
Return the bean name of the command.

setCommandClass

public final void setCommandClass(java.lang.Class commandClass)
Set the command class for this controller. An instance of this class gets populated and validated on each request.

getCommandClass

protected final java.lang.Class getCommandClass()
Return the command class for this controller.

setValidator

public final void setValidator(Validator validator)
Set the validator for this controller (can also be null). The validator must support the specified command class.

getValidator

protected final Validator getValidator()
Return the validator for this controller.

setValidateOnBinding

public final void setValidateOnBinding(boolean validateOnBinding)
Set if the validator should get applied when binding.

isValidateOnBinding

protected final boolean isValidateOnBinding()
Return if the validator should get applied when binding.

checkCommand

protected final boolean checkCommand(java.lang.Object command)
Check if the given command object is a valid for this controller, i.e. its command class.
Parameters:
command - command object to check
Returns:
if the command object is valid for this controller

createCommand

protected final java.lang.Object createCommand()
                                        throws javax.servlet.ServletException
Create a new command instance for the command class of this controller.
Returns:
the new command instance
Throws:
javax.servlet.ServletException - in case of instantiation errors

userObject

protected java.lang.Object userObject(javax.servlet.http.HttpServletRequest request)
                               throws javax.servlet.ServletException
Retrieve a user's command object for the given request.

Default implementation calls createCommand. Subclasses can override this.

Parameters:
request - current HTTP request
Returns:
object command to bind onto
See Also:
createCommand()

bindAndValidate

protected final ServletRequestDataBinder bindAndValidate(javax.servlet.http.HttpServletRequest request,
                                                         java.lang.Object command)
                                                  throws javax.servlet.ServletException
Bind the parameters of the given request to the given command object.
Parameters:
request - current HTTP request
command - command to bind onto
Returns:
the ServletRequestDataBinder instance for additional custom validation
Throws:
javax.servlet.ServletException - in case of invalid state or arguments

createBinder

protected final ServletRequestDataBinder createBinder(javax.servlet.http.HttpServletRequest request,
                                                      java.lang.Object command)
                                               throws javax.servlet.ServletException
Create a new binder instance for the given command and request.
Parameters:
command - command to bind onto
request - current request
Returns:
the new binder instance
Throws:
javax.servlet.ServletException - in case of invalid state or arguments
See Also:
bindAndValidate(javax.servlet.http.HttpServletRequest, java.lang.Object)

initBinder

protected void initBinder(javax.servlet.http.HttpServletRequest request,
                          ServletRequestDataBinder binder)
                   throws javax.servlet.ServletException
Initialize the given binder instance, e.g. with custom editors. Called by createBinder.
Parameters:
request - current request
binder - new binder instance
Throws:
javax.servlet.ServletException - in case of invalid state or arguments
See Also:
createBinder(javax.servlet.http.HttpServletRequest, java.lang.Object)

onBindAndValidate

protected void onBindAndValidate(javax.servlet.http.HttpServletRequest request,
                                 java.lang.Object command,
                                 BindException errors)
                          throws javax.servlet.ServletException
Callback for custom postprocessing in terms of binding and validation. Called on each submit, after standard binding and validation, and before error evaluation.
Parameters:
request - current HTTP request
command - bound command
errors - binder for additional custom validation
Throws:
javax.servlet.ServletException - in case of invalid state or arguments
See Also:
bindAndValidate(javax.servlet.http.HttpServletRequest, java.lang.Object)


Rod Johnson and Spring contributors 2001-2003.