Explain @Controller annotation example
The `@Controller` annotation in Spring is used to define a controller class that handles web requests. It’s a specialization of the `@Component` annotation, allowing Spring to detect and register it as a Spring bean. Unlike `@RestController`, which returns data directly to the client, `@Controller` typically returns a view name, which is resolved to an actual view by a `ViewResolver`.
Table of Contents
Example of Controller annotation example
Step 1: Create a Spring Boot Application
Ensure you have the necessary dependencies in your `pom.xml` (for Maven) or `build.gradle` (for Gradle).
Maven Dependency:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
Gradle Dependency:
```groovy
implementation 'org.springframework.boot:spring-boot-starter-web'
```
Step 2: Create the Main Application Class
Create the main class for the Spring Boot application.
```java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
Explanation:
@SpringBootApplication
: Indicates a configuration class that declares one or more@Bean
methods and also triggers auto-configuration and component scanning.
Step 3: Create a Controller
Create a simple controller using the @Controller
annotation.
```java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/web")
public class HelloController {
@GetMapping("/hello")
public String sayHello(Model model) {
model.addAttribute("message", "Hello, World!");
return "hello";
}
}
```
- Explanation:
- `@Controller`: Indicates that the class is a Spring MVC controller.
- `@RequestMapping("/web")`: Base URL for all endpoints in this controller.
- `@GetMapping("/hello")`: Maps HTTP GET requests to the `sayHello` method.
- `Model model`: Used to pass data to the view.
- `model.addAttribute("message", "Hello, World!")`: Adds an attribute to the model that can be accessed in the view.
- `return "hello"`: Returns the view name to be resolved.
Step 4: Create a View
Create an HTML file named hello.html
in the src/main/resources/templates
directory.
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
```
Explanation:
${message}
: Placeholder that will be replaced by the actual message passed from the controller.
Step 5: Running the Application
Run the application from the main class (DemoApplication
). When you navigate to http://localhost:8080/web/hello
in your web browser, you should see the message: “Hello, World!” displayed on the page.
Conclusion
- @Controller: Used to define a Spring MVC controller that returns view names to be resolved by a
ViewResolver
. - Example: Demonstrated how to use
@Controller
to create a simple web page that displays a message.