Nearby lessons
23 of 35Spring - SimpleFormController and Wizard Forms
- Build a single-page form with SimpleFormController, including its success view
- Split one form across several pages with AbstractWizardFormController
- Configure formView, successView and the wizard's page list in ds-servlet.xml
The two form controllers Spring MVC gives you: SimpleFormController for a single-page form and AbstractWizardFormController for a multi-page wizard that carries one command object across several screens. Both examples are complete — every JSP, the controller, the bean and both XML files.
SimpleFormController — index.jsp
This controller class is able to support the Command classes inorder to store form data in Command class objects when we submit user forms.
It was introduced in Spring2.5 version as "org.springframework.web.servlet.mvc.SimpleFormController" and it was deprecated in SPring3.x version and it was removed in spring4.x version. It is best option for Data Validations in Spring WEB MVC Framework, it will render the same page automatically when validation errors are identified and it will forward to successView page when no Validation messages are identified.
If we want to use this controller class then we have to declare an user defined class and it must be extended from SimpleFormController class and we must implement either of the following methods.
protected ModelAndView onSubmit(Object command) throws Exception
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindingExceptin exception) throws Exception
onSubmit(--) method will take the application logic which we want to execute by submitting form from client.
If we want to use this Controller class, we must provide the the following properties in Controller class configuration.
- formView: it will take form name to get User form.
- commandName: it will take logical name to the command class.
- commandClass: It will take fully qualified name of the command class.
- sessionForm: to keep Command Object in session scope inorder to reuse command
object.
In case of SimpleFormController, if we want to provide data to the GUI components like checkboxes, radio buttons, Select boxes,..... in user forms from Controller class then we have to override the following method from SimpleFormController class.
public Map referencedData(HttpServletRequest request)throws Exception
Example:
SimpleFormController — registrationform.jsp
SimpleFormController — status.jsp
SimpleFormController — User.java
SimpleFormController — RegistrationController.java
SimpleFormController — ds-servlet.xml
SimpleFormController — web.xml
AbstractWizardFormController — index.jsp
In general, In web applications , it is required to display more than one page in order to complete a given task.
In web applications, providing sequence of pages in a single task is called as "Wizard".
IN general, in Web applications, we will use multiple pages to enter user details like general details, qualification details, communication details,....in User registration process, to achieve this we have to use "Wizard".
To support for the Wizards in web applications, Spring Web MVC has provided a predefined controller in the form of "org.springframework.web.servlet.mvc.AbstractWizardFormController".
AbstractWizardFormController allows us to carry the same command object through an entire flow of Web pages in Wizard.
If we want to use AbstractWizardController class in Spring web MVC applications then we have to use the following steps.
- Prepare multiple web pages with the buttons where names of the buttons must be
- _finish: Finish the wizard form.
- _cancel: Cancel the wizard form.
- _targetx: Move to the target page, where x is the zero-based page index.
- Prepare Controller class by extending AbstractWizardFormController class and override the following methods.
a)protected ModelAndView processFinish(HttpServletRequest request, HttpServletResponse response, Object command, BindException exception) throws Exception
b)protected ModelAndView processCancel(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception
Where processFinish() method will handle Wizard Finish action. Where processCancel() method will handle izard cancel Task.
- Configure contoller class configuration in Spring configuration file with "pages" property , where "pages" property must be of "list" type, it must include all the pages names in a sequence inorder to open the pages when we click on Next buttons in Wizard.
EX:
<bean name="/reg" class="com.durgasoft.controller.UserController"> <property name="pages"> <list> <value>form1</value> <vale>form2</value> <value>form3</value> </list> </property> </beans>
Example:
AbstractWizardFormController — welcomepage.jsp
AbstractWizardFormController — form1.jsp
AbstractWizardFormController — form2.jsp
AbstractWizardFormController — form3.jsp
AbstractWizardFormController — userdetails.jsp
AbstractWizardFormController — UserController.java
AbstractWizardFormController — User.java
AbstractWizardFormController — ds-servlet.xml
AbstractWizardFormController — web.xml
- SimpleFormController separates showing the form from handling the submission
- A wizard keeps one command object alive across pages, so partial input survives navigation
- Both need their views declared in configuration, not hard-coded in the controller