Before we can start building our frontend we need to tell our Spring Boot application how to handle the incoming requests and what html page to display. This is done using Spring MVC which we added when we picked the spring-boot-starter-web dependency in the Spring Initializer. This dependency adds a built in tomcat web server that is configured with a set of defaults and sets up the application to scan for special classes that have been annotated with @Controller
When a class is annotated with @Controller Spring MVC knows that it will contain methods that have been annotated in such a way that the method can be mapped to a specific request.
I’ve created a new package in my source to hold all my controllers called, you guessed it, controllers. In here I have created a new java class called HomeController and I have annotated it the @Controller annotation above the class name. I then added a single method to the class and I have annotated that with @RequestMapping(“/”) which means whenever a request comes in that matches the root of the site you should return the string ‘home’.
This returned string is intercepted by Spring MVC and is passed to the configured template renderer which in our case is Thymeleaf. Thymeleaf will then look in the resources/templates folder for a html file called home.html and will send that to the web browser.
If we were to run the application right now we would get an error as we don’t have that html file yet.
The other thing to notice is that the RequestMapping method accepts a ModelMap called model. This is where we can add data to the model so that when the Thymeleaf processor is rendering the page it can take the data from the model and slot it in to place, This is very like how an XPage application can put values in to the rendered html. Right now I’m not putting anything in to the model so I’ll come back to that later.