liveBook · Manning

Controller

As explained in Spring Start Here, Second Edition

A controller in Spring MVC is an application component that receives and handles web requests and produces responses.

A controller connects a web request to the page the application returns. In a minimal Spring MVC flow, you write an HTML page and a controller action for the request; Spring Boot configures the other MVC components. section 7.3 section 7.5

For a simple page, the pieces fit together like this:

  • Put the HTML document, such as home.html, in the Spring Boot project's resources/static folder. section 7.3
  • Mark a class with @Controller so Spring adds it to its context as a bean. section 7.3
  • Use @RequestMapping to associate a controller method with a specific HTTP request. The method returns a reference to the page. section 7.3

A home page action

A MainController method mapped to /home can return "home.html". For a first example, the method needs no additional request logic; the page can simply display “Welcome!” section 7.3

Figure 7.15 The steps for adding a static web page to your application. Add the HTML document containing the information the browser will display, and then write a controller with an action assigned to it.

See the two-step flow: create the HTML page, then write a controller action for it. section 7.3

How Spring Start Here, Second Edition covers it

How deep it goes
Defines controller annotations and a simple page-returning action, stopping short of adding request-specific logic.
Examples
Uses a MainController action mapped to /home that returns home.html, whose heading says “Welcome!”.
What it assumes
Starts from an existing Spring Boot project and explains the controller and page setup without requiring prior Spring knowledge.
What you can do after
Build a minimal page flow by pairing a static HTML document with a mapped controller action.
How it connects
Links Spring MVC, Spring Boot, HTTP requests, and HTML pages through @Controller and @RequestMapping.

Also explained in

Compare how 13 books explain controller →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.