|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.context.support.ApplicationObjectSupport org.springframework.web.context.support.WebApplicationObjectSupport org.springframework.web.servlet.support.WebContentGenerator org.springframework.web.servlet.mvc.AbstractController org.springframework.web.servlet.mvc.BaseCommandController org.springframework.web.servlet.mvc.AbstractFormController org.springframework.web.servlet.mvc.SimpleFormController
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 workflow of this Controller does not differ much from the one described
in the AbstractFormController
. The difference
is that you do not need to implement showForm
and
processFormSubmission
: A form view and a
success view can be configured declaratively.
Workflow
(in addition to the superclass):
processFormSubmission
which inspects
the Errors
object to see if
any errors have occurred during binding and validation.isFormChangeRequest
is overridden and returns
true for the given request, the controller will return the formView too.
In that case, the controller will also suppress validation.
This is intended for requests that change the structure of the form,
which should not cause validation and show the form in any case.onSubmit
using all parameters, which in case of the default implementation delegates to
onSubmit
with just the command object.
The default implementation of the latter method will return the configured
successView. Consider implementing doSubmitAction(java.lang.Object)
doSubmitAction
for simply performing a submit action and rendering the success view.The submit behavior can be customized by overriding one of the
onSubmit
methods. Submit actions can also perform
custom validation if necessary (typically database-driven checks), calling
showForm
in case of validation errors to show the form view again.
Exposed configuration properties
(and those defined by superclass):
name | default | description | |||||||||
formView | null | Indicates what view to use when the user asks for a new form or when validation errors have occurred on form submission. | |||||||||
successView | null | Indicates what view to use when successful form submissions have
occurred. Such a success view could e.g. display a submission summary.
More sophisticated actions can be implemented by overriding one of
the onSubmit() methods. |
Field Summary |
Fields inherited from class org.springframework.web.servlet.mvc.BaseCommandController |
DEFAULT_COMMAND_NAME |
Fields inherited from class org.springframework.web.servlet.support.WebContentGenerator |
HEADER_CACHE_CONTROL, HEADER_EXPIRES, HEADER_PRAGMA, METHOD_GET, METHOD_POST |
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport |
logger |
Constructor Summary | |
SimpleFormController()
Create a new SimpleFormController. |
Method Summary | |
protected void |
doSubmitAction(Object command)
Template method for submit actions. |
String |
getFormView()
Return the name of the view that should be used for form display. |
String |
getSuccessView()
Return the name of the view that should be shown on successful submit. |
protected boolean |
isFormChangeRequest(HttpServletRequest request)
Determine whether the given request is a form change request. |
protected ModelAndView |
onSubmit(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
Submit callback with all parameters. |
protected ModelAndView |
onSubmit(Object command)
Simplest onSubmit version. |
protected ModelAndView |
onSubmit(Object command,
BindException errors)
Simpler onSubmit version. |
protected ModelAndView |
processFormSubmission(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
This implementation calls showForm in case of errors, and delegates to onSubmit's full version else. |
protected Map |
referenceData(HttpServletRequest request)
Create a reference data map for the given request. |
protected Map |
referenceData(HttpServletRequest request,
Object command,
Errors errors)
Create a reference data map for the given request and command, consisting of bean name/bean instance pairs as expected by ModelAndView. |
void |
setFormView(String formView)
Set the name of the view that should be used for form display. |
void |
setSuccessView(String successView)
Set the name of the view that should be shown on successful submit. |
protected ModelAndView |
showForm(HttpServletRequest request,
HttpServletResponse response,
BindException errors)
This implementation shows the configured form view. |
protected boolean |
suppressValidation(HttpServletRequest request)
This implementation delegates to isFormChangeRequest: A form change request changes the appearance of the form and should not get validated but just show the new form. |
Methods inherited from class org.springframework.web.servlet.mvc.AbstractFormController |
formBackingObject, getCommand, getErrorsForNewForm, getFormSessionAttributeName, getFormSessionAttributeName, handleInvalidSubmit, handleRequestInternal, isBindOnNewForm, isFormSubmission, isSessionForm, setBindOnNewForm, setSessionForm, showForm, showForm, showNewForm |
Methods inherited from class org.springframework.web.servlet.mvc.BaseCommandController |
bindAndValidate, checkCommand, createBinder, createCommand, getCommandClass, getCommandName, getMessageCodesResolver, getValidator, getValidators, initApplicationContext, initBinder, isValidateOnBinding, onBind, onBind, onBindAndValidate, setCommandClass, setCommandName, setMessageCodesResolver, setValidateOnBinding, setValidator, setValidators |
Methods inherited from class org.springframework.web.servlet.mvc.AbstractController |
handleRequest, isSynchronizeOnSession, setSynchronizeOnSession |
Methods inherited from class org.springframework.web.servlet.support.WebContentGenerator |
applyCacheSeconds, applyCacheSeconds, cacheForSeconds, cacheForSeconds, checkAndPrepare, checkAndPrepare, getCacheSeconds, getSupportedMethods, isRequireSession, isUseCacheControlHeader, isUseExpiresHeader, preventCaching, setCacheSeconds, setRequireSession, setSupportedMethods, setUseCacheControlHeader, setUseExpiresHeader |
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport |
getServletContext, getTempDir, getWebApplicationContext, isContextRequired |
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport |
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public SimpleFormController()
Subclasses should set the following properties, either in the constructor
or via a BeanFactory: commandName, commandClass, sessionForm, formView,
successView. Note that commandClass doesn't need to be set when overriding
formBackingObject
, as this determines the class anyway.
BaseCommandController.setCommandClass(java.lang.Class)
,
BaseCommandController.setCommandName(java.lang.String)
,
AbstractFormController.setSessionForm(boolean)
,
setFormView(java.lang.String)
,
setSuccessView(java.lang.String)
,
AbstractFormController.formBackingObject(javax.servlet.http.HttpServletRequest)
Method Detail |
public final void setFormView(String formView)
public final String getFormView()
public final void setSuccessView(String successView)
public final String getSuccessView()
protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception
Default implementation delegates to referenceData(request). Subclasses can override this to set reference data used in the view.
referenceData
in class AbstractFormController
request
- current HTTP requestcommand
- form object with request parameters bound onto iterrors
- validation errors holder
Exception
- in case of invalid state or argumentsModelAndView
protected Map referenceData(HttpServletRequest request) throws Exception
Default implementation returns null. Subclasses can override this to set reference data used in the view.
request
- current HTTP request
Exception
- in case of invalid state or argumentsreferenceData(HttpServletRequest, Object, Errors)
,
ModelAndView
protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors) throws Exception
showForm
in class AbstractFormController
request
- current HTTP requestresponse
- current HTTP responseerrors
- validation errors holder
Exception
- in case of invalid state or argumentssetFormView(java.lang.String)
protected final boolean suppressValidation(HttpServletRequest request)
suppressValidation
in class BaseCommandController
request
- current HTTP request
isFormChangeRequest(javax.servlet.http.HttpServletRequest)
protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception
This can only be overridden to check for an action that should be executed without respect to binding errors, like a cancel action. To just handle successful submissions without binding errors, override one of the onSubmit methods.
processFormSubmission
in class AbstractFormController
request
- current servlet requestresponse
- current servlet responsecommand
- form object with request parameters bound onto iterrors
- holder without errors (subclass can add errors if it wants to)
Exception
- in case of errorsshowForm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.validation.BindException)
,
onSubmit(HttpServletRequest, HttpServletResponse, Object, BindException)
protected boolean isFormChangeRequest(HttpServletRequest request)
Gets called by suppressValidation and processFormSubmission. Consequently, this single method determines to suppress validation and to show the form view in any case.
request
- current HTTP request
suppressValidation(javax.servlet.http.HttpServletRequest)
,
processFormSubmission(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception
Default implementation delegates to onSubmit(Object, BindException). For simply performing a submit action and rendering the specified success view, consider implementing doSubmitAction rather than an onSubmit version.
Subclasses can override this to provide custom submission handling like storing the object to the database. Implementations can also perform custom validation and call showForm to return to the form. Do not implement multiple onSubmit methods: In that case, just this method will be called by the controller.
Call errors.getModel()
to populate the ModelAndView model
with the command and the Errors instance, under the specified command name,
as expected by the "spring:bind" tag.
request
- current servlet requestresponse
- current servlet responsecommand
- form object with request parameters bound onto iterrors
- Errors instance without errors (subclass can add errors if it wants to)
Exception
- in case of errorsonSubmit(Object, BindException)
,
doSubmitAction(java.lang.Object)
,
showForm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.validation.BindException)
,
Errors
,
BindException.getModel()
protected ModelAndView onSubmit(Object command, BindException errors) throws Exception
Default implementation calls onSubmit(command), using the returned ModelAndView if actually implemented in a subclass. Else, the default behavior is applied: rendering the success view with the command and Errors instance as model.
Subclasses can override this to provide custom submission handling that does not need request and response.
Call errors.getModel()
to populate the ModelAndView model
with the command and the Errors instance, under the specified command name,
as expected by the "spring:bind" tag.
command
- form object with request parameters bound onto iterrors
- Errors instance without errors
Exception
- in case of errorsonSubmit(HttpServletRequest, HttpServletResponse, Object, BindException)
,
onSubmit(Object)
,
setSuccessView(java.lang.String)
,
Errors
,
BindException.getModel()
protected ModelAndView onSubmit(Object command) throws Exception
This implementation calls doSubmitAction
and returns null
as ModelAndView, making the calling onSubmit method perform its default
rendering of the success view.
Subclasses can override this to provide custom submission handling
that just depends on the command object. It's preferable to use either
onSubmit(command, errors)
or doSubmitAction(command)
,
though: Use the former when you want to build your own ModelAndView; use the
latter when you want to perform an action and forward to the successView.
command
- form object with request parameters bound onto it
Exception
- in case of errorsonSubmit(Object, BindException)
,
doSubmitAction(java.lang.Object)
,
setSuccessView(java.lang.String)
protected void doSubmitAction(Object command) throws Exception
This is the preferred submit callback to implement if you want to perform an action (like storing changes to the database) and then render the success view with the command and Errors instance as model. You don't need to care about the success ModelAndView here.
command
- form object with request parameters bound onto it
Exception
- in case of errorsonSubmit(Object)
,
setSuccessView(java.lang.String)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |