liveBook · Manning

Spring MVC

As explained in Spring in Action, Sixth Edition

Spring MVC is Spring's web framework for implementing model-view-controller applications and handling HTTP requests.

Spring MVC is Spring’s web framework for handling HTTP requests and building browser-facing applications. A controller receives a request, optionally adds model data, and forwards it to a view that produces HTML. section 1.3.1

A home-page controller

A controller for the root path can be only a few lines long: @Controller marks HomeController for component scanning, @GetMapping("/") handles HTTP GET requests, and returning "home" selects the logical home view. With Thymeleaf available, that name resolves to a Thymeleaf template. section 1.3.1

Listing 1.4 The home page controller
package tacos;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
 
@Controller                     #1
public class HomeController {
 
  @GetMapping("/")              #2
  public String home() {
    return "home";              #3
  }
 
}

Use the complete controller listing to see how component discovery, request mapping, and view selection fit together. section 1.3.1

Spring MVC supports a practical request-to-view workflow:

  • Map handlers with annotations such as @RequestMapping, @GetMapping, and @PostMapping. section 2.6
  • Return a logical view name, such as a Thymeleaf template, and forward the request with model data when needed. section 2.6
  • Process form input and validate it through the JavaBean Validation API and implementations such as Hibernate Validator. section 2.0 section 2.6
  • Create REST APIs that return non-HTML output as well as web frontends rendered through view technologies. section 1.4.1 section 2.6

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains controller annotations, view selection, model data, validation, and Boot autoconfiguration, while deferring deeper REST and reactive treatment.
Examples
Uses a root-path HomeController, a home view, Thymeleaf templates, custom taco forms, model data, and REST API output.
What it assumes
Assumes basic Spring application setup while introducing controllers, component scanning, request mappings, logical views, and autoconfiguration.
What you can do after
Build it from scratch by defining annotated controllers, mapping requests, passing model data, selecting views, and processing validated forms.
How it connects
Connects controllers with HTTP requests, model data, views, Thymeleaf, JavaBean Validation, REST APIs, and Spring WebFlux.

Related concepts

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