What is the role of the @Controller annotation in a Thymeleaf application?

Table of Contents

Introduction

In a Spring Boot application that uses Thymeleaf for rendering views, the @Controller annotation plays a key role in handling HTTP requests, processing business logic, and returning views. This annotation marks a class as a Spring MVC controller that is responsible for receiving web requests and returning appropriate model data and views (usually Thymeleaf templates).

Understanding how the @Controller annotation works is essential for integrating Thymeleaf with Spring Boot applications, as it is the central component for controlling the flow of requests and managing the data that is passed to the views.

Role of the @Controller Annotation

1. Defining Controller Classes

The @Controller annotation is used to define a Spring MVC controller class. A controller class handles web requests and is responsible for returning the appropriate response, typically as a view (HTML) rendered with Thymeleaf. This annotation allows Spring to detect and register the class as a controller during the component scan.

Example: Basic Controller

In this example:

  • The @Controller annotation marks the HomeController class as a Spring MVC controller.
  • The home() method is a request handler that processes HTTP GET requests to the root URL ("/").
  • The model.addAttribute() method adds a message attribute to the model, which can be accessed in the Thymeleaf template (home.html).
  • The method returns "home", which tells Spring to render the home.html view using Thymeleaf.

2. Mapping HTTP Requests to Controller Methods

One of the key roles of the @Controller annotation is to map HTTP requests to specific methods in the controller class. You can use annotations such as @GetMapping, @PostMapping, @RequestMapping, etc., to specify the HTTP method and the URL pattern that should trigger the execution of a controller method.

Example: Handling Different HTTP Methods

  • The home() method handles HTTP GET requests to the root URL ("/").
  • The submitForm() method handles HTTP POST requests to the /submit URL.
  • Both methods add data to the model and return the names of the views (home.html and greeting.html), which Thymeleaf will render.

3. Integrating with Thymeleaf Templates

When a controller method returns a string, Spring looks for a view (HTML) file with the same name in the /src/main/resources/templates/ directory. Thymeleaf will then render the view with the data that was added to the model.

In the case of the example above:

  • The home() method returns "home", which tells Spring to render the home.html template.
  • Any attributes added to the model (like message) will be available to Thymeleaf, which can render them in the view.

Example: home.html Template

In this example, the message attribute passed from the controller will be rendered within the <p> tag using the th:text expression.

4. Handling Data with Model and View

The @Controller annotation helps in populating the model with data that can be dynamically rendered in the Thymeleaf template. The model is passed from the controller method to the view, allowing you to display user-specific content, such as names, user roles, or form inputs.

Example: Passing Data from Controller to View

In the userProfile.html template, you can now access the user object and display its properties:

This example demonstrates how the User object from the controller is passed to the view, where the properties are dynamically displayed using Thymeleaf expressions.

5. Redirecting to Another View

The @Controller annotation also allows for handling redirection within the application. By returning a string prefixed with "redirect:", you can redirect the user to a different URL.

Example: Redirecting After a Post Request

In this example, after the user submits a form, they are redirected to the thankYou page.

Conclusion

The @Controller annotation is central to building web applications in Spring Boot with Thymeleaf. It allows you to define controller classes that handle HTTP requests, pass data to the view, and return the appropriate Thymeleaf templates for rendering. By using this annotation, you can easily integrate Thymeleaf with Spring Boot, build dynamic web pages, and manage the flow of data between the client and server.

Similar Questions