🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business


Comments
Post a Comment
Leave Comment