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.
Result:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.huongdanjava.springmvc; import java.text.DateFormat; import java.util.Date; import java.util.Locale; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Handles requests for the application home page. */ @Controller public class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController.class); /** * Simply selects the home view to render by returning its name. */ @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Locale locale, Model model) { logger.info("Welcome home! The client locale is {}.", locale); Date date = new Date(); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); String formattedDate = dateFormat.format(date); model.addAttribute("serverTime", formattedDate); return "home"; } } |
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.