In this tutorial, we will learn what is @Controller and how to use it in Spring Boot web applications.
Spring @Controller Annotation Overview
The @Controller annotation is used in Spring MVC to mark a class as a controller component. It acts as a request handler and handles incoming HTTP requests, performing the necessary processing and returning a response.
Controllers in Spring MVC are responsible for processing user requests, interacting with business logic or services, and returning a view or response to the client.
Spring @Controller Annotation Example
Let's use Spring Boot to develop a simple web application.
Add Maven Dependencies
Add the following Maven dependencies to your Spring boot application:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
The spring-boot-starter-web starter is responsible for setting up the web-related components and configurations in a Spring Boot application. It includes the necessary dependencies and auto-configurations to develop web applications using Spring MVC.
The spring-boot-starter-thymeleaf starter includes the Thymeleaf templating engine for server-side rendering of views. Thymeleaf is a popular Java-based templating language that enables the creation of dynamic web pages.
By including the spring-boot-starter-web and spring-boot-starter-thymeleaf starters in your Spring Boot project, you can quickly set up a web application with the necessary web infrastructure, request handling capabilities, and Thymeleaf templating support. These starters simplify the configuration process and provide a solid foundation for building modern, scalable, and interactive web applications using Spring Boot.
Create Spring MVC HelloController
Next, create a class and annotate it with @Controller:
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloController { @GetMapping({ "/", "/hello" }) public String hello(@RequestParam(value = "name", defaultValue = "World", required = true) String name, Model model) { model.addAttribute("name", name); return "hello"; } }
In the example above, the HelloController class is annotated with @Controller. The @GetMapping annotation is used to map the /hello and / URL paths to the home method, which returns a view name "hello". When a user visits the /hello URL, this method is executed and the associated view is rendered.
Create Thymeleaf Template View
Next, let's create a Thymeleaf template hello.html under /resources/templates folder and add the following code to it:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
In the above code, th:text Thymeleaf attribute specifies that the content of the paragraph should be set using Thymeleaf's text substitution feature.
"Hello, ' + ${name} + '!'": This Thymeleaf expression concatenates the string "Hello, " with the value of the ${name} variable and then appends "!" at the end. The ${name} variable is expected to be passed as a model attribute when rendering the template. The result of this expression will be dynamically inserted into the paragraph element when the page is rendered, displaying a personalized greeting message.
Demo
Next, let's run the Spring boot application and see the result in the browser:
Root URL: http://localhost:8080/
The /hello?Ramesh URL: http://localhost:8080/hello?Ramesh
Conclusion
The @Controller annotation is a fundamental building block of Spring MVC, allowing developers to create web applications and handle HTTP requests with ease. By using the @Controller annotation, you can define request mapping methods and build flexible, modular, and maintainable web applications using the Spring Framework.
Related Spring and Spring Boot Annotations
- Spring Boot @RestController Annotation Example Tutorial
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Comments
Post a Comment
Leave Comment