Chapter 8: Implementing web apps with Spring Boot and Spring MVC
Introduces dynamic web views and explains that a template engine displays variable data sent by a controller.
After this chapter you will be able to
- Purpose of template engines. Explain why a web app might use a template engine: pages need not be static, and the content returned to the browser can include variable data sent by a controller.
- Dynamic view response flow. Explain why the same page can show different content and how controller-supplied data enters the Spring MVC response flow.
- Controller Model attributes. Describe how a controller action passes named values to a view using a Model parameter.
- Thymeleaf template rendering. Describe where to place a dynamic HTML view and how it refers to attributes supplied through the Model.
- Query parameters and path variables. Distinguish query parameters from path variables by where each appears in the URI and when the section recommends a path variable.
- Headers and request body. Distinguish request headers from the request body as ways to send client data, including their differing suitability for data size.
- Query string key value syntax. Describe how to append one or more request parameters to a path and identify each parameter’s name and value.
- Controller request parameter binding. Explain how a Spring MVC controller action receives a query string value and can use it for a view.
- Required and optional parameter behavior. Distinguish the default behavior of @RequestParam from an explicitly optional request parameter.
- Path variable placement and binding. Explain how a client sends a path variable and how a controller receives it: the value occupies a position in the URL path, the controller names that position in curly braces, and @PathVariable marks the action parameter that receives it.
- Choosing path variables appropriately. Justify using path variables for one or two mandatory values central to a page’s result, and request parameters for optional values or larger numbers of values.
- HTTP methods and intent. Explain how a request’s path and HTTP method identify the requested action, and why GET should retrieve data rather than change it.
- Product list GET flow. Trace how the product-list example serves a GET request for /products through the controller, service, model, and view.
- Product addition through POST. Describe how the product example uses an HTML form and a POST controller action to add a product through the service.
- Method-specific controller mappings. Explain how Spring MVC distinguishes controller actions by both path and HTTP method, including the difference between @RequestMapping and dedicated method annotations.
What it builds on
- Implementing a web app with Spring MVC (chapter 7) · The prior introduction to implementing a web app with Spring MVC.
- Using the singleton bean scope (chapter 5) · Singleton bean scope.
Section by section
Introduction
Introduces dynamic web views and explains that a template engine displays variable data sent by a controller.
- Purpose of template engines: Explain why a web app might use a template engine: pages need not be static, and the content returned to the browser can include variable data sent by a controller.
Why it matters: This gives you the basic purpose of a template engine before the chapter shows one in use.
Key terms: dynamic views, template engine, controller, variable data
8.1 Implementing web apps with a dynamic view
This section shows how a Spring MVC controller sends data to a Thymeleaf view so the same page can display different content.
- Dynamic view response flow: Explain why the same page can show different content and how controller-supplied data enters the Spring MVC response flow.
Why it matters: This is the reason one page can reflect different data on different requests. - Controller Model attributes: Describe how a controller action passes named values to a view using a Model parameter.
Why it matters: Named Model attributes give the view a way to use values supplied by the controller. - Thymeleaf template rendering: Describe where to place a dynamic HTML view and how it refers to attributes supplied through the Model.
Why it matters: The template must be in the right place and refer to the right attribute names to display controller-supplied values.
Key terms: dynamic view, dispatcher servlet, handler mapping, controller, view name, Model, addAttribute(), key, value, @RequestMapping
8.1.1 Getting data from the HTTP request
This section explains why web apps receive client data and compares four ways to send it in an HTTP request.
- Client data and server processing: Explain why a client sends data to a server, using a section example such as an order, forum post, login, or contact form.
Why it matters: The purpose of sending data gives context for choosing how to include it in a request. - Query parameters and path variables: Distinguish query parameters from path variables by where each appears in the URI and when the section recommends a path variable.
Why it matters: Their different locations and the mandatory-value guideline help distinguish two ways to send small values. - Headers and request body: Distinguish request headers from the request body as ways to send client data, including their differing suitability for data size.
Why it matters: This distinction explains why data that is too large for a header belongs in the request body.
Key terms: client, server, HTTP request parameter, query parameter, query expression, path variable, HTTP request header, HTTP request body
8.1.2 Using query string parameters to send data from client to server
This section explains when query string parameters are useful, how a client sends them, and how a Spring MVC controller receives them.
- Suitable uses for request parameters: Explain why query string parameters suit small amounts of data and optional search or filtering criteria.
Why it matters: This helps you recognize when query parameters fit a client-to-server request. - Query string key value syntax: Describe how to append one or more request parameters to a path and identify each parameter’s name and value.
Why it matters: The controller can receive values only if the client sends them in the expected request format. - Controller request parameter binding: Explain how a Spring MVC controller action receives a query string value and can use it for a view.
Why it matters: This connects a value sent in the URL to the controller code that uses it. - Required and optional parameter behavior: Distinguish the default behavior of @RequestParam from an explicitly optional request parameter.
Why it matters: Optional search criteria need an explicit annotation setting so missing values do not cause a bad-request response.
Key terms: request parameters, query string parameters, search and filtering criteria, query string, key=value, request parameter, @RequestParam, controller’s action method, HTTP request parameter, @RequestParam(required=false)
8.1.3 Using path variables to send data from client to server
This section explains how path variables carry client-supplied values in a URL and when to use them instead of request parameters.
- Path variable placement and binding: Explain how a client sends a path variable and how a controller receives it: the value occupies a position in the URL path, the controller names that position in curly braces, and @PathVariable marks the action parameter that receives it.
Why it matters: This lets you connect a value in a request URL to the controller parameter that receives it. - Choosing path variables appropriately: Justify using path variables for one or two mandatory values central to a page’s result, and request parameters for optional values or larger numbers of values.
Why it matters: Choosing the right location for values keeps URLs readable and avoids treating optional inputs as required parts of the path.
Key terms: path variable, @PathVariable, controller’s action, path variables, request parameters, mandatory parameters, optional values
8.2 Using the GET and POST HTTP methods
The section uses a product-list web app to show how HTTP methods distinguish viewing data from adding it, how Spring MVC maps those requests, and how request data reaches a controller.
- HTTP methods and intent: Explain how a request’s path and HTTP method identify the requested action, and why GET should retrieve data rather than change it.
Why it matters: The method tells clients what kind of action a request is meant to perform. - Product list GET flow: Trace how the product-list example serves a GET request for /products through the controller, service, model, and view.
Why it matters: It connects a GET request to the page of products the user sees. - Product addition through POST: Describe how the product example uses an HTML form and a POST controller action to add a product through the service.
Why it matters: It shows how a data-changing user action becomes a POST request rather than a GET. - Method-specific controller mappings: Explain how Spring MVC distinguishes controller actions by both path and HTTP method, including the difference between @RequestMapping and dedicated method annotations.
Why it matters: Explicit mappings make it clear which controller action handles each kind of request. - Automatic Product parameter binding: Explain when Spring can create a Product controller parameter from request parameters instead of requiring explicit @RequestParam parameters.
Why it matters: This explains where an action’s Product argument comes from when the binding code is not explicit. - Shared product list limitation: Justify why the example’s list stored in a service bean is a teaching simplification rather than a production-ready design.
Why it matters: It prevents the example’s simple storage approach from being mistaken for safe production code.
Key terms: path, HTTP method, HTTP GET, /products, ProductService, Model, dispatcher servlet, view, HTTP POST, HTML form
8.3 Questions
This section provides questions for reviewing chapter concepts, with answers in Appendix A.
8.4 Summary
This summary connects dynamic views, client-supplied request data, and HTTP methods in Spring web apps.
Where this is used later
- Using the session scope in a Spring web app (chapter 9) · Controllers and views in a Spring web app.
- Using a request body to get data from the client (chapter 10) · Develops the use of a request body to receive client data.
- Summary (chapter 10) · Earlier work with path variables as client-to-server data.
- Implementing REST endpoints (chapter 10) · Distinguishing retrieval from data-changing requests helps explain REST endpoint mappings.
- Using the request scope in a Spring web app (chapter 9) · Using GET and POST requests in a web app.
- Using REST services to exchange data between apps (chapter 10) · Mapping controller actions to HTTP methods in a Spring web app.
- Using JdbcTemplate to work with persisted data (chapter 12) · Later persisted-data work replaces the example’s in-memory-list context.
Concepts explained in this chapter
- HTTP methodAn HTTP method is the standardized operation token in an HTTP request that indicates the intended action on a resource.
- dynamic viewA dynamic view is a presentation resource whose rendered content is generated or populated from runtime application data.
- template engineA template engine is software that combines a view template with data to generate rendered output such as HTML.
- HTTP GETGET is the HTTP method for requesting a representation of a resource without intending to change server-side state.
- POSTPOST is the HTTP method for submitting data to a resource for processing, often resulting in a state change.
- HTTP request parameterAn HTTP request parameter is a named value supplied with a request, such as in its query string or submitted form data.
- path variableA path variable is a value conveyed within a request path, typically as a variable segment of a resource URI.
- ThymeleafThymeleaf is a Java server-side template engine for processing HTML and other markup templates with application data.
- Spring MVC modelThe Spring MVC Model holds attributes that a controller makes available to a view.
- @RequestParam annotationThe @RequestParam annotation binds a Spring MVC controller method parameter to an HTTP request parameter.
- HTTP headerAn HTTP header is a metadata field in an HTTP request or response that conveys information about the message or its handling.
- payloadAn HTTP request body is the payload portion of a request that carries data sent by the client.
- HTTP PUT methodPUT is the HTTP method for creating or completely replacing the representation of a resource at a specified target URI.
- HTTP requestAn HTTP request is a message sent by an HTTP client to request an operation or resource from a server.
- MVC modelA model is an application object that represents data used by the application and made available for presentation.
- PATCHPATCH is the HTTP method for applying partial modifications to a resource.
- viewA view is the presentation resource whose content is rendered and returned as a web response.
- web formAn HTML form is a browser interface element that collects user input and submits it to a server.
- @getmappingThe @GetMapping annotation maps a Spring MVC method to HTTP GET requests for a specified path.
- @postmappingThe @PostMapping annotation maps a Spring MVC method to HTTP POST requests for a specified path.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
