Preface
In the previous article Spring Boot integration Thymeleaf , we learned how to integrate Thymeleaf template into Spring Boot, so today we will take a look at another old open source free template engine-FreeMarker!
Introduction to FreeMarker
FreeMarker is a template engine: a general tool based on templates and data to be changed, and used to generate output text (HTML pages, emails, configuration files, source code, etc.). It is not for end users, but a Java class library, a component that programmers can embed in the products they develop.
The above is the official definition from FreeMarker. By using FreeMarker, we can render the template and data we need into HTML to achieve the effect we want. By separating the template and the data, the division of labor is made clearer. The template is focused on how to display the data, while in the data aspect, we can focus on what kind of data to display. The following figure is the function of FreeMarker we described above.
Then, let's take a look at how to integrate FreeMarker into our Spring Boot like Thymeleaf to make our development easier.
Ready to work
Environmental preparation
Before the official start, the environment on which this article is based is still given to avoid the impact that environmental problems may have on everyone.
- JDK 17 (in theory, version 1.8 is recommended)
- IDEA
- SpringBoot 2.x
Add FreeMarker dependency
Before we start, we need to add FreeMarker related dependencies, and here we can choose two ways. One is to add it when the project is created, and the other is pom.xml
file after the project is created. Next, let's take a look at how to add it in two different ways. If you are not sure how to create a Spring Boot project, you can refer to my previous article: create a Spring Boot project .
- first type
When using IDEA to create a new Spring Boot project, check Apache FreeMarker
2. second kind
If the FreeMarker template engine dependency is not added during creation, or there is no such requirement at the beginning of the project creation, but the requirement is added in the subsequent process, you can directly add the FreeMarker dependency pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
Add FreeMarker related configuration
After adding dependencies, we need to add FreeMarker related configuration application.yml
spring:
freemarker:
# 模板后缀名
suffix: .ftl
# 文档类型
content-type: text/html
# 页面编码
charset: UTF-8
# 页面缓存
cache: false
# 模板路径
template-loader-path: classpath:/templates/
Write entity classes and Controller
Writing entity classes
Create a User
class, and complete its various setter
, getter
, construction methods, etc., here I still use the Lombok plug-in to simplify our code. For more usage of the plug-in, please refer to my previous article: Lombok Installation and Usage Guide .
package com.cunyu.springbootfreemarkerdemo.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
/**
* Created with IntelliJ IDEA.
*
* @author : zhangliang
* @version : 1.0
* @project : java-learning
* @package : com.cunyu.springbootfreemarkerdemo.entity
* @className : User
* @createTime : 2021/11/30 21:55
* @email : 747731461@qq.com
* @公众号 : 村雨遥
* @website : https://cunyu1943.github.io
* @description :
*/
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int age;
private String name;
private String email;
}
Write Controller
Here we create a User
object, and then set its age, Email, name and other related information, and then add it to the attributes to facilitate transmission to the front end for display.
package com.cunyu.springbootfreemarkerdemo.controller;
import com.cunyu.springbootfreemarkerdemo.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
* Created with IntelliJ IDEA.
*
* @author : zhangliang
* @version : 1.0
* @project : java-learning
* @package : com.cunyu.springbootfreemarkerdemo.controller
* @className : UserController
* @createTime : 2021/11/30 22:04
* @email : 747731461@qq.com
* @公众号 : 村雨遥
* @website : https://cunyu1943.github.io
* @description :
*/
@Controller
public class UserController {
@GetMapping("/index")
public String index(Model model) {
User user = new User();
user.setAge(26);
user.setEmail("747731461@qq.com");
user.setName("村雨遥");
model.addAttribute("user", user);
return "index";
}
}
Data rendering
When the entity class and Controller are written, we can use the template to display it. index.ftl
resources/templates
in the 061a63c83c5d6b road of the project. Note that the file name here must be consistent with the string returned by the index
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FreeMarker 实验</title>
</head>
<body>
<table>
<tr>
<td>姓名</td>
<td>年龄</td>
<td>Email</td>
</tr>
<tr>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.email}</td>
</tr>
</table>
</body>
</html>
test
Start our project, and then visit the following path in the browser:
http://localhost:8080/index
If the information shown in the figure below is displayed, it means that our integration work has been successfully completed!
Summarize
The above is the specific process of Spring Boot integrating FreeMarker, which can be combined with the previous article: Spring Boot integrating Thymeleaf instance read together, compare some common points and differences between Thymeleaf and FreeMarker in the integration process, I believe you will have more reward.
Finally, about the case code of this article, I have uploaded it to Github, and friends in need can : 161a63c83c5e40 Portal .
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。