阶段5 3.微服务项目【学成在线】_day04 页面静态化_10-freemarker静态化测试-基于模板文件静态化

把resource拷贝到test目录下

只保留文件夹结构和test1.ftl这个模板文件就可以了。

新建一个包


编写测试类

使用freemaker提供的方法生成静态文件
Configuration是import freemarker.template.Configuration;包下的


手动的设置模板的路径。获取当前类的classPath然后拼上template的路径

抛出异常

获取test1.ftl这个模板

定义获取数据的方法。填充map的数据是从上节课的controller里面复制过来的。单独定义一个getMap的方法来获取。

静态化

processTemplateIntoString方法也要抛出异常。TemplateException

加断点一步步测试

一步步往下走报错。

路径写错了 漏了一个s

复制静态化后的字符串内容。

粘贴到一个编辑器里面

package com.xuecheng.test.freemarker; import com.xuecheng.test.freemarker.model.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import java.io.File;
import java.io.IOException;
import java.util.*; @SpringBootTest
@RunWith(SpringRunner.class)
public class FreemarkerTest { //测试静态化,基于ftl模板文件生成html文件
@Test
public void testGenerateHtml() throws IOException, TemplateException {
//定义配置类
Configuration configuration = new Configuration(Configuration.getVersion());
//定义模板
//得到classpath的路径
String classpath = this.getClass().getResource("/").getPath();
configuration.setDirectoryForTemplateLoading(new File(classpath+"/templates/"));
//获取模板文件内容。
Template template = configuration.getTemplate("test1.ftl");
//定义数据模型。
Map map=getMap();
//静态化
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
System.out.println(content);
}
public Map getMap(){
Map<String,Object> map=new HashMap<>();
map.put("name","黑马程序员");
Student stu1 = new Student();
stu1.setName("小明");
stu1.setAge();
stu1.setMoney(1000.86f);
stu1.setBirthday(new Date());
Student stu2 = new Student();
stu2.setName("小红");
stu2.setMoney(200.1f);
stu2.setAge();
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); map.put("point",);
return map;
}
}
FreemarkerTest.cs
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello World!</title>
</head>
<body> <table>
<tr>
<td>序号</td>
<td>名字</td>
<td>年龄</td>
<td>金额</td>
</tr>
<tr>
<td></td>
<td style="background-color:cornflowerblue">小明</td>
<td></td>
<td style="background-color: cornflowerblue",000.86</td>
<td>2019年11月02日</td>
</tr>
<tr>
<td></td>
<td >小红</td>
<td></td>
<td 200.1</td>
<td>2019年11月02日</td>
</tr>
</table> <br/>
遍历数据模型中的stuMap(map)数据
<br/>
姓名:小明<br/>
年龄:<br/>
姓名:小明<br/>
年龄:<br/>
<br/>
遍历map中的key.stuMap?keys就是key列表(是一个list)
<br/>
姓名:小红<br/>
年龄:<br/>
姓名:小明<br/>
年龄:<br/> <br/>
学生的个数: <br/> <br/>
开户行:工商银行 账号:
</body>
</html>
生成的静态页内容
生成静态页
用一个很有名的IOUtil。是这个包下的:org.apache.commons.io.IOUtils;

上面定义输入流,下面定义输出流。第三行输入流拷贝到输出流

拷贝完成后就关闭流





