The third day of micro HR: SpringBoot integrates Freemarker

Let's talk about how SpringBoot integrates Freemarker:

You need to check web and Freemarker when creating a springboot project

After the project is started successfully, press shift twice to view the FreeMarkerAutoConfiguration class, which is the automatic configuration class of FreeMarket

Let's talk about what's done in FreeMarkerAutoConfiguration
Check template location:

Get template location:

The most important ones are:

@ConditionalOnClass({freemarker.template.Configuration.class, FreeMarkerConfigurationFactory.class})

The meaning of this code is: the following methods will take effect when Freemarker is introduced

And this dependency has been automatically configured in pom.xml file

@ConditionalOnClass({freemarker.template.Configuration.class, FreeMarkerConfigurationFactory.class})

The above comment is very important in Freemarker.

Look again:

@Import({FreeMarkerServletWebConfiguration.class, FreeMarkerReactiveWebConfiguration.class, FreeMarkerNonWebConfiguration.class})

This code allows FreeMarker to be used in different environments


Press and hold ctrl to click into FreeMarkerProperties:
Type safe property injection:

On the surface of this sentence, we can configure such a property as spring.freemarker in

Default template load path:

The template suffix is. ftl:

Next, we will use a specific example to illustrate how Freemarker uses:
1. Create entity class user

package org.javaboy.freemarker.bean;

public class User {
    private Long id;
    private String username;
    private String address;

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getUsername() {
        return username;

    public void setUsername(String username) {
        this.username = username;

    public String getAddress() {
        return address;

    public void setAddress(String address) {
        this.address = address;

    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", address='" + address + '\'' +

2. control class

package org.javaboy.freemarker.controller;

import org.javaboy.freemarker.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;

public class UserController {

    public String user(Model model) {
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId((long) i);
            user.setUsername("javaboy>>>" + i);
            user.setAddress(">>>" + i);
        model.addAttribute("users", users);
        return "user";

3. Create Freemarker template under template package
Click Edit File Templates

Edit the template name, suffix and content respectively

4. Create freemarker file

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <table border="1">
            <td>User name</td>
            <td>User address</td>
        <#list users as u>

Display the added content of control class in the form of table

5. boot
Visit: http://localhost:8080/user

We can also configure freemarker in the properties file as follows:

#Template encoding format, default is UTF-8
#Define the content type of the template
#Open cache or not
#Configure template suffix

264 original articles published, 23 praised, 20000 visitors+
Private letter follow

Tags: FreeMarker Spring SpringBoot Java

Posted on Mon, 13 Jan 2020 04:43:36 -0500 by programmingjeff