静态页面缓存(thymeleaf模板writer)


//前端html
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="../static-recourse/css/home.css" />
<link rel="stylesheet" href="../static-recourse/layui/layui.css" />
<link rel="stylesheet" href="../static-recourse/css/guide.css"/>
<script src="../static-recourse/js/jquery.min.js"></script>
<title>网站首页</title>
<style>
.query .notFount {
display: none;
}

.unitList p a {
margin-bottom: -4px;
}

.unitList ul li {
margin-left: 32px;
margin-right: 4px;
}

.unitList li p {
margin: 12px auto;
}

.unitInform .unitInf,
.unitInform .printInf {
margin-left: 50px;
}

.unitInform .unitInf p {
margin-top: 18px;
font-size: 12px;
}

.unitInform h3 {
margin-top: 30px;
}

.unitInform .printList p {
margin-top: 20px;
font-size: 12px;
}

.unitInform input[type="button"] {
margin-bottom: -6px;
}

.unitInform {
display: none;
}
.rules{
49%;
float: left;
}
.standar{
49%;
float: left;
margin-left: 18px;
}
.leftspan{
49%;
float: left;
}
</style>
</head>

<body>
<div class="content">
<div class="content layui-row layui-col-space9">
<div class="content_w serviceTable type_1_content rules">
<span class="title">新闻资讯</span>
<table class="layui-table service">
<colgroup>
<col width="250">
<col width="50">
<col>
</colgroup>
<!--<thead>
<tr>
<th>标题</th>
<th>时间</th>
</tr>
</thead>-->
<tbody id="guidance-list" class="guidance-list">
<tr th:each="var,varStat : ${xwzxList}" th:if="${varStat.index} lt 5">
<td class="titleTr">
<span th:if="${var.isTop}"><img src="../static-recourse/img/pin_1.gif" style="padding-right: 5px;"/></span>
<span th:if="${not var.isTop}"><img src="../static-recourse/img/folder_new.gif" style="padding-right: 5px;"/></span>
<a th:utext="${var.title}" th:attr="data-id=${var.id},title=${var.title}">关于推进全国统一 “多证合一”改革的意见</a>
</td>
<td class="checkTr" th:text="${#dates.format(var.updateTime,'yyyy-MM-dd')}">2019-01-01</td>
</tr>
<tr th:each="i:${#numbers.sequence(1,5-xwzxList.size())}" th:if="${xwzxList.size()} lt 5">
<td class="titleTr"><a>&nbsp;</a></td>
<td class="checkTr">&nbsp;</td>
</tr>
<tr>
<td class="titleTr"><a href="new_guide.html?flag=0" target="_parent">更多>></a></td>
<td class="checkTr">&nbsp;</td>
</tr>
</tbody>
</table>
</div>


<div class="content_w serviceTable type_2_content standar">
<span class="title">政策法规</span>
<table class="layui-table service">
<colgroup>
<col width="250">
<col width="50">
<col>
</colgroup>
<!--<thead>
<tr>
<th>标题</th>
<th>时间</th>
</tr>
</thead>-->
<tbody id="guidance-list" class="guidance-list">
<tr th:each="var,varStat : ${zcfgList}" th:if="${varStat.index} lt 5">
<td class="titleTr">
<span th:if="${var.isTop}"><img src="../static-recourse/img/pin_1.gif" style="padding-right: 5px;"/></span>
<span th:if="${not var.isTop}"><img src="../static-recourse/img/folder_new.gif" style="padding-right: 5px;"/></span>
<a th:utext="${var.title}" th:attr="data-id=${var.id},title=${var.title}">关于推进全国统一 “多证合一”改革的意见</a>
</td>
<td class="checkTr" th:text="${#dates.format(var.updateTime,'yyyy-MM-dd')}">2019-01-01</td>
</tr>
<tr th:each="i:${#numbers.sequence(1,5-zcfgList.size())}" th:if="${zcfgList.size()} lt 5">
<td class="titleTr"><a>&nbsp;</a></td>
<td class="checkTr">&nbsp;</td>
</tr>
<tr>
<td class="titleTr"><a href="new_guide.html?flag=1" target="_parent">更多>></a></td>
<td class="checkTr">&nbsp;</td>
</tr>
</tbody>
</table>
</div>

<div class="content_w serviceTable type_2_content leftspan">
<span class="title">行业标准</span>
<table class="layui-table service">
<colgroup>
<col width="250">
<col width="50">
<col>
</colgroup>
<!--<thead>
<tr>
<th>标题</th>
<th>时间</th>
</tr>
</thead>-->
<tbody id="guidance-list" class="guidance-list">
<tr th:each="var,varStat : ${hybzList}" th:if="${varStat.index} lt 5">
<td class="titleTr">
<span th:if="${var.isTop}"><img src="../static-recourse/img/pin_1.gif" style="padding-right: 5px;"/></span>
<span th:if="${not var.isTop}"><img src="../static-recourse/img/folder_new.gif" style="padding-right: 5px;"/></span>
<a th:utext="${var.title}" th:attr="data-id=${var.id},title=${var.title}">关于推进全国统一 “多证合一”改革的意见</a>
</td>
<td class="checkTr" th:text="${#dates.format(var.updateTime,'yyyy-MM-dd')}">2019-01-01</td>
</tr>
<tr th:each="i:${#numbers.sequence(1,5-hybzList.size())}" th:if="${hybzList.size()} lt 5">
<td class="titleTr"><a>&nbsp;</a></td>
<td class="checkTr">&nbsp;</td>
</tr>
<tr>
<td class="titleTr"><a href="new_guide.html?flag=2" target="_parent">更多>></a></td>
<td class="checkTr">&nbsp;</td>
</tr>
</tbody>
</table>
</div>


