1、pom文件,添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、配置application.yml
server:
port: 8881 #服务端口
spring:
freemarker:
cache: false #关闭模板缓存,方便测试
settings:
template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
suffix: .ftl #指定Freemarker模板文件的后缀名
template-loader-path: classpath:/templates #模板存放位置
3.在eneity包下创建实体类
package com.freemarker.test.eneity;
import lombok.Data;
import java.util.Date;
/**
* @description:
* @author: Administrator
* @time: 2024/1/5 15:23
*/
@Data
public class Student {
private String name;//姓名
private int age;//年龄
private Date birthday;//生日
private Float money;//钱包
}
4.创建模板
模板默认位置为resource下templates,该目录是freemarker的默认目录。创建模板hello.ftl,模板中的值会被freemarker替换成数据。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello World!</title>
</head>
<body>
<b>普通文本 String 展示:</b><br>
Hello ${name} <br>
<hr>
<b>对象Student中的数据展示:</b><br/>
姓名:${student.name}<br/>
年龄:${student.age}
<hr><br>
<#-- list 数据的展示 -->
<b>展示list中的stu数据:</b>
<br>
<br>
<table>
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
<td>钱包</td>
</tr>
<#list stuList as stu>
<tr>
<td>${stu_index+1}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.money}</td>
</tr>
</#list>
</table>
<hr>
</body>
</html>
5.创建Controller
package com.freemarker.test.controller;
import com.freemarker.test.eneity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @description:
* @author: Administrator
* @time: 2024/1/5 15:23
*/
@Controller
public class HelloController {
@GetMapping("/hello")
public String test(Model model){
//1.直接使用model添加数据
model.addAttribute("name","freemarker");
//2.在实体类种添加数据
Student student = new Student();
student.setAge(18);
student.setBirthday(new Date());
student.setMoney(10000000000.80f);
student.setName("小明");
model.addAttribute("student",student);
//3.在集合种添加数据
Student student2 = new Student();
student2.setAge(18);
student2.setBirthday(new Date());
student2.setMoney(10000000000.80f);
student2.setName("小红");
List<Student> stuList = new ArrayList<>();
stuList.add(student);
stuList.add(student2);
model.addAttribute("stuList",stuList);
return "hello";
}
}
6.根据模板文件生成HTML
需要在test下创建测试类。
package com.freemarker.test;
import com.freemarker.test.eneity.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.ui.Model;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
/**
* @description:
* @author: Administrator
* @time: 2024/1/6 18:18
*/
@SpringBootTest(classes = TestApplication.class)
@RunWith(SpringRunner.class)
public class FreemarkerTest {
@Autowired
private Configuration configuration;
@Test
public void test() throws IOException, TemplateException {
//freemarker的模板对象,获取模板
Template template = configuration.getTemplate("hello.ftl");
Map params = getData();
//合成
//第一个参数 数据模型
//第二个参数 输出流
template.process(params, new FileWriter("d:/hello.html"));
}
public Map getData(){
Map<String,Object> map = new HashMap<>();
//1.直接使用model添加数据
map.put("name","freemarker");
//2.在实体类种添加数据
Student student = new Student();
student.setAge(18);
student.setBirthday(new Date());
student.setMoney(10000000000.80f);
student.setName("小明");
map.put("student",student);
//3.在集合种添加数据
Student student2 = new Student();
student2.setAge(18);
student2.setBirthday(new Date());
student2.setMoney(10000000000.80f);
student2.setName("小红");
List<Student> stuList = new ArrayList<>();
stuList.add(student);
stuList.add(student2);
map.put("stuList",stuList);
return map;
}
}