![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5yNmFWY2gzYkJWZ4Y2M3gjNkZTM5EGOmNGZ1YGO5YGZl9CX0JXZ252bj91Ztl2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
1.导入依赖
<
dependency>
<
groupId>org.springframework.boot</
groupId>
<
artifactId>spring-boot-starter-freemarker</
artifactId>
</
dependency>
2.在application.properties文件添加配置
########## 配置freemarker ###########是否开启缓存
spring.freemarker.cache=
false#路径
spring.freemarker.template-loader-path=
classpath:/templates#文件后缀
spring.freemarker.suffix=
.ftl spring.freemarker.charset=
UTF-8 spring.freemarker.content-type=
text/html3.创建controller
packagecom.qf.controller;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.Model;
importorg.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(
"/freemarker")
public classFreeMarkerController {
@RequestMapping(
"/hello")
publicString hello(Model model){
System.
out.println(
"hello freemarker");
model.addAttribute(
"hello",
"hello freemarker !");
return "/freemarker";
}
}
4.在templates目录下创建freemarker.ftl
<!DOCTYPE
html>
<
html>
<
head>
<
meta charset="UTF-8">
<
title>Title</
title>
</
head>
<
body>
hello.ftl
<
hr>
${hello}</
body>
</
html>
5.访问controller测试即可
补充:FreeMarker生成页面1.在D盘ftl文件夹下编写hello.ftl和student.ftl文件
hello.ftl<html>
<head>
<title>student</title>
</head>
<body>
${hello}
</body>
</html>
student.ftl<html>
<head>
<title>student</title>
</head>
<body>
<table width=600>
<tr>
<th>index</th>
<th>id</th>
<th>name</th>
<th>age</th>
<th>address</th>
</tr>
<#list stuList as stu>
<#if stu_index % 2 == 0>
<tr bgcolor="red">
<#else>
<tr bgcolor="green">
</#if>
<td>${stu_index}</td>
<td>${stu.id}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.address}</td>
</tr>
</#list>
</table>
</body>
</html>
2.创建实体类
packagecom.qf.entity;
public classStudent {
privateInteger
id;
privateString
name;
privateInteger
age;
privateString
address;
publicStudent(Integer id, String name, Integer age, String address) {
this.
id= id;
this.
name= name;
this.
age= age;
this.
address= address;
}
publicStudent() {
}
publicInteger getId() {
return id;
}
public voidsetId(Integer id) {
this.
id= id;
}
publicString getName() {
return name;
}
public voidsetName(String name) {
this.
name= name;
}
publicInteger getAge() {
return age;
}
public voidsetAge(Integer age) {
this.
age= age;
}
publicString getAddress() {
return address;
}
public voidsetAddress(String address) {
this.
address= address;
}
}
3.创建测试类并运行
packagecom.qf.test;
importcom.qf.entity.Student;
importfreemarker.template.Configuration;
importfreemarker.template.Template;
importorg.junit.Test;
importjava.io.File;
importjava.io.FileWriter;
importjava.util.ArrayList;
importjava.util.HashMap;
public classFreeMarkerTest {
@Test
public voidtest_freemarker()
throwsException{
//获取配置对象
Configuration configuration =
newConfiguration(Configuration.getVersion());
//设置字符集
configuration.setDefaultEncoding(
"utf-8");//设置加载的模版目录
configuration.setDirectoryForTemplateLoading(
newFile(
"D:/ftl"));
//使用map集合加载数据
HashMap<String, String> map =
newHashMap<>();
map.put(
"hello",
"hello freemarker");//创建输出流对象
FileWriter fileWriter =
newFileWriter(
newFile(
"D:/ftl_html/hello.html"));//获取加载的模板
Template template = configuration.getTemplate(
"hello.ftl");
//生成html文件
template.process(map,fileWriter);
//关流
fileWriter.close();
}
@Test
public voidtest_freemarker_student()
throwsException{
//获取配置对象
Configuration configuration =
newConfiguration(Configuration.getVersion());//设置字符集
configuration.setDefaultEncoding(
"utf-8");
//设置加载的模版目录
configuration.setDirectoryForTemplateLoading(
newFile(
"D:/ftl"));
//创建List集合获取多个元素
ArrayList<Student> students =
newArrayList<>();
students.add(
newStudent(1001,
"jack",18,
"郑州二七"));
students.add(
newStudent(1002,
"rose",19,
"郑州中原"));
students.add(
newStudent(1003,
"tom",20,
"郑州金水"));//使用map集合加载数据
HashMap<String,ArrayList> map =
newHashMap<>();
map.put(
"stuList",students);
//创建输出流对象
FileWriter fileWriter =
newFileWriter(
newFile(
"D:/ftl_html/student.html"));//获取加载的模板
Template template = configuration.getTemplate(
"student.ftl");
//生成html文件
template.process(map,fileWriter);
//关流
fileWriter.close();
}
}