Category: Spring Updated: Jul 27, 2026

Mastering the @Controller Annotation in Java Spring: A Complete Guide for Beginners

Spring @Controller Annotation Tutorial for Beginners
Spring @Controller Annotation Tutorial for Beginners

Understanding Through a Teacher and Student Dialogue

Student

"I am starting to learn Java programming basics and building web applications with Spring Boot. When creating traditional web pages that render HTML views, I see the @Controller annotation used everywhere. What role does it play?"

Teacher

"The @Controller annotation is a specialized stereotype in Spring MVC used to define a traditional controller that handles web requests and returns view templates like JSP or Thymeleaf."

Student

"Ah, I see! So it is different from a REST controller because it renders actual web pages rather than raw JSON data?"

Teacher

"Exactly. Let us explore how to configure and use this web controller annotation effectively in your Spring projects!"

1. Introduction to Controller in Spring Boot

Key Takeaway: Let's dive into the core concepts of "1. Introduction to Controller in Spring Boot" cleanly and effectively.

1. Introduction to Controller in Spring Boot
1. Introduction to Controller in Spring Boot

When working with Java beginners tutorials for Spring MVC, handling page navigation and user web requests is a fundamental concept. The @Controller annotation marks an ordinary Java class as a Spring web component capable of receiving incoming HTTP requests from client browsers, executing business operations, and returning the logical name of a view template to be rendered.

For Java programming basics, think of @Controller as a hotel concierge. When a guest arrives with a request for a specific room or facility, the concierge figures out which physical room or layout matches the request and directs them to the right place.

2. Setting Up a Basic Web Controller

2. Setting Up a Basic Web Controller
2. Setting Up a Basic Web Controller

To use the @Controller annotation, place it directly above your class definition and combine it with request mapping methods. Let us look at a simple example demonstrating standard web page routing.


package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String displayHomePage() {
        return "index";
    }
}

In this initial code sample, returning the string "index" tells the Spring MVC view resolver to look for a template file named index.html or index.jsp in your resources directory.

3. Passing Data to Views Using Model

3. Passing Data to Views Using Model
3. Passing Data to Views Using Model

In web applications built for Java tutorial for beginners, controllers often need to pass dynamic data from the backend to the HTML view template. Spring provides a Model interface for this purpose.


package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class DashboardController {

    @GetMapping("/dashboard")
    public String loadDashboard(Model model) {
        model.addAttribute("userName", "JavaBeginner");
        return "dashboard";
    }
}

Using model.addAttribute binds key-value pairs that can be accessed directly inside your template engine to personalize the rendered user interface.

4. Handling Path Variables and Request Parameters

Key Takeaway: Let's dive into the core concepts of "4. Handling Path Variables and Request Parameters" cleanly and effectively.

4. Handling Path Variables and Request Parameters
4. Handling Path Variables and Request Parameters

Web controllers frequently need to capture dynamic segments from URLs or query strings. You can handle these inputs using annotations like @PathVariable or @RequestParam.


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.RequestParam;

@Controller
public class SearchController {

    @GetMapping("/search")
    public String searchCatalog(@RequestParam("query") String keyword, Model model) {
        model.addAttribute("searchTerm", keyword);
        return "search-results";
    }
}

This allows your traditional web views to react dynamically to user input submitted through search inputs or navigation parameters.

5. Returning Redirects and Forwarding Views

5. Returning Redirects and Forwarding Views
5. Returning Redirects and Forwarding Views

Sometimes after processing a form submission, a controller needs to redirect the user to a different URL route rather than rendering a template directly.


package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class AccountController {

    @PostMapping("/submit-form")
    public String processAccountUpdate() {
        return "redirect:/dashboard";
    }
}

Prefixing the return value with redirect: instructs the browser to issue a brand new request to the specified target URL path.

6. Verifying Controller Initialization Logs in Spring Boot

6. Verifying Controller Initialization Logs in Spring Boot
6. Verifying Controller Initialization Logs in Spring Boot

Inspecting console output during application launch confirms that Spring successfully discovers and maps your traditional controller endpoints.


2026-07-13 14:01:34.000 INFO --- [main] o.s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/], methods=[GET]}" onto public java.lang.String com.example.demo.controller.HomeController.displayHomePage()
2026-07-13 14:01:34.400 INFO --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http)

Reviewing these startup logs reassures you that your web controller paths are mapped correctly and ready to serve web pages.

Summary

Key Takeaway: Let's dive into the core concepts of "Summary" cleanly and effectively.

Summary
Summary

Throughout this comprehensive guide, we have explored the core fundamentals, mechanics, and practical applications of the @Controller annotation in Java Spring and Spring Boot. For beginners starting with Java programming basics and building traditional server-rendered web applications, understanding how HTTP requests map to view templates like Thymeleaf or JSP is an essential skill. We reviewed how @Controller handles request routing, how the Model interface passes dynamic data from backend controllers to frontend views, how request parameters capture user input, and how redirects manage page transitions effectively.

By mastering these traditional MVC concepts, you can build well-structured web interfaces and navigate Spring request lifecycles with absolute confidence.


package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class SummaryWebController {
    @GetMapping("/summary-page")
    public String renderSummaryPage() {
        return "summary";
    }
}
Review Conversation Between the Teacher and Student

Student

"Looking back at everything we discussed, I now see that @Controller is designed for rendering full HTML web pages rather than raw API payloads."

Teacher

"Precisely! It acts as the coordinator between incoming browser requests and your backend view templates."

Student

"And using the Model object makes it simple to inject dynamic data straight into the page."

Teacher

"Spot on. You are now fully prepared to develop and run your own dynamic web applications!"

Back to Category
Latest Articles
New1
Spring
Spring Boot and Java Version Compatibility Guide: Spring Boot 3.5 3.4 3.3 with Java 21 and Java 17
New
New2
Spring
Complete Guide to Spring Data JPA getReferenceById for Beginners
New
New3
Spring
Complete Guide to Spring Data JPA findAll for Beginners
New
New4
Spring
Spring Data JPA JpaRepository Tutorial for Beginners: Complete Guide to Database Access in Spring Boot
New
Popular Articles
No.1
Java&Spring記事人気No1
Spring
Complete Guide to Spring @GeneratedValue for Java Beginners – Understand Primary Key Generation in JPA
No.2
Java&Spring記事人気No2
Spring
Complete Guide to Spring @PreAuthorize for Java Beginners – Secure Your Application with Method-Level Security
No.3
Java&Spring記事人気No3
Spring
Spring Boot GetMapping Tutorial: Complete Guide to GetMapping in Java for Beginners
No.4
Java&Spring記事人気No4
Spring
Spring @Valid Annotation Tutorial for Beginners: Input Validation in Spring Boot
No.5
Java&Spring記事人気No5
Spring
Mastering the @PostMapping Annotation in Java Spring: A Complete Guide for Beginners
No.6
Java&Spring記事人気No6
Spring
Spring Data JPA Like Query Guide: findByFirstnameLike for Java Beginners
No.7
Java&Spring記事人気No7
Spring
Mastering the @Id Annotation in Java Spring: A Complete Guide for Beginners
No.8
Java&Spring記事人気No8
Spring
Mastering the @Entity Annotation in Java Spring: A Complete Guide for Beginners