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