最终代码
package com.xuecheng.test.freemarker; import com.xuecheng.test.freemarker.model.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
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.*; @SpringBootTest
@RunWith(SpringRunner.class)
public class FreemarkerTest { //测试静态化,基于ftl模板文件生成html文件
@Test
public void testGenerateHtml() throws IOException, TemplateException {
//定义配置类
Configuration configuration = new Configuration(Configuration.getVersion());
//定义模板
//得到classpath的路径
String classpath = this.getClass().getResource("/").getPath();
configuration.setDirectoryForTemplateLoading(new File(classpath+"/templates/"));
//获取模板文件内容。
Template template = configuration.getTemplate("test1.ftl");
//定义数据模型。
Map map=getMap();
//静态化
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
// System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content);
FileOutputStream outputStream = new FileOutputStream(new File("d:/test1.html"));
//输出文件
IOUtils.copy(inputStream,outputStream);
inputStream.close();
outputStream.close();
}
public Map getMap(){
Map<String,Object> map=new HashMap<>();
map.put("name","黑马程序员");
Student stu1 = new Student();
stu1.setName("小明");
stu1.setAge();
stu1.setMoney(1000.86f);
stu1.setBirthday(new Date());
Student stu2 = new Student();
stu2.setName("小红");
stu2.setMoney(200.1f);
stu2.setAge();
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); map.put("point",);
return map;
}
}
Configuration
阶段5 3.微服务项目【学成在线】_day04 页面静态化_10-freemarker静态化测试-基于模板文件静态化的更多相关文章
- 阶段5 3.微服务项目【学成在线】_day04 页面静态化_16-页面静态化-模板管理-模板制作
这是轮播图的原始文件 运行门户需要把 nginx启动起来 单独运行轮播图.把里面的css的引用都加上网址的url 这就是单独访问到的轮播图的效果 轮播图模板的地址: 阶段5 3.微服务项目[学成在线] ...
- 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_16-CMS前端工程创建-导入系统管理前端工程
提供了基于脚手架封装好的前端工程 H:\BaiDu\黑马传智JavaEE57期 2019最新基础+就业+在职加薪\阶段5 3.微服务项目[学成在线]·\day02 CMS前端开发\资料\xc-ui-p ...
- 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_03-用户认证技术方案-Oauth2协议
2.2 Oauth2认证 2.2.1 Oauth2认证流程 第三方认证技术方案最主要是解决认证协议的通用标准 问题,因为要实现 跨系统认证,各系统之间要遵循一定的 接口协议. OAUTH协议为用户资源 ...
- 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_01-用户认证需求分析
1.1 用户认证与授权 截至目前,项目已经完成了在线学习功能,用户通过在线学习页面点播视频进行学习.如何去记录学生的学习过程 呢?要想掌握学生的学习情况就需要知道用户的身份信息,记录哪个用户在什么时间 ...
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_05-Feign远程调用-客户端负载均衡介绍
2 Feign远程调用 在前后端分离架构中,服务层被拆分成了很多的微服务,服务与服务之间难免发生交互,比如:课程发布需要调用 CMS服务生成课程静态化页面,本节研究微服务远程调用所使用的技术. 下图是 ...
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_02-Eureka注册中心-搭建Eureka单机环境
我们先搭建单机环境 govern是治理的意思, 这样就把工程创建好了 创建包 创建SpringBoot的启动类. 在父工程里面已经确定了Spring Cloud的版本了.相当于锁定了版本 接下里只需要 ...
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_04-Eureka注册中心-将服务注册到Eureka Server
cms相当于客户端 配置客户端的信息 后面加逗号分隔开 50102表示向两台eureka服务上报服务,如果有一台死掉了 那么还可以上另外的一台去注册服务 直接把ip注册到eureka 启动类加注解 重 ...
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_01-Eureka注册中心-Eureka介绍
1 Eureka注册中心 1.1 需求分析 在前后端分离架构中,服务层被拆分成了很多的微服务,微服务的信息如何管理?Spring Cloud中提供服务注册中 心来管理微服务信息. 为什么 要用注册中心 ...
- 阶段5 3.微服务项目【学成在线】_day18 用户授权_03-方法授权-jwt令牌包含权限
修改认证服务的UserDetailServiceImpl类,下边的代码中 permissionList列表中存放了用户的权限, 并且将权限标识按照中间使用逗号分隔的语法组成一个字符串,最终提供给Spr ...
随机推荐
- Django drf:分页器详解
一.简单分页(查看第n页,每页显示N条) 二.偏移分页(在第n个位置,向后查看n条数据) 三.CursorPagination(加密分页,只能看上一页和下一页,速度快) 一.简单分页(查看第n页,每页 ...
- OSI七层与TCP/IP五层
OSI七层与TCP/IP五层网络架构详解 OSI和TCP/IP是很基础但又非常重要的网络基础知识,理解得透彻对运维工程师来说非常有帮助.今天偶又复习了一下: (1)OSI七层模型 OSI中的层 功能 ...
- ]Kinect for Windows SDK开发入门(六):骨骼追踪基础 上
原文来自:http://www.cnblogs.com/yangecnu/archive/2012/04/06/KinectSDK_Skeleton_Tracking_Part1.html Kinec ...
- C++ 输出PPM格式图片文件
PPM简介 学习图形学时为了直观地观察结果,需要输出图片,而PPM是一种最简单的图片格式,非常适合新手使用. PPM文件的内容大概是这样的: 第一行固定为P3,代表写入的是PPM格式的RGB图像,除此 ...
- GPU显存释放
一.当程序没有运行,但GPU仍被占用, 可通过nvidia-smi查看,被占用的pid是什么 或通过sudo fuser -v /dev/nvidia* #查找占用GPU资源的PID 然后采用kill ...
- tensorflow实战笔记(18)----textCNN
一.import 包 import os import pandas as pd import csv import time import datetime import numpy as np i ...
- Paper Reading:DetNet
论文:DetNet: A Backbone network for Object Detection 发表时间:2018 发表作者:(Face++)Chao Peng, Gang Yu (Tsingh ...
- devops发展历程
第一阶段:只有 Dev ,没有 Ops ,Dev 是全栈工程师 如何理解?最初的时候,产品和业务形态都处于摸索期,业务复杂度不高,访问量不大,软件能够尽快跑起来推向市场是最重要的,所以架构上不设计的很 ...
- Java基础- TreeMap<Person,Integer> 实现的定制排序(Comparator 接口)
测试内容: TreeMap<Person,Integer> 的两级排序 1.先按Person.id 从小到大 (需要把int类型转换成包装类 Integer, 才可以使用p1.getId( ...
- sql语句中的占位符?有什么作用
String sql = "SELECT userid,name FROM tuser WHERE userid=? AND password=?" ; pstmt = conn. ...