- freemarker
- 什么是freemarker
- 为什么要使用freemarker
- 在java使用freemarker做静态化
- 页面预览和页面发布
- mongdb的gridfs
- 页面预览
- 页面发布
freemarker是一款模板引擎技术,即一种基于模板和数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 是一个Java类库
为什么要使用freemarkerfreemarker可以使用模板和动态数据结合,生成一个html的静态页面,实现动态页面静态化的一个操作,静态的html页面可以减少用户查询对于数据库的压力,适合在不经常变动的数据的地方
在java使用freemarker做静态化创建一个springboot项目
pom文件中导入依赖
org.springframework.boot spring-boot-starter-freemarker org.springframework.boot spring-boot-starter-web org.projectlombok lombok 1.18.2 commons-io commons-io 2.9.0 org.springframework.boot spring-boot-devtools runtime true org.springframework.boot spring-boot-starter-test test
在resources文件下创建templates包
在templates中创建test.flt模板文件
Hello World!
Hello ${name}!
然后yml文件配置
server:
port: 8088 #服务端口
spring:
application:
name: test-freemarker #指定服务名
freemarker:
cache: false #关闭模板缓存,方便测试
settings:
template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
classic_compatible: true #处理空值
datetime_format: yyyy-MM-dd HH:mm
suffix: .ftl #文件后缀
template-loader-path: #模板文件路径
- classpath:/templates
创建一个存数据的对象
@Data
@ToString
public class Student {
private String name;//姓名
private int age;//年龄
private Date birthday;//生日
private Float money;//钱包
private List friends;//朋友列表
private Student bestFriend;//最好的朋友
}
在测试用例里面定义一个拿数据的方法
private static Map getMap(){
Map map = new HashMap<>();
// map就是freemarker模板所使用的数据
map.put("name","超级攻城狮");
//学生1的信息
Student one = new Student();
one.setName("小明");
one.setAge(18);
one.setMoney(1000.86f);
one.setBirthday(new Date());
//学生2的信息
Student two = new Student();
two.setName("小红");
two.setMoney(200.1f);
two.setAge(19);
two.setBirthday(new Date());
//学生2的朋友列表(学生1)
List friends = new ArrayList<>();
friends.add(one);
//添加学生2的朋友列表
two.setFriends(friends);
//添加学生2的最好朋友学生1
two.setBestFriend(one);
//准备lish数据
List stus = new ArrayList<>();
stus.add(one);//学生1
stus.add(two);//学生2
//向数据模型放数据---------->添加List集合
map.put("stus",stus);
//准备map数据
HashMap stuMap = new HashMap<>();
stuMap.put("stu1",one);
stuMap.put("stu2",two);
//向数据模型放数据---------->添加Student对象
map.put("stu1",one);
//向数据模型放数据---------->添加Map集合
map.put("stuMap",stuMap);
map.put("num",1234567);
return map;
}
使用模板文件
@Test
public void test1() throws IOException, TemplateException {
//1.根据模板版本创建配置类
Configuration configuration = new Configuration(Configuration.getVersion());
//2.设置模板所在的绝对路径
String classPath = this.getClass().getResource("/").getPath();
System.out.println("classPath:"+classPath);
configuration.setDirectoryForTemplateLoading(new File(classPath+"/templates/"));
//3.设置字符集
configuration.setDefaultEncoding("utf-8");
//4.加载模板
Template template = configuration.getTemplate("test.ftl");
//5.创建模型数据
Map map=getMap();
//6.静态化:将数据填充到模板
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
//8.利用IOUtils将输出的内容加载到流
InputStream is = IOUtils.toInputStream(content);
//9.定义输出路径
FileOutputStream fos = new FileOutputStream(new File("D:\test.html"));
//10.输出
IOUtils.copy(is,fos);
//11.关流
fos.close();
is.close();
}
使用string字符模板
@Test
public void testGenerateHtmlByString() throws IOException, TemplateException
{
// 1.根据模板版本创建配置类
Configuration configuration = new
Configuration(Configuration.getVersion());
// 2.模板内容,这里测试时使用简单的字符串作为模板
String templateString="" +
"n" +
" n" +
" n" +
" 名称:${name}n" +
" n" +
"";
// 3.模板加载器,根据字符串加载模板
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
stringTemplateLoader.putTemplate("template",templateString);
configuration.setTemplateLoader(stringTemplateLoader);
// 4.获得模板
Template template = configuration.getTemplate("template","utf-8");
// 5.数据模型
Map map = getMap();
// 6.静态化
String content =
FreeMarkerTemplateUtils.processTemplateIntoString(template,map);
// 7.dubug测试静态化内容
System.out.println(content);
// 8.输出文件
InputStream inputStream = IOUtils.toInputStream(content);
FileOutputStream fileOutputStream = new FileOutputStream(new
File("D:\testByString.html"));
IOUtils.copy(inputStream,fileOutputStream);
// 9.关流
inputStream.close();
fileOutputStream.close();
}
页面预览和页面发布
mongdb的gridfs
mongdb中的gridfs可以用来存储不超过16mb的文件,会将文件分成多块存入,拿的时候会进行组合
定义一个config包在springboot启动类的同包下
在创建Mongdb的配置类
@Configuration
public class MongoConfig {
@Value("${spring.data.mongodb.database}")
String db;
@Bean
public GridFSBucket getGridFSBucket(MongoClient mongoClient){
//获取数据库
MongoDatabase database = mongoClient.getDatabase(db);
GridFSBucket bucket = GridFSBuckets.create(database);
return bucket;
}
}
使用自动装配拿到gridfs
通过fridfsbucket对象去下载存在gridfs里面的文件
页面预览使用gridfs对模板进行存储
给页面设置一个数据接口的字段,存入一个可以调用拿到数据的http请求,我们后台会去用RestTemplate去请求这个接口,拿到数据,再去mongdb使用gridfs查询到模板文件,使用freemarker生成一个html页面InputStream,返回给前台
页面发布在页面预览的基础上,将生成的html放入我们的前端项目路径,替换里面的html页面
`