<div class="content_w serviceTable type_2_content standar">
<span class="title">知识园地</span>
<table class="layui-table service">
<colgroup>
<col width="250">
<col width="50">
<col>
</colgroup>
<!--<thead>
<tr>
<th>标题</th>
<th>时间</th>
</tr>
</thead>-->
<tbody id="guidance-list" class="guidance-list">
<tr th:each="var,varStat : ${zsydList}" th:if="${varStat.index} lt 5">
<td class="titleTr">
<span th:if="${var.isTop}"><img src="../static-recourse/img/pin_1.gif" style="padding-right: 5px;"/></span>
<span th:if="${not var.isTop}"><img src="../static-recourse/img/folder_new.gif" style="padding-right: 5px;"/></span>
<a th:utext="${var.title}" th:attr="data-id=${var.id},title=${var.title}">关于推进全国统一 “多证合一”改革的意见</a>
</td>
<td class="checkTr" th:text="${#dates.format(var.updateTime,'yyyy-MM-dd')}">2019-01-01</td>
</tr>
<tr th:each="i:${#numbers.sequence(1,5-zsydList.size())}" th:if="${zsydList.size()} lt 5">
<td class="titleTr"><a>&nbsp;</a></td>
<td class="checkTr">&nbsp;</td>
</tr>
<tr>
<td class="titleTr"><a href="new_guide.html?flag=3" target="_parent">更多>></a></td>
<td class="checkTr">&nbsp;</td>
</tr>
</tbody>
</table>
</div>
</div>


</div>
</body>
<script th:inline="javascript">
   // 后台数据传输到js变量中
   var down_url = [[${downUrl}]];
   var arry = [];
   arry = [[${FileList}]];
// 查看每条记录
$(".guidance-list").on('click', 'a', function () {
var id = $(this).attr("data-id");
if(id){
window.open('details/'+id+'.html','_blank');// 详情跳转
}
})
</script>
</html>

//END-前端html

//后端写操作
package
com.dhht.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import java.io.File;
import java.io.PrintWriter;
import java.util.Map;

/**
* @Author: sh
* @Description: PageCacheService
* @Date: 14:25 2019/9/24
*/
@Slf4j
@Service
public class PageCacheService {

static String CACHE_FILE_SUFFIX = ".html";

@Value("${regulation_path}")
String REGULATION_PATH;// 缓存页面磁盘路径

@Value("${regulation_temp_source}")
String REGULATION_TEMP_SOURCE;

@Resource
private TemplateEngine templateEngine;

/**
* 模板数据填充
*
* @param valsMap
* @param templateId
*/
public void setValToTemplat(Map<String, Object> valsMap, String templateId) {
setVals(valsMap,templateId,templateId,REGULATION_PATH);
}

/**
* 数据填充
*
* @param val 填充值
* @param tempId 模板id
* @param regulationId 资讯id
* @param tarDir 本地磁盘目录
*/
public void setVals(Map<String, Object> val, String tempId, String regulationId, String tarDir) {
PrintWriter writer = null;
try {
Context context = new Context();
context.setVariables(val);
File dirPath = new File(tarDir);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
File uploadFile = new File(tarDir, regulationId + CACHE_FILE_SUFFIX);// ResourceUtils.getFile("classpath:templates/" + templateId + ".html");
if (uploadFile.exists()) {
uploadFile.delete();
}
writer = new PrintWriter(uploadFile, "UTF-8");
// 生成html,第一个参数是thymeleaf页面下的原型名称
templateEngine.process(tempId, context, writer);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
if(null!=writer){
writer.close();
}
}
}
}
package com.dhht.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import java.io.File;
import java.io.PrintWriter;
import java.util.Map;

/**
* @Author: sh
* @Description: PageCacheService
* @Date: 14:25 2019/9/24
*/
@Slf4j
@Service
public class PageCacheService {

static String CACHE_FILE_SUFFIX = ".html";

@Value("${regulation_path}")
String REGULATION_PATH;// 缓存页面磁盘路径

@Value("${regulation_temp_source}")
String REGULATION_TEMP_SOURCE;

@Resource
private TemplateEngine templateEngine;

/**
* 模板数据填充
*
* @param valsMap
* @param templateId
*/
public void setValToTemplat(Map<String, Object> valsMap, String templateId) {
setVals(valsMap,templateId,templateId,REGULATION_PATH);
}

/**
* 数据填充
*
* @param val 填充值
* @param tempId 模板id
* @param regulationId 资讯id
* @param tarDir 本地磁盘目录
*/
public void setVals(Map<String, Object> val, String tempId, String regulationId, String tarDir) {
PrintWriter writer = null;
try {
Context context = new Context();
context.setVariables(val);
File dirPath = new File(tarDir);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
File uploadFile = new File(tarDir, regulationId + CACHE_FILE_SUFFIX);// ResourceUtils.getFile("classpath:templates/" + templateId + ".html");
if (uploadFile.exists()) {
uploadFile.delete();
}
writer = new PrintWriter(uploadFile, "UTF-8");
// 生成html,第一个参数是thymeleaf页面下的原型名称
templateEngine.process(tempId, context, writer);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
if(null!=writer){
writer.close();
}
}
}
}

// 后台数据设置到js变量中var down_url = [[${downUrl}]];//"http://127.0.0.1:8080/downFile";var arry = [];arry = [[${FileList}]];

原文地址:https://www.cnblogs.com/sung1024/p/11700395.html