Skip to content

Spring MVC

Estimated time to read: 4 minutes

Spring Boot Starter

The dependency required for Spring MVC is spring-boot-starter-web. Once packages are included within the CLASSPATH, Spring Boot will provide the required dependencies and automatic configuration needed for developing a web application, including;

  • A dispatcher servlet
  • A default error page
  • An embedded servlet container
  • Tomcat is the default
  • WebJARs
  • To manage static dependencies

MVC Design Pattern

MVC or Model-View-Controller, is one of the most common design patterns and is used heavily within web application development.

graph
    a[Model</br> Representation of data in a system]
    b[View </br> Responsible for displaying data]
    c[Controller </br> Directing incoming user requests]

To avoid confusion, whilst MVC is used here. It can also be broken down to include the following sub-components:

Controller Model Service Repository View

flowchart LR

    ZZ[User]
    A[Controller]
    C[Service]
    D[Repository]
    E[View]
    F[Database]
    GG[Model]

    subgraph Model[ ]
    C <--> D
    D <--> F

    end

    subgraph two [ ]
    A --> E
    A <--> GG

    end


    GG <--> C

    ZZ --> A


    E --> ZZ

MVC decouples major components to promote faster development cycles and works well for developing large scale web applications that need to be supported by large teams of developers.

graph
    a(User)
    b[Controller]
    c[View]
    d[Model]

    a --> |Activates| b
    a --> |Sees| c
    d --> |Displays on| c

Model

A model, also referred to as an entity or domain model, is the representation of data in a system.

The model is typically stored within a database and is programmatically represented as a Java object.

View

The view layer is responsible for displaying data, or the model. In Java, the view is typically done in JSP or JSF.

Controller

The controller is responsible for directing incoming user requests to the correct resources and sending responses from those resources back to the user.

ThymeLeaf

ThymeLeaf is a substitute for JSP that works as a view layer within MVC based applications. It provides full Spring Framework integration.

ThymeLeaf has a concept of fragments. Fragments allow defining repeatable blocks of code that can be reused in another ThymeLeaf template file. This is useful components that are to be used across web pages.

For example, a navigation bar within a web application could be considered a fragment, as it will be shared across the index, main application and other pages.

MVC in Action

Using the course scenario, MVC is implemented as such:

Trackzilla Model

Within Trackzilla, Models have been implemented as per the example on 103-Data TAVISTOFIX

Trackzilla Controller

Within Trackzilla, there is a single Controller within the controller layer. This single controller is responsible for handling all incoming HTTP requests. All HTTP requests are, in this instance, mapped to the service layer.

```java title="TzaController.java // ...

@Controller // The @Controller annotation is used to mark classes as a Spring MVC controller. //Spring will consider this class when handling incoming web requests. public class TzaController { private ApplicationService applicationService; private TicketService ticketService; private ReleaseService releaseService;

@Autowired
public void setApplicationService(ApplicationService applicationService) {
    this.applicationService = applicationService;
}

// ...

@GetMapping("/applications") // The @GetMapping annotation provides routing information to Spring
public String retrieveApplications(Model model){
    model.addAttribute("applications", applicationService.listApplications());
    return "applications";
}

// ... } ```

View

The view layer within Trackzilla is implemented using ThymeLeaf. For more information, see ThymeLeaf and refer to the GitLab repository for Trackzilla for an example implementation.

Trackzilla Service

The service layer within Trackzilla is implemented within the implementation of each service interface. The service layer typically contains the business logic and retrieves data using the repository layer. Services act as a way of encapsulating data and work between the controller and model layers. The service interface states the requirements that must be fulfilled by the service implementation for the relevant model. java title="ApplicationService.java" package io.entityfour.service; import io.entityfour.entity.Application; public interface ApplicationService { Iterable<Application> listApplications(); }

ApplicationServiceImpl.java
package io.entityfour.service;

import io.entityfour.entity.Application;
import io.entityfour.repository.ApplicationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationRepository applicationRepository;

    @Override
    public Iterable<Application> listApplications() {
        return applicationRepository.findAll();
    }

}

Packaging and Deployment

Spring Boot is flexible and can deploy web applications to a variety of platforms.

Spring Boot based applications can be packaged as a traditional web application in a .WAR file and deployed to an external web server, or as an executable JAR file with an embedded server.

Standalone applications are packaged. This ensures that all required dependencies for operation are included along with the applications' code and main method. A Tomcat servlet container is also included within a packaged application, when using this approach.

Spring Boot Maven Plugin

The Spring Boot Maven plugin provides features for packaging and deployment, it can:

  • Repackage JAR and WAR files to make the executable
  • As well as collecting all required dependencies from the classpath
  • Runs Spring Boot application
  • Searches for the public static void main method and flags it as runnable
  • Provides a built-in dependency resolver
  • Manages the lifecycle of the application
  • Also creates metrics for use with the Actuator