Sunday, December 16, 2012

Work Flow in Spring SimpleFormController


Here is the sequence of method execution in org.springframework.web.servlet.mvc.SimpleFormController

GET received :

1.  protected Object formBackingObject(HttpServletRequest request) throws Exception
can be overridden to retrieve object from DB or setting default values to object.
2.  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
allows to register custom editor
3. if bindNewForm == true then  ServletRequestDataBinder is used for binding
4. protected abstract ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors) throws Exception
prepares form model and view including reference and errors. for reference data it calls
protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception which creates reference data map for request.
view is rendered.

POST received :

1. if sessionForm == false calls formBackingObject(…) mehotd
else gets object from session
2. ServletRequestDataBinder is used for binding
3. protected void onBind(HttpServletRequest request, Object command, BindException errors) throws Exception
used for custom processing after binding and before validation.
4. if validateOnBinding == true then registered validator is invoked. validator registers errors (if any).
5. protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) throws Exception
used for custom processing after binding and validation.
6. protected ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors) throws Exception
if error occurred then shows the form again using showForm(…) method
else calls
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
Object command, BindException errors) throws Exception
this method can be simply used to lead the user to a success page. this can also be overriden 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.
References:

No comments:

Post a Comment