Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Work flow of simpleFormcontroller in spring MVC 3.0

I have seen many examples on how simpleFormcontroller works.

But still I have some confusion.

I want to know when formBackingObject(), referenceData(), onSubmit() methods invoked?

I dont know exact working flow of these methods?

Can anyone explain me?

like image 433
khyati Avatar asked Oct 11 '12 06:10

khyati


People also ask

What is SimpleFormController?

SimpleFormController is a class that implements form processing in Spring applications that is deprecated as of Spring 3.

Which HTTP methods are used in form viewing and submission stages of SimpleFormController?

during the http get request the formview will be rendered. when the form is submitted (during the http post request) the onsubmit() method of the usercontroller class will be called, on successful execution of the method the successview will be rendered.

What is the default name for form backing object in SimpleFormController?

The default implementation calls onSubmit(Object) , using the returned ModelAndView if actually implemented in a subclass. Else, the default behavior will apply: rendering the success view with the command and Errors instance as model.


1 Answers

Workflow is as follows and it is controlled by AbstractFormController class-

  1. The controller receives a request for a new form (typically a GET).
  2. 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).
  3. 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.
  4. Only if 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.
  5. 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.
  6. 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).
  7. Model gets exposed and view gets rendered, to let the user fill in the form.
  8. The controller receives a form submission (typically a POST). To use a different way of detecting a form submission, override the isFormSubmission method.
  9. 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 to handleInvalidSubmit which - by default - tries to create a new form object and resubmit the form.
  10. The ServletRequestDataBinder gets applied to populate the form object with current request parameters.
  11. 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).
  12. If 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 object.
  13. 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).
  14. Call processFormSubmission() to process the submission, with or without binding errors. This method has to be implemented in subclasses.

Source

For more details and diagrammatic representation to understand the flow you can refer to below link -

like image 86
Aniket Thakur Avatar answered Sep 20 '22 11:09

Aniket Thakur