freemaker頁面靜態化

1. 網頁靜態化

可以使用Freemarker實現網頁靜態化。

 

1.1. 什麼是freemarker

FreeMarker是一個用Java語言編寫的模板引擎,它基於模板來生成文本輸出。FreeMarkerWeb容器無關,即在Web運行時,它並不知道ServletHTTP。它不僅可以用作表現層的實現技術,而且還可以用於生成XMLJSPJava 等。

 

目前企業中:主要用Freemarker做靜態頁面或是頁面展示

 

1.2. Freemarker的使用方法

freemarkerjar包添加到工程中。

Maven工程添加依賴

<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方法輸出文件。

第八步:關閉流。

 

模板:

${hello}

 

@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 as student>

${student.id}/${studnet.name}

</#list>

循環使用格式:

<#list 要循環的數據 as 循環後的數據>

</#list>

 

 

 

1.3.4. 取循環中的下標

<#list studentList as student>

${student_index}

</#list>

 

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包:

Freemarkerjar

 

 

1.4.1. 創建整合spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="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">

 

<bean id="freemarkerConfig"

class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

<property name="templateLoaderPath" value="/WEB-INF/ftl/" />

<property name="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

public class HtmlGenController {

@Autowired

private FreeMarkerConfigurer freeMarkerConfigurer;

 

@RequestMapping("/genhtml")

@ResponseBody

public String genHtml()throws Exception {

// 1、從spring容器中獲得FreeMarkerConfigurer對象。

// 2、從FreeMarkerConfigurer對象中獲得Configuration對象。

Configuration configuration = freeMarkerConfigurer.getConfiguration();

// 3、使用Configuration對象獲得Template對象。

Template template = configuration.getTemplate("hello.ftl");

// 4、創建數據集

Map dataModel = new HashMap<>();

dataModel.put("hello", "1000");

// 5、創建輸出文件的Writer對象。

Writer out = new FileWriter(new File("D:/temp/term197/out/spring-freemarker.html"));

// 6、調用模板對象的process方法,生成文件。

template.process(dataModel, out);

// 7、關閉流。

out.close();

return "OK";

}

}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章