Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. This Developer Center contains the guides for Commerce 6.13.0 through 7.4.1.Visit new site

This version of Elastic Path Commerce is no longer supported or maintained. To upgrade to the latest version, contact your Elastic Path representative.

Spring MVC web framework

Spring MVC web framework

Spring MVC is a web application framework that performs a similar role to that of Apache Struts. The purpose of Spring MVC is to separate view layer logic from the underlying domain objects and services to maintain loose coupling and application maintainability.

Model View Controller (MVC)

The MVC pattern is used to achieve the separation between view and domain. In the Spring MVC layer, the "Model" is the domain objects and services in lower layers of the system. The "View" is the Velocity templates that render information about the model and collect user input. The "Controller" component represents a set of Java classes called controllers that manage the interaction between the View and the Model. At a high level, the intent of the Controllers is to prevent coupling between the domain (and services) and the Velocity that is used to present them.

Spring MVC Controllers

The Spring MVC Controllers typically manage a single page request and serve one or more of the following roles in displaying the page.

  • Specify the Velocity template that will be displayed to satisfy the request.
  • Obtain and optionally prepare domain objects for display by the Velocity template.
  • Declare a validator to validate field input.
  • Invoke methods on domain objects or services as required to satisfy the request.
  • Return an appropriate response after performing the required task.

Spring MVC Controllers are declared and configured in the url-mapping.xml file.

  • This file declares and configures the controller classes as beans.
  • The URL mapping section at the top of the file specifies which controller should be invoked for a given URL.

    Spring MVC Controllers should contain only a small amount of logic to initiate operations in lower layers. Work flow logic should appear in services, not in Spring Controllers.

  • The problem with implementing workflow logic in controllers is that only Spring MVC has access to it - Web services and DWR will not be able to reuse the logic.
  • Therefore, even when it doesn't look like there's much need for a service layer call (ie. only a few methods will be executed in the service layer method) logic should still be implemented in the service layer.

Elastic Path Controller hierarchy

Controllers in Elastic Path will typically inherit from one of the following base classes. The base class to extend depends on the purpose of the controller.

  • SimplePageController - Use this controller to load a static page that does not invoke any service layer methods such as a "Return Policy" page. There is no need to implement a new controller class when using SimplePageController, you can just use Spring configuration in url-mapping.xml to create an instance of SimplePageController and specify the Velocity template to be displayed.
  • AbstractEpController - Extend this controller when your page does not use a form, but you want to perform an action on the service layer or a domain object. For example, a page that changes the user's locale might read a local parameter from the request and change the locale in the user's profile. When extending AbstractEpController, override handleRequestInternal() to implement the logic that is performed when a user visits the page.
  • AbstractEpFormController - Extend this controller to create pages with HTML forms. For example, consider a "forgotten password" page that requests the user's email address and then emails them a new password. When using AbstractEpFormController, a "form-backing" object is specified as the "command" property in url-mapping.xml. This form-backing object will receive the form input specified by the user. Override onSubmit() to perform the action that occurs when the user submits the form. If your form-backing object requires further preparation before display, you can perform this initialization by overriding formBackingObject() and returning a prepared object.
    Tip: Specify a Validator

    If your form input requires validation, specify a validator in the Controller's Spring configuration block. Validation for new form-backing objects can be specified in validation.xml.

For more information, see the Spring framework documentation site.