The template engine is used to generate dynamic web content. Once the Controller has collected enough data, it hands the data to the template engine for processing. The template engine generates HTML from the data and returns it to the browser.
Spring Boot supports a variety of templating engines (which can be configured automatically), including FreeMarker, Thymeleaf, Mustache, and Groovy Templates. JSP, as you probably know, is considered an outdated technology by many. Although it still does most of what you need and can be used in Spring Boot, it is no longer recommended. . Thymeleaf is now widely adopted, and its feature is that it can open template files directly in the browser to facilitate front-end development. FreeMarker uses the FreeMarker Template Language (FTL), which has a similar feel to using JSP or PHP, but is not a universal language, focusing only on presenting data in templates. Mustache and Groovy Templates are a little confusing, so just take a look at the examples.
It should be noted that there is no default template engine for Spring Boot. All four templates mentioned above are treated equally. If you need one, you can add the corresponding Starter. In addition, the template engine is used to implement V(View) in MVC, if your project is separated from the front end, you don’t need a template engine.
Let’s take a look at how to add their dependencies:
Choose whichever one suits you. Once the dependency is selected, the Controller’s request handler can return a String representing the name of the desired template file. So where do I find template files? By default, the Spring Boot will set the path to the SRC/main/resources/templates, if you need the name of “home” the template file, you can go to the path search the prefix for home files, suffix, according to the different template engine would be different. For example, Thymeleaf uses HTML directly, FreeMarker uses FTL, Mustache uses Mustache, and Groovy Templates uses TPL.
The home method in the figure tells the framework that it needs to use homt.ftl to generate a web page. If the home method returns the string “pages/home”, then the framework goes to the Pages directory under Templates to look for the template file. Deeper paths and so on.
The following two diagrams illustrate how data is passed to the template engine and how the four template engines present the data. The home method has a Model parameter, and all the data passed to the template engine is added through the Model object. The template engine locates data based on the names “list” and “title” and displays:
You can see the difference in the picture. I suggest you use FreeMarker or Thymeleaf, after all, there are more people using it, it is convenient to solve problems. My personal preference for comparison is to use FreeMarker, where the logic of presenting data is separate from DOM and intuitive. Thymeleaf puts the logic of control into the label, takes a detour in your head, and claims the advantage are not so obvious.
All four template engines have properties that control whether caching is turned on or not. Caching means that you parse only once at run time and use it directly afterwards. This caching can be efficient in production environments, but in development environments, where you may change pages frequently, caching is not necessary. The following figure lists several attributes of the template engine control cache, along with their default values:
It is strange that the default values are not quite the same. I looked at the official Spring Boot documentation, and there are some on and some off. I read in Spring in Action, Fifth Edition that the author states that by default these properties are true, meaning that caching is on by default. I don’t know who to believe, but trust the code. I wrote the code to verify that it is indeed consistent with the description of the official document. If you want to avoid this uncertainty, it is recommended to write the configuration separately for the development environment and the build environment, with one on and one off.
Original link: fookwood.com/spring-boot…