Chapter 2. Mastering the MVC Architecture

In this chapter, we will discuss the MVC architecture principles and see how Spring MVC implements those.

We will continue to use the application from the previous chapter and build something more interesting. Our goal is to design a simple page where users can search for tweets corresponding to certain criteria and display them to our users.

To achieve this, we will use the Spring Social Twitter project, which is available at http://projects.spring.io/spring-social-twitter/.

We will see how to make Spring MVC work with a modern template engine, Thymeleaf, and try to understand the inner mechanics of the framework. We will route our users through different views, and finally, we will give a stellar look to our application using WebJars and Materialize (http://materializecss.com).

The MVC architecture

I expect the meaning of the MVC acronym to be familiar to most. It stands for Model View Controller, and it is considered to be a very popular way to build a user interface by decoupling the data and the presentation layers.

The MVC architecture

The MVC pattern became wildly popular after emerging from the world of Smalltalk and landing in the Ruby on Rails framework.

The architectural pattern features three layers:

  • The Model: This consists of various representations of the data your application knows about.
  • The View: This is made up of several representations of the data that will be displayed to your users.
  • The Controller: This is the part of the application that will handle user interactions. It's a bridge between the model and the view.

The idea behind MVC is to decouple the View from the Model. The model must be self-contained and ignorant of the UI. This basically allows the same data to be reused across multiple views. These views are different way to look at the data. Drill down or using different renderers (HTML, PDF) are good illustrations of this principle.

The Controller acts as a mediator between the user and the data. Its role is to control actions available to the end user, as well as routing through the different views of the application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.180.43