com.interface21.web.servlet.mvc
Class SimpleFormController

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
                          |
                          +--com.interface21.web.servlet.mvc.AbstractFormController
                                |
                                +--com.interface21.web.servlet.mvc.SimpleFormController
All Implemented Interfaces:
ApplicationContextAware, Controller

public class SimpleFormController
extends AbstractFormController

Concrete FormController implementation that provides configurable form and success views, and an onSubmit chain for convenient overriding.

Automatically resubmits to the form view in case of validation errors, and renders the success view in case of a valid submission.

The submit behavior can be customized by overriding one of the onSubmit methods. Submit actions can be used as custom validation if necessary (e.g. login), by calling showForm in case of validation errors.

Since:
05.05.2003
Author:
Juergen Hoeller

Fields inherited from class com.interface21.web.servlet.mvc.BaseCommandController
DEFAULT_BEAN_NAME
 
Fields inherited from class com.interface21.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
SimpleFormController()
          Create a new SimpleFormController.
 
Method Summary
protected  java.lang.String getFormView()
          Return the name of the view that should be used for form display.
protected  java.lang.String getSuccessView()
          Return the name of the view that should be shown on successful submit.
protected  ModelAndView onSubmit(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.Object command, BindException errors)
          Submit callback with all parameters.
protected  ModelAndView onSubmit(java.lang.Object command)
          Simplest onSubmit version.
protected  ModelAndView processSubmit(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.Object command, BindException errors)
          This implementation calls showForm in case of errors, and delegates to onSubmit's full version else.
protected  java.util.Map referenceData(javax.servlet.http.HttpServletRequest request)
          Create a reference data map for the given request.
protected  java.util.Map referenceData(javax.servlet.http.HttpServletRequest request, java.lang.Object command, Errors errors)
          Create a reference data map for the given request, command, and errors, consisting of bean name/bean instance pairs as expected by ModelAndView.
 void setFormView(java.lang.String formView)
          Set the name of the view that should be used for form display.
 void setSuccessView(java.lang.String successView)
          Set the name of the view that should be shown on successful submit.
protected  ModelAndView showForm(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, BindException errors)
          This implementation shows the configured form view.
 
Methods inherited from class com.interface21.web.servlet.mvc.AbstractFormController
formBackingObject, getFormSessionAttributeName, handleInvalidSubmit, handleRequestInternal, isBindOnNewForm, isFormSubmission, isSessionForm, setBindOnNewForm, setSessionForm, showForm, showForm, showNewForm, userObject
 
Methods inherited from class com.interface21.web.servlet.mvc.BaseCommandController
bindAndValidate, checkCommand, createBinder, createCommand, getBeanName, getCommandClass, getValidator, initBinder, isValidateOnBinding, onBindAndValidate, setBeanName, setCommandClass, setValidateOnBinding, setValidator
 
Methods inherited from class com.interface21.web.servlet.mvc.AbstractController
handleRequest, 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
 

Constructor Detail

SimpleFormController

public SimpleFormController()
Create a new SimpleFormController.

Subclasses should set the following properties, either in the constructor or via a BeanFactory: commandClass, beanName, sessionForm, formView, successView. Note that commandClass doesn't need to be set when overriding formBackingObject, as this determines the class anyway.

See Also:
BaseCommandController.setCommandClass(java.lang.Class), BaseCommandController.setBeanName(java.lang.String), AbstractFormController.setSessionForm(boolean), setFormView(java.lang.String), setSuccessView(java.lang.String)
Method Detail

setFormView

public final void setFormView(java.lang.String formView)
Set the name of the view that should be used for form display.

getFormView

protected final java.lang.String getFormView()
Return the name of the view that should be used for form display.

setSuccessView

public final void setSuccessView(java.lang.String successView)
Set the name of the view that should be shown on successful submit.

getSuccessView

protected final java.lang.String getSuccessView()
Return the name of the view that should be shown on successful submit.

referenceData

protected java.util.Map referenceData(javax.servlet.http.HttpServletRequest request,
                                      java.lang.Object command,
                                      Errors errors)
                               throws javax.servlet.ServletException
Create a reference data map for the given request, command, and errors, consisting of bean name/bean instance pairs as expected by ModelAndView.

Default implementation delegates to referenceData(request). Subclasses can override this to set reference data used in the view.

Overrides:
referenceData in class AbstractFormController
Parameters:
request - current HTTP request
command - form object with request parameters bound onto it
errors - binder containing current errors, if any
Returns:
a Map with reference data entries, or null if none
Throws:
javax.servlet.ServletException - in case of invalid state or arguments
See Also:
ModelAndView

referenceData

protected java.util.Map referenceData(javax.servlet.http.HttpServletRequest request)
                               throws javax.servlet.ServletException
Create a reference data map for the given request. Called by referenceData version with all parameters.

Default implementation returns null. Subclasses can override this to set reference data used in the view.

Parameters:
request - current HTTP request
Returns:
a Map with reference data entries, or null if none
Throws:
javax.servlet.ServletException - in case of invalid state or arguments
See Also:
ModelAndView

showForm

protected final ModelAndView showForm(javax.servlet.http.HttpServletRequest request,
                                      javax.servlet.http.HttpServletResponse response,
                                      BindException errors)
                               throws javax.servlet.ServletException
This implementation shows the configured form view. Can be called within onSubmit implementations, to redirect back to the form in case of custom validation errors (i.e. not determined by the validator).
Overrides:
showForm in class AbstractFormController
See Also:
setFormView(java.lang.String)

processSubmit

protected ModelAndView processSubmit(javax.servlet.http.HttpServletRequest request,
                                     javax.servlet.http.HttpServletResponse response,
                                     java.lang.Object command,
                                     BindException errors)
                              throws javax.servlet.ServletException,
                                     java.io.IOException
This implementation calls showForm in case of errors, and delegates to onSubmit's full version else.

This should only be overridden to check for an action that should be executed without respect to binding errors, like a cancel action.

Overrides:
processSubmit in class AbstractFormController
See Also:
showForm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, com.interface21.validation.BindException), onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)

onSubmit

protected ModelAndView onSubmit(javax.servlet.http.HttpServletRequest request,
                                javax.servlet.http.HttpServletResponse response,
                                java.lang.Object command,
                                BindException errors)
                         throws javax.servlet.ServletException,
                                java.io.IOException
Submit callback with all parameters. Called in case of submit without errors reported by the registered validator resp. on every submit if no validator.

Default implementation delegates to onSubmit(command,errors). Subclasses can override this to provide custom submission handling like triggering a custom action. They can also provide custom validation and call showForm/super.onSubmit accordingly.

Parameters:
request - current servlet request
response - current servlet response
command - form object with request parameters bound onto it
errors - binder without errors (subclass can add errors if it wants to)
Returns:
the prepared model and view, or null
Throws:
javax.servlet.ServletException - in case of invalid state or arguments
java.io.IOException - in case of I/O errors
See Also:
onSubmit(Object), showForm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, com.interface21.validation.BindException)

onSubmit

protected ModelAndView onSubmit(java.lang.Object command)
                         throws javax.servlet.ServletException
Simplest onSubmit version. Called by the default implementation of the onSubmit version with all parameters.

Default implementation prepares success view.

Parameters:
command - form object with request parameters bound onto it
Returns:
the prepared model and view, or null
Throws:
javax.servlet.ServletException - in case of invalid state or arguments
See Also:
setSuccessView(java.lang.String)


Rod Johnson and Spring contributors 2001-2003.