使用freemarker
將頁面生成html
文件,本節測試html
文件生成的方法:
1、使用模板文件靜態化
定義模板文件,使用freemarker
靜態化程序生成html
文件。
2、使用模板字符串靜態化
定義模板字符串,使用freemarker
靜態化程序生成html
文件。
1. pom.xml
配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. application.yml
配置
server:
port: 8088
spring:
application:
name: test-freemarker
# freemarker配置
freemarker:
cache: false #關閉模板緩存,方便測試
settings:
template_update_delay: 0 #檢查模板更新延遲時間,設置為0表示立即檢查,如果時間大於0會有緩存不方便進行模板測試
template-loader-path: classpath:/templates
charset: UTF-8
check-template-location: true
suffix: .ftl
content-type: text/html
expose-request-attributes: true
expose-session-attributes: true
request-context-attribute: request
3. 使用模板文件靜態化
3.1 創建測試類,編寫測試方法
package com.example.demo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* @author john
* @date 2019/12/20 - 19:09
*/
@SpringBootTest
public class TestFreemarkerHtml {
//基於模板生成靜態化文件
@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<String, Object> map = new HashMap<>();
map.put("name", "john");
//靜態化
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
//靜態化內容
System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content);
//輸出文件
FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/test/test1.html"));
int copy = IOUtils.copy(inputStream, fileOutputStream);
}
}
編寫模板
<html>
<head>
<title>hello world!</title>
</head>
<body>
${name}<br/>
<#assign text="{'bank':'工商銀行','account':'10101920201920212'}" />
<#assign data=text?eval />
開戶行:${data.bank} 賬號:${data.account}
</body>
</html>
生成文件
3.2 使用模板字符串靜態化
package com.example.demo;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* @author john
* @date 2019/12/20 - 19:09
*/
@SpringBootTest
public class TestFreemarkerHtml {
//基於模板字符串生成靜態化文件
@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<String, Object> map = new HashMap<>();
map.put("name", "john");
//靜態化
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
//靜態化內容
System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content);
//輸出文件
FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/test/test2.html"));
IOUtils.copy(inputStream, fileOutputStream);
}
}