天天看點

【轉】使用Freemarker實作網頁靜态化

使用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

  1. <dependency>  
  2.   <groupId>org.freemarker</groupId>  
  3.   <artifactId>freemarker</artifactId>  
  4.   <version>2.3.23</version>  
  5. </dependency>  

原理:

使用步驟:

第一步:建立一個Configuration對象,直接new一個對象。構造方法的參數就是freemarker對于的版本号。

第二步:設定模闆檔案所在的路徑。

第三步:設定模闆檔案使用的字元集。一般就是utf-8.

第四步:加載一個模闆,建立一個模闆對象。

第五步:建立一個模闆使用的資料集,可以是pojo也可以是map。一般是Map。

第六步:建立一個Writer對象,一般建立一FileWriter對象,指定生成的檔案名。

第七步:調用模闆對象的process方法輸出檔案。

第八步:關閉流。

[java] view plain copy

  1. @Test  
  2.      public void genFile() throws Exception {  
  3.          // 第一步:建立一個Configuration對象,直接new一個對象。構造方法的參數就是freemarker對于的版本号。  
  4.          Configuration configuration = new Configuration(Configuration.getVersion());  
  5.          // 第二步:設定模闆檔案所在的路徑。  
  6.          configuration.setDirectoryForTemplateLoading(new File("D:/workspaces-itcast/term197/taotao-item-web/src/main/webapp/WEB-INF/ftl"));  
  7.          // 第三步:設定模闆檔案使用的字元集。一般就是utf-8.  
  8.          configuration.setDefaultEncoding("utf-8");  
  9.          // 第四步:加載一個模闆,建立一個模闆對象。  
  10.          Template template = configuration.getTemplate("hello.ftl");  
  11.          // 第五步:建立一個模闆使用的資料集,可以是pojo也可以是map。一般是Map。  
  12.          Map dataModel = new HashMap<>();  
  13.          //向資料集中添加資料  
  14.          dataModel.put("hello", "this is my first freemarker test.");  
  15.          // 第六步:建立一個Writer對象,一般建立一FileWriter對象,指定生成的檔案名。  
  16.          Writer out = new FileWriter(new File("D:/temp/term197/out/hello.html"));  
  17.          // 第七步:調用模闆對象的process方法輸出檔案。  
  18.          template.process(dataModel, out);  
  19.          // 第八步:關閉流。  
  20.          out.close();  
  21.      }  

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的配置檔案

  1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?>  
  2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"  
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"  
  4.      xmlns:context="http://www.springframework.org/schema/context"  
  5.      xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd  
  8.         http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  9.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  10.      <beanidbeanid="freemarkerConfig"  
  11.          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  12.          <propertynamepropertyname="templateLoaderPath"value="/WEB-INF/ftl/"/>  
  13.          <propertynamepropertyname="defaultEncoding"value="UTF-8"/>  
  14.      </bean>  
  15. </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、關閉流。

加載配置檔案:

  1. @Controller  
  2. publicclass HtmlGenController {  
  3.      @Autowired  
  4.      private FreeMarkerConfigurerfreeMarkerConfigurer;  
  5.      @RequestMapping("/genhtml")  
  6.      @ResponseBody  
  7.      public String genHtml()throws Exception {  
  8.          // 1、從spring容器中獲得FreeMarkerConfigurer對象。  
  9.          // 2、從FreeMarkerConfigurer對象中獲得Configuration對象。  
  10.          Configuration configuration = freeMarkerConfigurer.getConfiguration();  
  11.          // 3、使用Configuration對象獲得Template對象。  
  12.          // 4、建立資料集  
  13.          dataModel.put("hello","1000");  
  14.          // 5、建立輸出檔案的Writer對象。  
  15.          Writer out = new FileWriter(new File("D:/temp/term197/out/spring-freemarker.html"));  
  16.          // 6、調用模闆對象的process方法,生成檔案。  
  17.          // 7、關閉流。  
  18.          return"OK";  
  19. }