org.springframework.web.servlet.mvc
@Deprecated public abstract class AbstractFormController extends BaseCommandController
Form controller that auto-populates a form bean from the request.
This, either using a new bean instance per request, or using the same bean
when the sessionForm
property has been set to true
.
This class is the base class for both framework subclasses such as
SimpleFormController
and AbstractWizardFormController
and custom form controllers that you may provide yourself.
A form-input view and an after-submission view have to be provided
programmatically. To provide those views using configuration properties,
use the SimpleFormController
.
Subclasses need to override showForm
to prepare the form view,
and processFormSubmission
to handle submit requests. For the latter,
binding errors like type mismatches will be reported via the given "errors" holder.
For additional custom form validation, a validator (property inherited from
BaseCommandController) can be used, reporting via the same "errors" instance.
Comparing this Controller to the Struts notion of the Action
shows us that with Spring, you can use any ordinary JavaBeans or database-
backed JavaBeans without having to implement a framework-specific class
(like Struts' ActionForm
). More complex properties of JavaBeans
(Dates, Locales, but also your own application-specific or compound types)
can be represented and submitted to the controller, by using the notion of
a java.beans.PropertyEditor
. For more information on that
subject, see the workflow of this controller and the explanation of the
BaseCommandController
.
Workflow
(and that defined by superclass):
formBackingObject()
which by default,
returns an instance of the commandClass that has been configured
(see the properties the superclass exposes), but can also be overridden
to e.g. retrieve an object from the database (that needs to be modified
using the form).initBinder()
which allows you to register
custom editors for certain fields (often properties of non-primitive
or non-String types) of the command class. This will render appropriate
Strings for those property values, e.g. locale-specific date strings.bindOnNewForm
is set to true
, then
ServletRequestDataBinder
gets applied to populate the new form object with initial request parameters and the
onBindOnNewForm(HttpServletRequest, Object, BindException)
callback method is
called. Note: any defined Validators are not applied at this point, to allow
partial binding. However be aware that any Binder customizations applied via
initBinder() (such as
DataBinder.setRequiredFields(String[])
will
still apply. As such, if using bindOnNewForm=true and initBinder() customizations are
used to validate fields instead of using Validators, in the case that only some fields
will be populated for the new form, there will potentially be some bind errors for
missing fields in the errors object. Any view (JSP, etc.) that displays binder errors
needs to be intelligent and for this case take into account whether it is displaying the
initial form view or subsequent post results, skipping error display for the former.showForm()
to return a View that should be rendered (typically the view that renders
the form). This method has to be implemented in subclasses.referenceData()
,
which you can implement to provide any relevant reference data you might need
when editing a form (e.g. a List of Locale objects you're going to let the
user select one from).isFormSubmission
method.
sessionForm
is not set, formBackingObject()
is called to retrieve a form object. Otherwise, the controller tries to
find the command object which is already bound in the session. If it cannot
find the object, it does a call to handleInvalidSubmit
which - by default - tries to create a new form object and resubmit the form.ServletRequestDataBinder
gets applied to populate the form object with current request parameters.
onBind(HttpServletRequest, Object, Errors)
which allows
you to do custom processing after binding but before validation (e.g. to manually
bind request parameters to bean properties, to be seen by the Validator).validateOnBinding
is set, a registered Validator will be invoked.
The Validator will check the form object properties, and register corresponding
errors via the given Errors
onBindAndValidate()
which allows you
to do custom processing after binding and validation (e.g. to manually
bind request parameters, and to validate them outside a Validator).processFormSubmission()
to process the submission, with
or without binding errors. This method has to be implemented in subclasses.In session form mode, a submission without an existing form object in the
session is considered invalid, like in case of a resubmit/reload by the browser.
The handleInvalidSubmit
method is invoked then,
by default trying to resubmit. It can be overridden in subclasses to show
corresponding messages or to redirect to a new form, in order to avoid duplicate
submissions. The form object in the session can be considered a transaction
token in that case.
Note that views should never retrieve form beans from the session but always from the request, as prepared by the form controller. Remember that some view technologies like Velocity cannot even access a HTTP session.
Exposed configuration properties
(and those defined by superclass):
name | default | description |
bindOnNewForm | false | Indicates whether to bind servlet request parameters when creating a new form. Otherwise, the parameters will only be bound on form submission attempts. |
sessionForm | false | Indicates whether the form object should be kept in the session when a user asks for a new form. This allows you e.g. to retrieve an object from the database, let the user edit it, and then persist it again. Otherwise, a new command object will be created for each request (even when showing the form again after validation errors). |
showForm(HttpServletRequest, HttpServletResponse, BindException)
,
processFormSubmission(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
,
SimpleFormController
,
AbstractWizardFormController
DEFAULT_COMMAND_NAME
Constructor and Description |
---|
AbstractFormController()
Deprecated.
Create a new AbstractFormController.
|
Modifier and Type | Method and Description |
---|---|
protected Object |
currentFormObject(javax.servlet.http.HttpServletRequest request,
Object sessionFormObject)
Deprecated.
Return the current form object to use for binding and further processing,
based on the passed-in form object as found in the HttpSession.
|
protected Object |
formBackingObject(javax.servlet.http.HttpServletRequest request)
Deprecated.
Retrieve a backing object for the current form from the given request.
|
protected Object |
getCommand(javax.servlet.http.HttpServletRequest request)
Deprecated.
Return the form object for the given request.
|
protected org.springframework.validation.BindException |
getErrorsForNewForm(javax.servlet.http.HttpServletRequest request)
Deprecated.
Create a BindException instance for a new form.
|
protected String |
getFormSessionAttributeName()
Deprecated.
Return the name of the HttpSession attribute that holds the form object
for this form controller.
|
protected String |
getFormSessionAttributeName(javax.servlet.http.HttpServletRequest request)
Deprecated.
Return the name of the HttpSession attribute that holds the form object
for this form controller.
|
protected org.springframework.web.servlet.ModelAndView |
handleInvalidSubmit(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Deprecated.
Handle an invalid submit request, e.g.
|
protected org.springframework.web.servlet.ModelAndView |
handleRequestInternal(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Deprecated.
Handles two cases: form submissions and showing a new form.
|
boolean |
isBindOnNewForm()
Deprecated.
Return
true if request parameters should be bound in case of a new form. |
protected boolean |
isFormSubmission(javax.servlet.http.HttpServletRequest request)
Deprecated.
Determine if the given request represents a form submission.
|
boolean |
isSessionForm()
Deprecated.
Return
true if session form mode is activated. |
protected void |
onBindOnNewForm(javax.servlet.http.HttpServletRequest request,
Object command)
Deprecated.
Callback for custom post-processing in terms of binding for a new form.
|
protected void |
onBindOnNewForm(javax.servlet.http.HttpServletRequest request,
Object command,
org.springframework.validation.BindException errors)
Deprecated.
Callback for custom post-processing in terms of binding for a new form.
|
protected abstract org.springframework.web.servlet.ModelAndView |
processFormSubmission(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
Object command,
org.springframework.validation.BindException errors)
Deprecated.
Process form submission request.
|
protected Map |
referenceData(javax.servlet.http.HttpServletRequest request,
Object command,
org.springframework.validation.Errors errors)
Deprecated.
Create a reference data map for the given request, consisting of
bean name/bean instance pairs as expected by ModelAndView.
|
void |
setBindOnNewForm(boolean bindOnNewForm)
Deprecated.
Set whether request parameters should be bound to the form object
in case of a non-submitting request, that is, a new form.
|
void |
setSessionForm(boolean sessionForm)
Deprecated.
Activate/deactivate session form mode.
|
protected org.springframework.web.servlet.ModelAndView |
showForm(javax.servlet.http.HttpServletRequest request,
org.springframework.validation.BindException errors,
String viewName)
Deprecated.
Prepare model and view for the given form, including reference and errors.
|
protected org.springframework.web.servlet.ModelAndView |
showForm(javax.servlet.http.HttpServletRequest request,
org.springframework.validation.BindException errors,
String viewName,
Map controlModel)
Deprecated.
Prepare model and view for the given form, including reference and errors,
adding a controller-specific control model.
|
protected abstract org.springframework.web.servlet.ModelAndView |
showForm(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
org.springframework.validation.BindException errors)
Deprecated.
Prepare the form model and view, including reference and error data.
|
protected org.springframework.web.servlet.ModelAndView |
showNewForm(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Deprecated.
Show a new form.
|
bindAndValidate, checkCommand, createBinder, createCommand, getBindingErrorProcessor, getCommandClass, getCommandName, getMessageCodesResolver, getPropertyEditorRegistrars, getValidator, getValidators, getWebBindingInitializer, initApplicationContext, initBinder, isValidateOnBinding, onBind, onBind, onBindAndValidate, prepareBinder, setBindingErrorProcessor, setCommandClass, setCommandName, setMessageCodesResolver, setPropertyEditorRegistrar, setPropertyEditorRegistrars, setValidateOnBinding, setValidator, setValidators, setWebBindingInitializer, suppressBinding, suppressValidation, suppressValidation, suppressValidation, useDirectFieldAccess
handleRequest, isSynchronizeOnSession, setSynchronizeOnSession
applyCacheSeconds, applyCacheSeconds, cacheForSeconds, cacheForSeconds, checkAndPrepare, checkAndPrepare, getCacheSeconds, getSupportedMethods, isAlwaysMustRevalidate, isRequireSession, isUseCacheControlHeader, isUseCacheControlNoStore, isUseExpiresHeader, preventCaching, setAlwaysMustRevalidate, setCacheSeconds, setRequireSession, setSupportedMethods, setUseCacheControlHeader, setUseCacheControlNoStore, setUseExpiresHeader
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
public AbstractFormController()
Subclasses should set the following properties, either in the constructor
or via a BeanFactory: commandName, commandClass, bindOnNewForm, sessionForm.
Note that "commandClass" doesn't need to be set when overriding
formBackingObject(javax.servlet.http.HttpServletRequest)
, since the latter determines the class anyway.
"cacheSeconds" is by default set to 0 (-> no caching for all form controllers).
public final void setBindOnNewForm(boolean bindOnNewForm)
public final boolean isBindOnNewForm()
true
if request parameters should be bound in case of a new form.public final void setSessionForm(boolean sessionForm)
This is necessary for either wizard-style controllers that populate a single form object from multiple pages, or forms that populate a persistent object that needs to be identical to allow for tracking changes.
Please note that the AbstractFormController
class (and all
subclasses of it unless stated to the contrary) do not support
the notion of a conversation. This is important in the context of this
property, because it means that there is only one form per session:
this means that if session form mode is activated and a user opens up
say two tabs in their browser and attempts to edit two distinct objects
using the same form, then the shared session state can potentially
(and most probably will) be overwritten by the last tab to be opened,
which can lead to errors when either of the forms in each is finally
submitted.
If you need to have per-form, per-session state management (that is, stateful web conversations), the recommendation is to use Spring WebFlow, which has full support for conversations and has a much more flexible usage model overall.
sessionForm
- true
if session form mode is to be activatedpublic final boolean isSessionForm()
true
if session form mode is activated.protected org.springframework.web.servlet.ModelAndView handleRequestInternal(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception
isFormSubmission(javax.servlet.http.HttpServletRequest)
,
always treating requests without existing form session attribute
as new form when using session form mode.handleRequestInternal
in class org.springframework.web.servlet.mvc.AbstractController
Exception
isFormSubmission(javax.servlet.http.HttpServletRequest)
,
showNewForm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,
processFormSubmission(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
protected boolean isFormSubmission(javax.servlet.http.HttpServletRequest request)
The default implementation treats a POST request as form submission. Note: If the form session attribute doesn't exist when using session form mode, the request is always treated as new form by handleRequestInternal.
Subclasses can override this to use a custom strategy, e.g. a specific request parameter (assumably a hidden field or submit button name).
request
- current HTTP requestprotected String getFormSessionAttributeName(javax.servlet.http.HttpServletRequest request)
The default implementation delegates to the getFormSessionAttributeName()
variant without arguments.
request
- current HTTP requestnull
if not in session form modegetFormSessionAttributeName(javax.servlet.http.HttpServletRequest)
,
HttpSession.getAttribute(java.lang.String)
protected String getFormSessionAttributeName()
Default is an internal name, of no relevance to applications, as the form session attribute is not usually accessed directly. Can be overridden to use an application-specific attribute name, which allows other code to access the session attribute directly.
HttpSession.getAttribute(java.lang.String)
protected final org.springframework.web.servlet.ModelAndView showNewForm(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception
request
- current HTTP requestresponse
- current HTTP responseException
- in case of an invalid new form objectgetErrorsForNewForm(javax.servlet.http.HttpServletRequest)
protected final org.springframework.validation.BindException getErrorsForNewForm(javax.servlet.http.HttpServletRequest request) throws Exception
showNewForm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
.
Can be used directly when intending to show a new form but with
special errors registered on it (for example, on invalid submit).
Usually, the resulting BindException will be passed to
showForm(HttpServletRequest, HttpServletResponse, BindException)
,
after registering the errors on it.
request
- current HTTP requestException
- in case of an invalid new form objectshowNewForm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,
showForm(HttpServletRequest, HttpServletResponse, BindException)
,
handleInvalidSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected void onBindOnNewForm(javax.servlet.http.HttpServletRequest request, Object command, org.springframework.validation.BindException errors) throws Exception
bindOnNewForm
is true
.
The default implementation delegates to onBindOnNewForm(request, command)
.
request
- current HTTP requestcommand
- the command object to perform further binding onerrors
- validation errors holder, allowing for additional
custom registration of binding errorsException
- in case of invalid state or argumentsonBindOnNewForm(javax.servlet.http.HttpServletRequest, Object)
,
setBindOnNewForm(boolean)
protected void onBindOnNewForm(javax.servlet.http.HttpServletRequest request, Object command) throws Exception
Called by the default implementation of the
onBindOnNewForm(HttpServletRequest, Object, BindException)
variant
with all parameters, after standard binding when displaying the form view.
Only called if bindOnNewForm
is set to true
.
The default implementation is empty.
request
- current HTTP requestcommand
- the command object to perform further binding onException
- in case of invalid state or argumentsonBindOnNewForm(HttpServletRequest, Object, BindException)
,
setBindOnNewForm(boolean)
protected final Object getCommand(javax.servlet.http.HttpServletRequest request) throws Exception
Calls formBackingObject(javax.servlet.http.HttpServletRequest)
if not in session form mode.
Else, retrieves the form object from the session. Note that the form object
gets removed from the session, but it will be re-added when showing the
form for resubmission.
getCommand
in class BaseCommandController
request
- current HTTP requestorg.springframework.web.HttpSessionRequiredException
- if a session was expected but no active session (or session form object) foundException
- in case of invalid state or argumentsformBackingObject(javax.servlet.http.HttpServletRequest)
protected Object formBackingObject(javax.servlet.http.HttpServletRequest request) throws Exception
The properties of the form object will correspond to the form field values in your form view. This object will be exposed in the model under the specified command name, to be accessed under that name in the view: for example, with a "spring:bind" tag. The default command name is "command".
Note that you need to activate session form mode to reuse the form-backing object across the entire form workflow. Else, a new instance of the command class will be created for each submission attempt, just using this backing object as template for the initial form.
The default implementation calls BaseCommandController.createCommand()
,
creating a new empty instance of the specified command class.
Subclasses can override this to provide a preinitialized backing object.
request
- current HTTP requestException
- in case of invalid state or argumentsBaseCommandController.setCommandName(java.lang.String)
,
BaseCommandController.setCommandClass(java.lang.Class)
,
BaseCommandController.createCommand()
protected Object currentFormObject(javax.servlet.http.HttpServletRequest request, Object sessionFormObject) throws Exception
The default implementation simply returns the session form object as-is. Subclasses can override this to post-process the session form object, for example reattaching it to a persistence manager.
sessionFormObject
- the form object retrieved from the HttpSessionException
- in case of invalid state or argumentsprotected abstract org.springframework.web.servlet.ModelAndView showForm(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, org.springframework.validation.BindException errors) throws Exception
A typical implementation will call
showForm(request, errors, "myView")
to prepare the form view for a specific view name, returning the
ModelAndView provided there.
For building a custom ModelAndView, 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.
You also need to include the model returned by referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.Errors)
.
Note: If you decide to have a "formView" property specifying the view name, consider using SimpleFormController.
request
- current HTTP requestresponse
- current HTTP responseerrors
- validation errors holdernull
if handled directlyException
- in case of invalid state or argumentsshowForm(HttpServletRequest, BindException, String)
,
Errors
,
BindException.getModel()
,
referenceData(HttpServletRequest, Object, Errors)
,
SimpleFormController.setFormView(java.lang.String)
protected final org.springframework.web.servlet.ModelAndView showForm(javax.servlet.http.HttpServletRequest request, org.springframework.validation.BindException errors, String viewName) throws Exception
In session form mode: Re-puts the form object in the session when returning to the form, as it has been removed by getCommand.
Can be used in subclasses to redirect back to a specific form page.
request
- current HTTP requesterrors
- validation errors holderviewName
- name of the form viewException
- in case of invalid state or argumentsprotected final org.springframework.web.servlet.ModelAndView showForm(javax.servlet.http.HttpServletRequest request, org.springframework.validation.BindException errors, String viewName, Map controlModel) throws Exception
In session form mode: Re-puts the form object in the session when returning to the form, as it has been removed by getCommand.
Can be used in subclasses to redirect back to a specific form page.
request
- current HTTP requesterrors
- validation errors holderviewName
- name of the form viewcontrolModel
- model map containing controller-specific control data
(e.g. current page in wizard-style controllers or special error message)Exception
- in case of invalid state or argumentsprotected Map referenceData(javax.servlet.http.HttpServletRequest request, Object command, org.springframework.validation.Errors errors) throws Exception
The default implementation returns null
.
Subclasses can override this to set reference data used in the view.
request
- current HTTP requestcommand
- form object with request parameters bound onto iterrors
- validation errors holdernull
if noneException
- in case of invalid state or argumentsModelAndView
protected abstract org.springframework.web.servlet.ModelAndView processFormSubmission(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object command, org.springframework.validation.BindException errors) throws Exception
handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
in case of a form submission, with or without binding errors. Implementations
need to proceed properly, typically showing a form view in case of binding
errors or performing a submit action else.
Subclasses can implement this to provide custom submission handling like
triggering a custom action. They can also provide custom validation and call
showForm(HttpServletRequest, HttpServletResponse, BindException)
or proceed with the submission accordingly.
For a success view, 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. For a form view,
simply return the ModelAndView object provided by
showForm(HttpServletRequest, HttpServletResponse, BindException)
.
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)null
Exception
- in case of errorshandleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,
isFormSubmission(javax.servlet.http.HttpServletRequest)
,
showForm(HttpServletRequest, HttpServletResponse, BindException)
,
Errors
,
BindException.getModel()
protected org.springframework.web.servlet.ModelAndView handleInvalidSubmit(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception
The default implementation simply tries to resubmit the form with a new form object. This should also work if the user hit the back button, changed some form data, and resubmitted the form.
Note: To avoid duplicate submissions, you need to override this method.
Either show some "invalid submit" message, or call showNewForm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
for
resetting the form (prepopulating it with the current values if "bindOnNewForm"
is true). In this case, the form object in the session serves as transaction token.
protected ModelAndView handleInvalidSubmit(HttpServletRequest request, HttpServletResponse response) throws Exception { return showNewForm(request, response); }You can also show a new form but with special errors registered on it:
protected ModelAndView handleInvalidSubmit(HttpServletRequest request, HttpServletResponse response) throws Exception { BindException errors = getErrorsForNewForm(request); errors.reject("duplicateFormSubmission", "Duplicate form submission"); return showForm(request, response, errors); }
request
- current HTTP requestresponse
- current HTTP responsenull
if handled directlyException
- in case of errorsshowNewForm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,
getErrorsForNewForm(javax.servlet.http.HttpServletRequest)
,
showForm(HttpServletRequest, HttpServletResponse, BindException)
,
setBindOnNewForm(boolean)
Copyright © 2015 OpenMRS Inc.. All Rights Reserved.