Introduce a FreeMarker

Apache FreeMarker™ is a templating engine: a Java library for generating textual output (HTML web pages, emails, configuration files, source code, etc.) from templates and change data. Templates are written in the FreeMarker Template Language (FTL), which is a simple, proprietary language (unlike a full programming language like PHP). Typically, a general-purpose programming language such as Java is used to prepare the data (publish database queries, do business calculations). Apache FreeMarker then uses the template to display the prepared data. In the template, you will focus on how to render the data, and outside the template, you will focus on the data to render.

Thymeleaf is recommended by SpringBoot, and since I’m familiar with FreeMarker, I’ll show you how to use FreeMarker’s current front end page in SpringBoot. This is excerpted from FreeMarker’s website

SpringBoot uses FreeMarker steps

The first step is to introduce the spring-boot-starter-freemarker dependency in pm.xml as follows:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
Copy the code

Freemarkdemo. FTL = “freemarkDemo.ftl”

<h1>${msg}</h1>
Copy the code

The third step is to create a Controller that accesses freemarkDemo.ftl.


@Controller
@RequestMapping("/hello")
public class HelloWorldController {
	@RequestMapping("/test")
    public String test(Model model){
		model.addAttribute("msg"."SpringBoot With Freemark hello world!");
        return "test/helloworld"; }}Copy the code

test

In the browser input access FreeMarker Controller URL of the page: http://localhost:8080/sbe/hello/test for testing, test results are as follows:

summary

SpringBoot uses FreeMarker as follows:

  1. Add a FreeMarker starter dependency
  2. Create template page and visit template query page Controller

Code sample

See my GitHub repository springBootExamples under the module project name spring-boot-2.x-freemarker for code examples.

GitHub:github.com/zhuoqianmin…