Command Controllers

Command Class

Command Class is a normal Java Bean class, it can be instantiate by Spring WEB MVC framework inorder to store form data which is submitted by the respective Client along with request.

The main intention to store form data in Command Class objects is

  1. To make available form data to Business Logic to use.
  2. To transfer form data from Controller Layer to View Layer like DTO[Fata Transfer Object]
  3. To perform Server side Data Validatins before using data in Business logic.

To use Command classes in Spring WEB MVC applications, we have to use the following conventions.

  1. Command class must be a normal JAVA Bean class.
  2. Command class must be a public, non-abstract and non-final class.
  3. Command class must have the properties whose names must be same as the form properties name.
  4. In Command class, we must provide a separate setXXX() method and getXXX() method for each and every property.
  5. In Command classes we have to declare all properties as private and methods as public.
  6. If we want to provide constructor in Command class then we can provide constructor, but, it must be public and 0-argument constructor.
  7. It is suggestible to implement java.io.Serializable interface.

E.g

public class Employee implements Serializable {
   private String empName;
   private String empId;
   private String empAddress;
   private double salary;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public String getEmpAddress() {
      return empAddress;
   }

   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;
   }

   public double getSalary() {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }
}

Note

In Spring WEB MVC applications, Framework will create a seperate Command object for each and every request which is generated from form.

If we want to use Command classes in Spring WEB MVC Applications then we have to use Command Controller Classes.

Spring has provided the following Command Controller classes to use Command classes.

  1. BaseCommandController
  2. AbstractCommandController
  3. AbstractFormController
  4. SimpleFormController
  5. AbstractWizardFormController
1. BaseCommandController

It is an abstract class provided by SpringFramework as “org. springframework.web.servlet. mvc.BaseCommandController”.

It is a base class for all Command Controller classes which are wishing to populate request parameters data in Command class objects.

Note

It is a deprecated abstract class , it was not existed in Spring4.x version.

2. AbstractCommandController

It is an abstract class provided by Spring Framework as “org. springframework.web.servlet.mvc.AbstractCommandController” with the following methods.

protected abstract ModelAndView  handle(HttpServletRequest request, HttpServletResponse response,  java.lang.Object command, BindException errors)
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)

This controller class will populate form data in Command class objects automatically by creating Command class object for each and every request.

In general, we will use Controller class when we have form submission from client and when we don’t want to perform Data Validations at Server side.

If we want to use this CommandController class then we have to declare an user defined class and it must be extended from “AbstractCommandController” abstract class and we must override handle(–) method.

Note

In User defined Controller class , we have to provide 0-arg constructor, where we have to set command class by using the following method.

public void setCommandClass(Class class)

or

set the following properties in Controller class configuration in Spring configuration file.

  • commandName: any name
  • commandClass: Fully qualified name of the Command class.

E.g

<bean name="/login" class="com.ashok.spring.mvc.controller.LoginController">
   <property name="commandName" value="user"/>
   <property name="commandClass" value="com.ashok.spring.mvc.command.User"/>
</bean>

Note

AbstractCommandController class is deprecated and it was removed from Spring4.x version, so that, to execute the below application we have to use either Spring2.5 version or at least Spring3.x version.

3. AbstractFormController

It is an abstract class provided by Spring Framework as “org. springframework.web.servlet.mvc.AbstractFormController” with the following methods.

  • protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command, BindException exception) throws Exception
  • protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException exception) throws Exception
  • Where processFormSubmission() method will handle the request , It will include the application logic which we want to execute after submmitting form, It will be executed when we submit POST rquest from the user form.
  • Where we have to override showForm() method to prepare view name and it will be executed when we submitted GET request from client.

This controller class will populate form data in Command class objects automatically either by creating Command class object for each and every request or it will reuse the command class object from session scope if we set “sessionForm” property value true. In general, we will use Controller class when we have form submission from client and when we don’t want to perform Data Validations at Server side.

If we want to use this CommandController class then we have to declare an user defined class and it must be extended from “AbstractCommandController” abstract class and we must override processFormSubmission(–) method and showForm() method.

Note

In User defined Controller class , we have to provide 0-arg constructor, where we have to set command class by using the following method.

public void setCommandClass(Class class)

or

We have to set the following properties in Controller bean configurations in spring configuration file.

  • sessionForm –> true
  • commandName –> any name
  • commandClass –> Fully qualified name of the command class.

Note

AbstractFormController class is deprecated in Spring3.x version, to use this Controller class we have to use either Spring2.5 version atleast Spring3.x version.

4. SimpleFormController

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.

1. formView: it will take form name to get User form.

2. commandName: it will take logical name to the command class.

3. commandClass: It will take fully qualified name of the command class.

4. 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

5. AbstractWizardFormController

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 etc 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.

1. 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.

2. Prepare Controller class by extending AbstractWizardFormController class and override the following methods.

protected ModelAndView processFinish(HttpServletRequest request, HttpServletResponse response, Object command, BindException exception) throws Exception

protected ModelAndView processCancel(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception

Where

processFinish() method will handle Wizard Finish action.
processCancel() method will handle wizard cancel Task.

3. 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.

E.g

<bean name="/reg" class="com.ashok.spring.mvc.controller.UserController">
   <property name="pages">
      <list>
         <value>form1</value>
         <value>form2</value>
         <value>form3</value>
      </list>
   </property>
</beans>

Command Controllers
Scroll to top