liveBook · Manning

Template engine

As explained in Spring in Action, Sixth Edition

A view template library is a software library that processes templates to generate rendered web views.

A template engine processes templates to generate rendered web views. In a Spring MVC application, a controller returns a logical view name, and the selected library resolves that name to a template. section 1.3.1

Connect a template library to a Spring Boot web application:

  • Choose a library and add its Spring Boot starter dependency to the build. section 2.5
  • Place templates under /src/main/resources/templates in a Maven or Gradle project. section 1.3.1 section 2.5
  • Let Spring Boot detect the dependency and automatically configure the components needed to serve controller views. section 2.5
  • Replace the dependency and rewrite templates in the chosen language when switching libraries. section 2.5

Resolve a home page

A controller mapped to GET / can return "home". With Thymeleaf available, Spring resolves that logical name to /templates/home.html. The equivalent setup can use Mustache or another supported library, but the template syntax must change. section 1.3.1 section 2.5

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
  }
 
}

Inspect the controller annotations, route mapping, and logical view name that connect a web request to a template. section 1.3.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains dependency setup, template placement, view-name resolution, and autoconfiguration, but defers library-specific language details.
Examples
Uses a home controller, /templates/home.html, a Thymeleaf starter, and a Mustache ingredient-group iteration.
What it assumes
Assumes a Spring Boot web project and basic controller familiarity while teaching dependency selection, template placement, and view resolution.
What you can do after
Operate/configure a Spring Boot application with a selected template library and connect controller routes to rendered views.
How it connects
Ties template engines to Spring MVC controllers, Spring Boot autoconfiguration, Thymeleaf, Mustache, FreeMarker, Groovy templates, and JSP.

Also explained in

Compare how 5 books explain template engine →

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.