Overview about Controller in Spring MVC

In the MVC model, the controller receives the request from the user, processes the request, builds the data for the view (model), and selects the view to return the result to the user. In this tutorial, we will look at an overview about the controller in Spring MVC and its components.

Create project

I will create a new Spring MVC project for example, if you guys do not know how to create it, please see my tutorial here.

My project is structured as follows:

To run this project, you can refer to it here.


Have you wondered how when we request to the home page, our web application can return such results? The answer is thanks to the controller in Spring MVC.

In the project I just created, the HomeController class with the following content:

It is a controller in Spring MVC. The task of this class is to receive the request from the user to the home page, process the request and return the result to the user.

Components of the controller in Spring MVC

Looking at the HomeController class in the project I just created above, you can see that a controller in Spring MVC has the following components:

  • An @Controller annotation is declared along with the definition of the HomeController class.
  • An @RequestMapping annotation is declared along with the definition of the home() method. In this annotation, we have the value attribute to define the request URL and the method attribute to define the HTTP request method.
  • The home() method in the HomeController class is returning a “home” string, which defines the view name that will be used to display the request result to the user.
  • The home() method also constructs a model variable that stores the data needed for the “home” view to use.
  • The home() method is also using the locale variable of the Locale object to get the date and time data of the server running our website.

Here are the basic components of a controller in Spring MVC.

In fact, Spring MVC provides us with another way to define a controller, which is using the org.springframework.web.servlet.mvc.Controller interface, but here I show you the common way, easiest to use and understandable: use the org.springframework.stereotype.Controller annotation.

Add Comment