文章目录
一、SpringBoot整合FreeMarker
1.1导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
1.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/html
1.3创建实体类
package com.qf.pojo;
public class Student {
private Integer id;
private String name;
private Integer age;
private String address;
public Student(Integer id, String name, Integer age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public Student() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
1.4在templates目录下创建student.ftl文件(然后把该ftl文件拷贝到D:/ftl目录下,一会用)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1" width=600>
<tr>
<th>index</th>
<th>id</th>
<th>name</th>
<th>age</th>
<th>address</th>
</tr>
<#list students as student>
<#if student_index % 2 == 0>
<tr bgcolor="red">
<#else>
<tr bgcolor="yellow">
</#if>
<td>${student_index}</td>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.address}</td>
</tr>
</#list>
</table>
</body>
</html>
1.5创建controller
package com.qf.controller;
import com.qf.pojo.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
@Controller
@RequestMapping("/freemarker")
public class FreeMarkerController {
@RequestMapping("/student")
public String hello(Model model) throws Exception {
//创建List集合获取多个元素
ArrayList<Student> students = new ArrayList<>();
students.add(new Student(1, "jack", 18, "郑州二七"));
students.add(new Student(2, "rose", 19, "郑州中原"));
students.add(new Student(3, "tom", 20, "郑州金水"));
model.addAttribute("students",students);
return "/student";
}
//生成静态页面的方法
@RequestMapping("createHtml")
@ResponseBody
public String createHtml()throws Exception{
//获取配置对象
Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
//设置字符集
configuration.setDefaultEncoding("utf-8");
//设置加载的模版目录
configuration.setDirectoryForTemplateLoading(new File("D:/ftl"));
//创建List集合获取多个元素
ArrayList<Student> students = new ArrayList<>();
students.add(new Student(1,"张三",18,"北京"));
students.add(new Student(2,"李四",19,"上海"));
students.add(new Student(3,"王五",20,"广州"));
//使用map集合加载数据
HashMap<String,ArrayList> map = new HashMap<>();
map.put("students",students);
//创建输出流对象
FileWriter fileWriter = new FileWriter(new
File("D:/ftl_html/student.html"));
//获取加载的模板
Template template = configuration.getTemplate("student.ftl");
//生成html文件
template.process(map,fileWriter);
//关流
fileWriter.close();
return "success";
}
}
启动工程,进行测试