本文源码:GitHub·点这里 || GitEE·点这里html
静态页面java
即静态网页,指已经装载好内容HTML页面,无需通过请求服务器数据和编译过程,直接加载到客户浏览器上显示出来。通俗的说就是生成独立的HTML页面,且不与服务器进行数据交互。git
优缺点描述:github
动态页面算法
指跟静态网页相对的一种网页编程技术,页面的内容须要请求服务器获取,在不考虑缓存的状况下,服务接口的数据变化,页面加载的内容也会实时变化,显示的内容倒是随着数据库操做的结果而动态改变的。spring
优缺点描述:数据库
动态页面和静态页面有很强的相对性,对比之下也比较好理解。编程
动态页面静态化处理的应用场景很是多,例如:浏览器
静态化技术的根本:提示服务的响应速度,或者说使响应节点提早,如通常的流程,页面(客户端)请求服务,服务处理,响应数据,页面装载,一系列流程走下来不只复杂,并且耗时,若是基于静态化技术处理以后,直接加载静态页面,好了请求结束。缓存
静态页面转换是一个相对复杂的过程,其中核心流程以下:
主流程大体如上,若是数据接口响应参数有变,则须要从新生成静态页,因此在数据的加载实时性上面会低不少。
FreeMarker是一款模板引擎:即一种基于模板和要改变的数据,并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
这里既使用FreeMarker开发的模板样式。
<html> <head> <title>PageStatic</title> </head> <body> 主题:${myTitle}<br/> <#assign text="{'auth':'cicada','date':'2020-07-16'}" /> <#assign data=text?eval /> 做者:${data.auth} 日期:${data.date}<br/> <table class="table table-striped table-hover table-bordered" id="editable-sample"> <thead> <tr> <th>规格描述</th> <th>产品详情</th> </tr> </thead> <tbody> <#list tableList as info> <tr class=""> <td>${info.desc}</td> <td><img src="${info.imgUrl}" height="80" width="80"></td> </tr> </#list> </tbody> </table><br/> <#list imgList as imgIF> <img src="${imgIF}" height="300" width="500"> </#list> </body> </html>
FreeMarker的语法和原有的HTML语法基本一致,可是具备一套本身的数据处理标签,用起来不算复杂。
经过解析,把页面模板和数据接口的数据合并到一块儿便可。
@Service public class PageServiceImpl implements PageService { private static final Logger LOGGER = LoggerFactory.getLogger(PageServiceImpl.class) ; private static final String PATH = "/templates/" ; @Override public void ftlToHtml() throws Exception { // 建立配置类 Configuration configuration = new Configuration(Configuration.getVersion()); // 设置模板路径 String classpath = this.getClass().getResource("/").getPath(); configuration.setDirectoryForTemplateLoading(new File(classpath + PATH)); // 加载模板 Template template = configuration.getTemplate("my-page.ftl"); // 数据模型 Map<String, Object> map = new HashMap<>(); map.put("myTitle", "页面静态化(PageStatic)"); map.put("tableList",getList()) ; map.put("imgList",getImgList()) ; // 静态化页面内容 String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map); LOGGER.info("content:{}",content); InputStream inputStream = IOUtils.toInputStream(content,"UTF-8"); // 输出文件 FileOutputStream fileOutputStream = new FileOutputStream(new File("F:/page/newPage.html")); IOUtils.copy(inputStream, fileOutputStream); // 关闭流 inputStream.close(); fileOutputStream.close(); } private List<TableInfo> getList (){ List<TableInfo> tableInfoList = new ArrayList<>() ; tableInfoList.add(new TableInfo(Constant.desc1, Constant.img01)); tableInfoList.add(new TableInfo(Constant.desc2,Constant.img02)); return tableInfoList ; } private List<String> getImgList (){ List<String> imgList = new ArrayList<>() ; imgList.add(Constant.img02) ; imgList.add(Constant.img02) ; return imgList ; } }
生成后的HTML页面直接使用浏览器打开便可,再也不须要依赖任何数据接口服务。
GitHub·地址 https://github.com/cicadasmile/middle-ware-parent GitEE·地址 https://gitee.com/cicadasmile/middle-ware-parent
推荐阅读:微服务架构系列