Freemarker
Freemarker是一個用Java開發的模板引擎,一種基於模板和要改變的數據, 並用來生成輸出文本(HTML網頁、電子郵件、配置文件、源代碼等)的通用工具。
也就是說模型+數據=輸入(HTML等)
Spring Boot+Freemarker
目錄結構:
引入依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
application.yml:
spring:
application:
name: test-freemarker #指定服務名
freemarker:
cache: false #關閉模板緩存,方便測試
settings:
template_update_delay: 0 #檢查模板更新延遲時間,設置爲0表示立即檢查,如果時間大於0會有緩存不方便進行模板測試
template-loader-path: classpath:/templates/ #模板路徑
suffix: .ftl #模板後綴名稱
Student :
@Data
@ToString
public class Student {
private String name;//姓名
private int age;//年齡
private Date birthday;//生日
private Float mondy;//錢包
private List<Student> friends;//朋友列表
private Student bestFriend;//最好的朋友
}
test1.ftl:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Freemarker</title>
</head>
<body>
<table>
<tr>
<td>序號</td>
<td>姓名</td>
<td>年齡</td>
<td>餘額</td>
</tr>
<#list stus as stu>
<tr>
<td>${stu_index + 1}</td>
<td <#if stu.name =='小明'>style="background:deepskyblue;"</#if>>${stu.name}</td>
<td>${stu.age}</td>
<td >${stu.mondy}</td>
</tr>
</#list>
</table>
<br/><br/>
輸出stu1的學生信息:<br/>
姓名:${stuMap['stu1'].name}<br/>
年齡:${stuMap['stu1'].age}<br/>
輸出stu1的學生信息:<br/>
姓名:${stu1.name}<br/>
年齡:${stu1.age}<br/>
遍歷輸出兩個學生信息:<br/>
<table>
<tr>
<td>序號</td>
<td>姓名</td>
<td>年齡</td>
<td>餘額</td>
</tr>
<#list stuMap?keys as k>
<tr>
<td>${k_index + 1}</td>
<td>${stuMap[k].name}</td>
<td>${stuMap[k].age}</td>
<td >${stuMap[k].mondy}</td>
</tr>
</#list>
</table>
</body>
</html>
FreemarkerController:
@RequestMapping("/freemarker")
@Controller
public class FreemarkerController {
@RequestMapping("/test1")
public String freemarker(Map<String, Object> map){
//向數據模型放數據
map.put("name","freemarker");
Student stu1 = new Student();
stu1.setName("張三");
stu1.setAge(18);
stu1.setMondy(1000f);
stu1.setBirthday(new Date());
Student stu2 = new Student();
stu2.setName("李四");
stu2.setMondy(200f);
stu2.setAge(19);
stu2.setBirthday(new Date());
List<Student> friends = new ArrayList<>();
friends.add(stu1);
stu2.setFriends(friends);
stu2.setBestFriend(stu1);
List<Student> stus = new ArrayList<>();
stus.add(stu1);
stus.add(stu2);
//向數據模型放數據
map.put("stus",stus);
//準備map數據
HashMap<String,Student> stuMap = new HashMap<>();
stuMap.put("stu1",stu1);
stuMap.put("stu2",stu2);
//向數據模型放數據
map.put("stu1",stu1);
//向數據模型放數據
map.put("stuMap",stuMap);
//返回模板文件名稱
return "test1";
}
}
演示效果:
模板文件靜態化
使用freemarker將模板和數據生成html靜態文件存儲在磁盤上。
添加依賴:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
編寫測試類:
@SpringBootTest
@RunWith(SpringRunner.class)
public class FreemarkerTest {
//基於模板生成靜態化文件
@Test
public void testGenerateHtml() throws IOException, TemplateException {
//創建配置類
Configuration configuration=new Configuration(Configuration.getVersion());
String classpath = this.getClass().getResource("/").getPath();
//設置模板路徑
configuration.setDirectoryForTemplateLoading(new File(classpath + "/templates/"));
//設置字符集
configuration.setDefaultEncoding("utf-8");
//加載模板
Template template = configuration.getTemplate("test1.ftl");
//數據模型
Map map = getMap();
//靜態化
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
//靜態化內容
System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content);
//輸出文件到d:/test1.html中
FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/test1.html"));
int copy = IOUtils.copy(inputStream, fileOutputStream);
System.out.println(copy);
}
//基於模板字符串生成靜態化文件
@Test
public void testGenerateHtmlByString() throws IOException, TemplateException {
//創建配置類
Configuration configuration=new Configuration(Configuration.getVersion());
//獲取模板內容
//模板內容,這裏測試時使用簡單的字符串作爲模板
String templateString="" +
"<html>\n" +
" <head></head>\n" +
" <body>\n" +
" 名稱:${name}\n" +
" </body>\n" +
"</html>";
//加載模板
//模板加載器
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
stringTemplateLoader.putTemplate("template",templateString);
configuration.setTemplateLoader(stringTemplateLoader);
Template template = configuration.getTemplate("template","utf-8");
//數據模型
Map map = getMap();
//靜態化
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
//靜態化內容
System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content);
//輸出文件
FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/test1.html"));
IOUtils.copy(inputStream, fileOutputStream);
}
//數據模型
private Map getMap(){
Map<String, Object> map = new HashMap<>();
//向數據模型放數據
map.put("name","freemarker");
Student stu1 = new Student();
stu1.setName("張三");
stu1.setAge(18);
stu1.setMondy(1000f);
stu1.setBirthday(new Date());
Student stu2 = new Student();
stu2.setName("李四");
stu2.setMondy(200f);
stu2.setAge(19);
stu2.setBirthday(new Date());
List<Student> friends = new ArrayList<>();
friends.add(stu1);
stu2.setFriends(friends);
stu2.setBestFriend(stu1);
List<Student> stus = new ArrayList<>();
stus.add(stu1);
stus.add(stu2);
//向數據模型放數據
map.put("stus",stus);
//準備map數據
HashMap<String,Student> stuMap = new HashMap<>();
stuMap.put("stu1",stu1);
stuMap.put("stu2",stu2);
//向數據模型放數據
map.put("stu1",stu1);
//向數據模型放數據
map.put("stuMap",stuMap);
return map;
}
}