文章目录
1. 页面静态化需求
2. FreeMarker研究
2.1 依赖与配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
server:
port: 8088
spring:
application:
name: xc-test-freemarker
# mongodb config
freemarker:
cache: false #关闭模板缓存,方便测试
settings:
template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
2.2 freemarker总结
https://www.cnblogs.com/forever2h/p/6952833.html
2.3 静态化测试
@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<String, Object> map = new HashMap<>();
map.put("name", "黑马程序员");
//静态化
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
//静态化内容
System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content);
//输出文件
FileOutputStream fileOutputStream = new FileOutputStream(new File("e:/test1.html"));
int copy = IOUtils.copy(inputStream, fileOutputStream);
}
//基于模板字符串生成静态化文件
@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", "某马程序员");
//静态化
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
//静态化内容
System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content);
//输出文件
FileOutputStream fileOutputStream = new FileOutputStream(new File("E:/test1.html"));
IOUtils.copy(inputStream, fileOutputStream);
}
}
3.页面静态化
3.1 模型
3.2 远程请求
SpringMVC提供 RestTemplate请求http接口,RestTemplate的底层可以使用第三方的http客户端工具实现http 的请求,常用的http客户端工具有Apache HttpClient、OkHttpClient等,本项目使用OkHttpClient完成http请求,原因也是因为它的性能比较出众。
3.3 模板管理
3.4 GridFS研究
GridFS是MongoDB提供的用于持久化存储文件的模块,CMS使用MongoDB存储数据,使用GridFS可以快速集成
开发。
它的工作原理是:
在GridFS存储文件是将文件分块存储,文件会按照256KB的大小分割成多个块进行存储,GridFS使用两个集合(collection)存储文件,一个集合是chunks, 用于存储文件的二进制数据;一个集合是files,用于存储文件的元数据信息(文件名称、块大小、上传时间等信息)。从GridFS中读取文件要对文件的各各块进行组装、合并。
关键类
GridFsTemplate
GridFSBucket
4. 页面预览
4.1 流程
4.2 关键代码
@GetMapping("/cms/preview/{pageId}")
public void preview(@PathVariable("pageId") String pageId) {
String pageHtml = pageService.getPageHtml(pageId);
if (StringUtils.isNotEmpty(pageHtml)) {
ServletOutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
outputStream.write(pageHtml.getBytes("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}