一、FreeMarker實現網頁靜態化。html
FreeMarker是一個用Java語言編寫的模板引擎,它基於模板來生成文本輸出。FreeMarker與Web容器無關,即在Web運行時,它並不知道Servlet或HTTP。它不只能夠用做表現層的實現技術,並且還能夠用於生成XML,JSP或Java 等。目前企業中:主要用Freemarker作靜態頁面或是頁面展現。java
二、使用freemarker須要的jar。web
a)、把下載到的jar包(freemarker-2.3.23.jar)放到\webapp\WEB-INF\lib目錄下。官方網站:http://freemarker.org/spring
b)、若是使用的是Maven結構,可在pom.xml中引入如下座標。
數組
1 <dependency> 2 <groupId>org.freemarker</groupId> 3 <artifactId>freemarker</artifactId> 4 <version>2.3.23</version> 5 </dependency>
三、Freemarker原理圖。模板 + 數據模型 = 輸出
app
四、freemarker的測試案例以下所示:eclipse
1 package com.taotao.freemarker; 2 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 import java.io.Writer; 7 import java.util.HashMap; 8 import java.util.Map; 9 10 import org.junit.Test; 11 12 import freemarker.template.Configuration; 13 import freemarker.template.Template; 14 import freemarker.template.TemplateException; 15 16 /** 17 * freemarker網頁靜態化 18 * 19 * @ClassName: TaoTaoFreemarker.java 20 * @author: biehl 21 * @since: 2019年9月21日 下午5:46:49 22 * @Copyright: ©2019 biehl 版權全部 23 * @version: 0.0.1 24 * @Description: 25 */ 26 public class StaticPageFreemarker { 27 28 @Test 29 public void freemarkerStaticPage() { 30 try { 31 // 一、建立一個模板文件 32 // 二、建立一個Configuration對象 33 Configuration configuration = new Configuration(Configuration.getVersion()); 34 35 // 三、設置模板所在得路徑 36 configuration.setDirectoryForTemplateLoading( 37 new File("D:\\eclipse\\workspace_taotao\\taotao-item-web\\src\\main\\webapp\\WEB-INF\\ftl")); 38 39 // 四、設置模板得字符集,通常使用utf-8 40 configuration.setDefaultEncoding("utf-8"); 41 42 // 五、使用Configuration對象加載一個模板文件,須要指定模板文件得文件名 43 Template template = configuration.getTemplate("hello.ftl"); 44 45 // 六、建立一個數據集,能夠是pojo也能夠是map,推薦使用map 46 Map data = new HashMap<>(); 47 data.put("hello", "hello fremarker!!!"); 48 49 // 七、建立一個Writer對象,指定輸出文件的路徑以及文件名 50 Writer out = new FileWriter(new File("D:\\biehl\\photo\\out\\hello.txt")); 51 52 // 八、使用模板對象的process方法輸出文件 53 template.process(data, out); 54 55 // 九、關閉流 56 out.close(); 57 } catch (IOException e) { 58 e.printStackTrace(); 59 } catch (TemplateException e) { 60 e.printStackTrace(); 61 } 62 63 } 64 }
相關文件以下所示:webapp
五、fremarker模板的語法學習。maven
1 package com.taotao.freemarker; 2 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 import java.io.Writer; 7 import java.util.ArrayList; 8 import java.util.Date; 9 import java.util.HashMap; 10 import java.util.List; 11 import java.util.Map; 12 13 import org.junit.Test; 14 15 import com.taotao.pojo.Student; 16 17 import freemarker.template.Configuration; 18 import freemarker.template.Template; 19 import freemarker.template.TemplateException; 20 21 /** 22 * freemarker網頁靜態化 23 * 24 * @ClassName: TaoTaoFreemarker.java 25 * @author: biehl 26 * @since: 2019年9月21日 下午5:46:49 27 * @Copyright: ©2019 biehl 版權全部 28 * @version: 0.0.1 29 * @Description: 30 */ 31 public class StaticPageFreemarker { 32 33 @Test 34 public void freemarkerStaticPage() { 35 try { 36 // 一、建立一個模板文件 37 // 二、建立一個Configuration對象 38 Configuration configuration = new Configuration(Configuration.getVersion()); 39 40 // 三、設置模板所在得路徑 41 configuration.setDirectoryForTemplateLoading( 42 new File("D:\\eclipse\\workspace_taotao\\taotao-item-web\\src\\main\\webapp\\WEB-INF\\ftl")); 43 44 // 四、設置模板得字符集,通常使用utf-8 45 configuration.setDefaultEncoding("utf-8"); 46 47 // 五、使用Configuration對象加載一個模板文件,須要指定模板文件得文件名 48 // Template template = configuration.getTemplate("hello.ftl"); 49 Template template = configuration.getTemplate("student.ftl"); 50 51 // 六、建立一個數據集,能夠是pojo也能夠是map,推薦使用map 52 Map data = new HashMap<>(); 53 data.put("hello", "hello fremarker!!!"); 54 Student stu = new Student(1, "小辣椒", 25, "北京市西城區西什庫大街31號院"); 55 // 注意,對象的key就是模板裏面的對應的.前面的對象名稱 56 data.put("student", stu); 57 58 // freemarker遍歷集合對象 59 List<Student> stuList = new ArrayList<Student>(); 60 stuList.add(new Student(1008611, "小辣椒1號", 25, "北京市西城區西什庫大街30號院")); 61 stuList.add(new Student(1008612, "小辣椒2號", 21, "北京市西城區西什庫大街32號院")); 62 stuList.add(new Student(1008613, "小辣椒3號", 22, "北京市西城區西什庫大街33號院")); 63 stuList.add(new Student(1008614, "小辣椒4號", 23, "北京市西城區西什庫大街34號院")); 64 stuList.add(new Student(1008615, "小辣椒5號", 24, "北京市西城區西什庫大街35號院")); 65 stuList.add(new Student(1008616, "小辣椒6號", 20, "北京市西城區西什庫大街36號院")); 66 stuList.add(new Student(1008617, "小辣椒7號", 18, "北京市西城區西什庫大街31號院")); 67 data.put("stuList", stuList); 68 69 // 日期類型的處理 70 data.put("date", new Date()); 71 72 // null值得處理 73 data.put("val", null); 74 75 // 七、建立一個Writer對象,指定輸出文件的路徑以及文件名 76 Writer out = new FileWriter(new File("D:\\biehl\\photo\\out\\student.html")); 77 78 // 八、使用模板對象的process方法輸出文件 79 template.process(data, out); 80 81 // 九、關閉流 82 out.close(); 83 } catch (IOException e) { 84 e.printStackTrace(); 85 } catch (TemplateException e) { 86 e.printStackTrace(); 87 } 88 89 } 90 }
freemarker模板以下所示:學習
1 <html> 2 <head> 3 <title>測試頁面</title> 4 </head> 5 <body> 6 <center> 7 <!-- 1)、freemarker語法-取pojo的屬性 --> 8 學生信息:<br> 9 學號:${student.id}<br> 10 姓名:${student.name}<br> 11 年齡:${student.age}<br> 12 家庭住址:${student.address}<br><br><br> 13 學生列表:<br> 14 <table border="1"> 15 <tr> 16 <th>序號</th> 17 <th>學號</th> 18 <th>姓名</th> 19 <th>年齡</th> 20 <th>家庭住址</th> 21 </tr> 22 <!-- 2)、freemarker語法-list,歷遍集合/數組, --> 23 <#list stuList as stu> 24 <!-- 邏輯運算符(==、!=、||、&&) --> 25 <#if stu_index%2==0> 26 <tr bgcolor="yellow"> 27 <#else> 28 <tr bgcolor="purple"> 29 </#if> 30 <!-- 得到當前迭代的索引x_index --> 31 <td>${stu_index}</td> 32 <td>${stu.id}</td> 33 <td>${stu.name}</td> 34 <td>${stu.age}</td> 35 <td>${stu.address}</td> 36 </tr> 37 </#list> 38 </table> 39 <br><br><br> 40 <!-- 41 默認格式 42 1)、date。 43 cur_time?date 44 2)、datetime。 45 cur_time?datetime 46 3)、time。 47 cur_time?time 48 4)、自定義格式。 49 cur_time?string("yyyy-MM-dd HH:mm:ss") 50 --> 51 年:月:日:${date?date}<br> 52 年:月:日 時:分:秒:${date?datetime}<br> 53 時:分:秒:${date?time}<br> 54 年/月/日:${date?string("yyyy/MM/dd")}<br> 55 日期類型的處理:${date?string("yyyy/MM/dd HH:mm:ss")} 56 <br><br><br> 57 <!-- null值的處理、方式一:null 變 空串 --> 58 方式1、null值的處理:${val!} 59 <br> 60 方式2、爲Null時給默認值 61 ${val!"我是默認值"} 62 <br> 63 方式3、使用if判斷null值: 64 <#if val??> 65 val是有值的. 66 <#else> 67 val值爲null. 68 </#if> 69 <br><br><br> 70 <!-- 將另外一個頁面引入本頁面時可用如下命令完成 --> 71 include標籤測試: 72 <#include "hello.ftl"> 73 </center> 74 </body> 75 </html>
效果以下所示:
六、freemarker與spring整合。因爲使用的maven項目,因此引入相應的依賴jar包。
1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-context-support</artifactId> 4 <version>4.1.3.RELEASE</version> 5 </dependency> 6 <dependency> 7 <groupId>org.freemarker</groupId> 8 <artifactId>freemarker</artifactId> 9 <version>2.3.23</version> 10 </dependency>
在ApplicationContext.xml中添加以下內容:
1 <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 2 <property name="templateLoaderPath" value="/WEB-INF/freemarker/" /> 3 <property name="defaultEncoding" value="UTF-8" /> 4 </bean>
整合代碼實現以下所示:
1 package com.taotao.item.controller; 2 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.Writer; 6 import java.util.HashMap; 7 import java.util.Map; 8 9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.stereotype.Controller; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import org.springframework.web.bind.annotation.ResponseBody; 13 import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; 14 15 import freemarker.template.Configuration; 16 import freemarker.template.Template; 17 18 /** 19 * 20 * @ClassName: HtmlGenController.java 21 * @author: biehl 22 * @since: 2019年9月26日 下午8:15:01 23 * @Copyright: ©2019 biehl 版權全部 24 * @version: 0.0.1 25 * @Description: 26 */ 27 @Controller 28 public class HtmlGenController { 29 30 @Autowired 31 private FreeMarkerConfigurer freeMarkerConfigurer; 32 33 @RequestMapping("/genhtml") 34 @ResponseBody 35 public String genHtml() throws Exception { 36 // 生成靜態頁面 37 Configuration configuration = freeMarkerConfigurer.getConfiguration(); 38 Template template = configuration.getTemplate("hello.ftl"); 39 Map data = new HashMap<>(); 40 data.put("hello", "spring freemarker test"); 41 Writer out = new FileWriter(new File("D:/test.html")); 42 template.process(data, out); 43 out.close(); 44 // 返回結果 45 return "OK"; 46 } 47 48 }
待續......