使用Freemarker實作網頁靜态化
2017年08月20日 20:45:51
閱讀數:1981
1.1. 什麼是freemarker
FreeMarker是一個用Java語言編寫的模闆引擎,它基于模闆來生成文本輸出。FreeMarker與Web容器無關,即在Web運作時,它并不知道Servlet或HTTP。它不僅可以用作表現層的實作技術,而且還可以用于生成XML,JSP或Java 等。
目前企業中:主要用Freemarker做靜态頁面或是頁面展示
1.2. Freemarker的使用方法
把freemarker的jar包添加到工程中。
Maven工程添加依賴
[html] view plain copy
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>2.3.23</version>
- </dependency>
原理:
使用步驟:
第一步:建立一個Configuration對象,直接new一個對象。構造方法的參數就是freemarker對于的版本号。
第二步:設定模闆檔案所在的路徑。
第三步:設定模闆檔案使用的字元集。一般就是utf-8.
第四步:加載一個模闆,建立一個模闆對象。
第五步:建立一個模闆使用的資料集,可以是pojo也可以是map。一般是Map。
第六步:建立一個Writer對象,一般建立一FileWriter對象,指定生成的檔案名。
第七步:調用模闆對象的process方法輸出檔案。
第八步:關閉流。
[java] view plain copy
- @Test
- public void genFile() throws Exception {
- // 第一步:建立一個Configuration對象,直接new一個對象。構造方法的參數就是freemarker對于的版本号。
- Configuration configuration = new Configuration(Configuration.getVersion());
- // 第二步:設定模闆檔案所在的路徑。
- configuration.setDirectoryForTemplateLoading(new File("D:/workspaces-itcast/term197/taotao-item-web/src/main/webapp/WEB-INF/ftl"));
- // 第三步:設定模闆檔案使用的字元集。一般就是utf-8.
- configuration.setDefaultEncoding("utf-8");
- // 第四步:加載一個模闆,建立一個模闆對象。
- Template template = configuration.getTemplate("hello.ftl");
- // 第五步:建立一個模闆使用的資料集,可以是pojo也可以是map。一般是Map。
- Map dataModel = new HashMap<>();
- //向資料集中添加資料
- dataModel.put("hello", "this is my first freemarker test.");
- // 第六步:建立一個Writer對象,一般建立一FileWriter對象,指定生成的檔案名。
- Writer out = new FileWriter(new File("D:/temp/term197/out/hello.html"));
- // 第七步:調用模闆對象的process方法輸出檔案。
- template.process(dataModel, out);
- // 第八步:關閉流。
- out.close();
- }
1.3. 模闆的文法
1.3.1. 通路map中的key
${key}
1.3.2. 通路pojo中的屬性
Student對象。學号、姓名、年齡
${key.property}
1.3.3. 取集合中的資料
<#list studentList asstudent>
${student.id}/${studnet.name}
</#list>
循環使用格式:
<#list 要循環的資料as循環後的資料>
1.3.4. 取循環中的下标
<#list studentList as student>
${student_index}
1.3.5. 判斷
<#if student_index % 2 == 0>
<#else>
</#if>
1.3.6. 日期類型格式化
直接取值:${date}(date是屬性名)如果傳來的是一個Date型資料會報錯
${date?date} 2016-9-13
${date?time} 17:53:55
${date?datetime} 2016-9-13 17:53:55
1.3.7. Null值的處理
如果直接取一個不存在的值(值為null)時會報異常
${aaa}
處理: ${aaa!”預設值”}或者${aaa! }代表空字元串
1.3.8. Include标簽
<#include “模闆名稱”>
(相當于jstl中的包含)
1.4. Freemarker整合spring
引入jar包:
Freemarker的jar包
1.4.1. 建立整合spring的配置檔案
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
- http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <beanidbeanid="freemarkerConfig"
- class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
- <propertynamepropertyname="templateLoaderPath"value="/WEB-INF/ftl/"/>
- <propertynamepropertyname="defaultEncoding"value="UTF-8"/>
- </bean>
- </beans>
需要編寫一Controller進行測試
1.4.2. Controller
請求的url:/genhtml
參數:無
傳回值:ok (String, 需要使用@ResponseBody)
業務邏輯:
1、從spring容器中獲得FreeMarkerConfigurer對象。
2、從FreeMarkerConfigurer對象中獲得Configuration對象。
3、使用Configuration對象獲得Template對象。
4、建立資料集
5、建立輸出檔案的Writer對象。
6、調用模闆對象的process方法,生成檔案。
7、關閉流。
加載配置檔案:
- @Controller
- publicclass HtmlGenController {
- @Autowired
- private FreeMarkerConfigurerfreeMarkerConfigurer;
- @RequestMapping("/genhtml")
- @ResponseBody
- public String genHtml()throws Exception {
- // 1、從spring容器中獲得FreeMarkerConfigurer對象。
- // 2、從FreeMarkerConfigurer對象中獲得Configuration對象。
- Configuration configuration = freeMarkerConfigurer.getConfiguration();
- // 3、使用Configuration對象獲得Template對象。
- // 4、建立資料集
- dataModel.put("hello","1000");
- // 5、建立輸出檔案的Writer對象。
- Writer out = new FileWriter(new File("D:/temp/term197/out/spring-freemarker.html"));
- // 6、調用模闆對象的process方法,生成檔案。
- // 7、關閉流。
- return"OK";
- }