1. 程式人生 > >Freemarker頁面靜態化技術

Freemarker頁面靜態化技術

view color 分享圖片 doctype nbsp fig ces 取數據 doc

初步理解:

技術分享圖片

架構優化:

技術分享圖片技術分享圖片技術分享圖片

技術分享圖片技術分享圖片技術分享圖片

技術分享圖片

靜態頁面的訪問速度優於從緩存獲取數據的動態頁面的訪問速度;

Freemarker:

導包技術分享圖片

模板:hello.ftl

技術分享圖片
 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>freemarker入門案例</title>
 6 </head>
 7 <body>
 8 <h1>獲取字符類型數據:${hello}</h1>
 9 </body>
10 </html>
View Code

生成靜態頁面:

技術分享圖片
 1     @Test
 2     public void test01() throws Exception{
 3         //創建freemarker核心配置對象
 4         Configuration cf = new Configuration(Configuration.getVersion());
 5         //指定模版文件存儲路徑
 6         cf.setDirectoryForTemplateLoading(new File("E:\\folder\\template"));
 7         //
指定模版文件編碼 8 cf.setDefaultEncoding("UTF-8"); 9 //讀取模版文件,獲取模版對象 10 Template template = cf.getTemplate("hello.ftl"); 11 //準備數據 12 Map<String, Object> maps = new HashMap<String, Object>(); 13 maps.put("hello", "freemarker很簡單,非常簡單!"); 14 // maps.put("hello", "sdfs");
15 // maps.put("hello", 0.23); 16 //創建一個輸出流對象,把生成html頁面寫入磁盤 17 Writer out = new FileWriter(new File("E:\\folder\\template\\out\\first.html")); 18 //生成HTML頁面 19 template.process(maps, out); 20 //關閉 21 out.close(); 22 }
View Code

spring整合freemarker:

1 <!-- freeemarker交給spring管理 -->
2     <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
3         <property name="templateLoaderPath" value="/WEB-INF/fm/"></property>
4         <property name="defaultEncoding" value="UTF-8"></property>
5     </bean>

Freemarker頁面靜態化技術