前,我写过一个“WordPress的静态化方法”,使用的是一个名为cos-html-cache的插件实现。
这个插件非常简洁小巧,直接在原网站上生成首页和文章页的html文件,不过,这个插件只支持文章静态化,不支持页面、标签和分类的静态化,之后也再没有过更新。
后来,我想到过利用WordPress插件导出全静态化网站的方法,用这个方法,对于少量文章挺方便,但文章数量一旦多了,就经常出错。
后来,我看到有人在cos-html-cache插件的基础上又开发了一个插件,名叫Super Static Cache,我用了一下,发现其BUG较多,但Rewrite模式是可以正常使用的,在这个模式下,可以将首页、文章页、单页、分类页、Tag页都生成静态化文件,并保存在一个名为super-static-cache的目录下,直接复制这个目录即可得到一个静态化网站。
因此,一个更简单的生成静态化网站的方法来了,先在网站安装Super Static Cache,之后运行一个抓取网站的工具,这类工具很多,例如wget、sitemaps生成器之类的,把整个网站抓一遍(wget还能多生成一份),即可在super-static-cache的目录获取到网站的静态化Html文件。
wget在Windows、Linux、Mac都有,用wget下载网站的命令是:
wget -m 网站地址
phinx是一款支持多种编程语言的文档生成工具,可以由reStructuredText或Markdown文档生成HTML静态网页,并且自动生成索引,可以作为个人网站、博客,或者制作电子教程、书籍等。
1.安装Python
Python官网(https://www.python.org/)下载安装Python3+。
2.安装sphinx
pip install sphinx
3.安装markdown支撑的模块
pip install sphinx-markdown-tables
4.安装主题模板
pip install sphinx-rtd-theme
5.创建项目文件夹test并进入
6.启动Sphinx,输入以下信息
Project name:Python教程
Author name(s):zbxx.net
Project language [en]:zh_CN
启动Sphinx后会在项目文件夹test中创建如下文件结构:
7.编辑 source/conf.py 文件
extensions = ['recommonmark','sphinx_markdown_tables']
html_theme = 'sphinx_rtd_theme'
8.将markdown笔记文件Python.md放到source目录下
9.编辑 source/index.rst 文件,加入python.md
10.生成HTML
make html
生成静态HTML网页文件,位置:build\html,我们就拥有了一个完整的静态网站。
打开index.html预览效果,可以本地使用或上传个人网站。
markdown笔记修改后,需要清空HTML,重新生成。
make clean
之前我介绍了在spring boot中使用thymeleaf模板,这次我会给大家介绍在spring boot中使用freemarker模板技术,同时利用freemarker生成静态html页面。生成静态html页面就能实现网站的静态化进而提高网站的访问速度以及提高SEO能力。
首先在pom.xml中添加依赖
添加依赖
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
application配置
在application.properties中添加freemarker的配置参数
##freemarker spring.freemarker.cache=false spring.freemarker.charset=UTF-8 spring.freemarker.check-template-location=true spring.freemarker.content-type=text/html spring.freemarker.enabled=true spring.freemarker.suffix=.ftl spring.freemarker.template-loader-path=classpath:/templates
Controller和ftl模板
下一步我们就建一个基础Controller类和配套的ftl模板
Controller类
package com.hw.myp2c.common.controller; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import java.io.*; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; @Controller @RequestMapping("") public class MainController { @GetMapping public String main(Model model){ String w="Welcome FreeMarker!"; Map root = new HashMap(); root.put("w",w); model.addAttribute("w","Welcome FreeMarker!"); return "test"; } }
可以看到很简单,跟之前的thymelefa和jsp的没有区别。
freemarker模板
<html> <head> <title>Welcome!</title> <link rel="stylesheet" href="/bootstrap.min.css"> <script src="/lib/jquery.min.js"></script> </head> <body> <h1>Hello ${w}!</h1> </body> </html>
这样之后我们就能完成了基础freemarker的使用,更多的使用参见freemarker官方网站,这里不做过多的描述。
这里我们已经完成了标准的freemarker集成,下面我们将介绍如何利用freemarker生成静态html页面,直接上代码,作为演示我们还是在Controller中完成,在实际应用中我们可以按照自己的实际需要进行封装。
package com.hw.myp2c.common.controller; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import java.io.*; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; @Controller @RequestMapping("") public class MainController { @Resource Configuration cfg; @GetMapping public String main(Model model){ String w="Welcome FreeMarker!"; Map root = new HashMap(); root.put("w",w); freeMarkerContent(root); model.addAttribute("w","Welcome FreeMarker!"); return "test"; } private void freeMarkerContent(Map<String,Object> root){ try { Template temp = cfg.getTemplate("test.ftl"); //以classpath下面的static目录作为静态页面的存储目录,同时命名生成的静态html文件名称 String path=this.getClass().getResource("/").toURI().getPath()+"static/test.html"; Writer file = new FileWriter(new File(path.substring(path.indexOf("/")))); temp.process(root, file); file.flush(); file.close(); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } } }
利用freemarker生成静态页面我理解的流程是这样的
1.利用Configuration读取想生成静态页面的模板,这里是test.ftl
2.解析模板文件,并将模板中的${}!包含的参数替换成真实的数据
3.最终将读取了真实数据的模板生成相应的html文件,并写入指定目录
这样我们就完成了spring boot中使用freemarker模板,并且利用freemarker生成静态html文件
*请认真填写需求信息,我们会在24小时内与您取得联系。