前,我寫過一個“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小時內與您取得聯系。