Nearby lessons

20 of 35

Spring - Command Class

📌 What You Will Learn
  • Understand Spring - Command Class
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Spring - Command Class step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

Command Class

o spring-expression-4.3.9.RELEASE.jar o spring-jdbc-4.3.9.RELEASE.jar o spring-tx-4.3.9.RELEASE.jar o spring-web-4.3.9.RELEASE.jar o spring-webmvc-4.3.9.RELEASE.jar

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

  • To make available form data to Business Logic to use.
  • To transfer form data from Controller Layer to View Layer like DTO[Fata Transfer Object]
  • 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.

  • Command class must be a normal JAVA Bean class.
  • Command class must be a public, non-abstract and non-final class.
  • Command class must have the properties whose names must be same as the form

properties name.

  • In Command class, we must provide a seperate setXXX() method and getXXX() method for

each and every property.

  • In Command classesm we have to declare all properties as private and methods as

public.

  • If we want to provide constructor in Command class then we can provide constructor,

but, it must be public and 0-argument constructor.

  • It is suggestible to implement java.io.Serializable interface
Example02
Loading editor…

Command Class

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.

  • BaseCommandController
  • AbstractCommandController
  • AbstractFormController
  • SimpleFormController
  • AbstractWizardFormController

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.

AbstractCommandController — index.jsp

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

 protected abstract ModelAndViewhandle(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 dont 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.

EX:

<bean name="/login" class="com.durgasoft.controller.LoginController"> <property name="commandName" value="user"/> <property name="commandClass" value="com.durgasoft.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 versin or atleast Spring3.x version.

EX:

Example05
Loading editor…

AbstractCommandController — loginform.jsp

Example06
Loading editor…

AbstractCommandController — status.jsp

Example07
Loading editor…

AbstractCommandController — LoginController.java

Example08
Loading editor…

AbstractCommandController — User.java

Example09
Loading editor…

AbstractCommandController — ds-servlet.xml

Example10
Loading editor…

AbstractCommandController — web.xml

Example11
Loading editor…

AbstractFormController — index.jsp

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 eithet 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 dont 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.

Example:

Example12
Loading editor…

AbstractFormController — registrationform.jsp

Example13
Loading editor…

AbstractFormController — registrationdetails.jsp

Example14
Loading editor…

AbstractFormController — Student.java

Example15
Loading editor…

AbstractFormController — StudentController.java

Example16
Loading editor…

AbstractFormController — ds-servlet.xml

Example17
Loading editor…
📝 Key Takeaways
  • Key ideas of Spring - Command Class explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.