使用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);
}
}

最新文章

  1. 总是多次出现 那个同样的 权限错误 _storage_write_error_, 所以一开始就把机器设好setenforce 0
  2. 微服务(Microservices)&mdash;&mdash;Martin Flower【翻译】
  3. async.whilst 的一个简化版实现
  4. js只弹窗一次
  5. elasticsearch auto delete old indices
  6. 最短路(Dijkstra) POJ 1062 昂贵的聘礼
  7. 1028-Digital Roots
  8. WindowManage与Window的在Activity的一点小应用
  9. HDU4648+Easy
  10. JS中replace()用法举例
  11. winform复制文件到指定目录
  12. 年会抽奖 抽奖系统 抽奖软件 C# Winform
  13. 《Linux内核分析》第一周——计算机是如何工作的?
  14. 软盘相关知识和通过BIOS中断访问软盘
  15. java日志-纯Java配置使用slf4j配置log4j(转)
  16. HTML---初识HTML
  17. [USACO08JAN]Telephone Lines
  18. 上楼梯问题(递归C++)
  19. hdu1950 Bridging signals
  20. 每天一个linux命令(3):cd命令

热门文章

  1. 浏览器事件循环机制(event loop)
  2. codeforces#1148E. Earth Wind and Fire(贪心)
  3. 简述JAVA类的生命周期
  4. Java如何接收前端传来的多层嵌套的复杂json串
  5. bypass disable_function总结学习
  6. np数组转换函数
  7. sql 查询存在一个表而不在另一个表中的数据
  8. CentOS 上 Jenkins 的安装
  9. fastcgi代理
  10. 使用Jsp/Js/Ajax/Json/Jquery/Easyui + Servlet + JDBC + Lucene/Mysql/Oracle完成数据库分页