Define controller in Spring MVC

In the previous tutorial, you learned about the components of a controller in Spring MVC, in this tutorial, we will learn together how to define a controller in Spring MVC.

Continue with the project which I created in the previous tutorial, as you can see, a class defined with the @Controller annotation will be considered a controller in Spring MVC. The object of this class is used to process requests to a certain page defined in this class.

The example of the HomeController class in our project has been defined with the @Controller annotation.

The name of this class you can put whatever name does not matter, it is important to have the @Controller annotation declared at the beginning of the class definition.

For example, here we rename the class to ExampleController:

when running, the result is the same.

Add Comment