The controller receives a request for a new form (typically a GET).
- Call to
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). 默认调用formBackingObject()方法 - Call to
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. 调用initBinder()方法,注册需要的类型转换器 - Only if
bindOnNewForm
is set totrue
, thenServletRequestDataBinder
gets applied to populate the new form object with initial request parameters and theonBindOnNewForm(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 asDataBinder.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. - Call to
showForm()
to return a View that should be rendered (typically the view that renders the form). This method has to be implemented in subclasses. 调用showForm()函数,返回显示页面,在子类中实现。 - The showForm() implementation will call
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). - Model gets exposed and view gets rendered, to let the user fill in the form.
The controller receives a form submission (typically a POST).
To use a different way of detecting a form submission, override the isFormSubmission
method.
- If
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 tohandleInvalidSubmit
which - by default - tries to create a new form object and resubmit the form. - The
ServletRequestDataBinder
gets applied to populate the form object with current request parameters. - Call to
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). - If
validateOnBinding
is set, a registered Validator will be invoked. The Validator will check the form object properties, and register corresponding errors via the givenErrors
object. - Call to
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). - Call
processFormSubmission()
to process the submission, with or without binding errors. This method has to be implemented in subclasses.