freemarker模板引擎技術


freemarker模板引擎技術:

  先上個圖片

  

 

    簡單說就是莫辦系統,為了減少重復的代碼,更能降低數據庫的壓力,

  優點:速度快,分擔高並發,降低服務器壓力

  缺點:使用的局限性,應用於模板固定的新聞頁面,商品詳情頁面等

  文件后綴:XXX.ftl

  使用到的技術: IO 流 ->文件的讀取->輸出生成的文件

 

使用步驟:

  1.創建一個Configuration 對象 直接new一個對象.構造方法的參數就是freemaker對應的版本號

  Configuration configuration = new Configuration();

  2.設置模板文件使用的字符集.一般就是UTF-8 (這一步根據情況是設定)

  3.設置模板文件所在的路徑

  configuration.setDirectoryForTemplateLoading(new File("C:\\Demo\\pyg_project\\freemarkerDemo\\src\\main\\resources\\ftl"));

  4.加載一個模板,創建一個模板對象

  Template template =
configuration.getTemplate("文件名.ftl");

  5.創建一個模板使用的數據庫集 一般是Map

  Map<String, Object> rootMap = new HashMap<String, Object>();
//測試傳入假數據
rootMap.put("name", "張三");
rootMap.put("message", "歡迎來到品優購商城");

  6.創建一個Writer對象(這就是上面說的io流技術)指定生成的文件名->輸出

  
    Writer out = new FileWriter(new File("生成的文件名.html生成的文件類型"));

  7.調用模板對象的process方法輸出文件

  template.process(rootMap,out);

  8.關閉流

   out.close();

以上是生成的代碼:

  需要的依賴

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>

技術Demo及代碼源碼:自己手寫的
  
package cn.itcast.core;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TestFreemarker {
public static void main(String[] args) throws IOException, TemplateException {
//1.創建freemarker初始化對象
Configuration configuration = new Configuration();
//2.加載模板的所在目錄位置
configuration.setDirectoryForTemplateLoading(new File("C:\\Demo\\pyg_project\\freemarkerDemo\\src\\main\\resources\\ftl"));

//3.根據模板名稱創建模板對象
Template template =
configuration.getTemplate("test.ftl");

//4.創建模擬的假數據
Map<String, Object> rootMap = new HashMap<String, Object>();
//測試傳入假數據
rootMap.put("name", "張三");
rootMap.put("message", "歡迎來到品優購商城");
//5.定義輸出流 指定生成后的頁面的位置極其名稱

ArrayList<String> numberList = new ArrayList<String>();
numberList.add("張1");
numberList.add("張2");
numberList.add("張3");
numberList.add("張4");
numberList.add("張5");
rootMap.put("numberList", numberList);

HashMap<String, String> persionMap = new HashMap<String, String>();
persionMap.put("001", "張三1");
persionMap.put("002", "張三2");
persionMap.put("003", "張三3");
persionMap.put("004", "張三4");

rootMap.put("persionMap", persionMap);


//測試想模板中傳入日期類型數據
rootMap.put("today", new Date());

//
rootMap.put("point",1513513233);

Writer out = new FileWriter(new File("test.html"));

//6.生成
template.process(rootMap,out);
//7.關閉流
out.close();

}
}

技術使用的ftl文件
  
<html>
<head>
<meta charset="utf-8">
<title>Freemarker入門小DEMO </title>
</head>
<body>
<#--我只是一個注釋-->
${name},你好.${message}

<#--在頁面定義變量-->
<#assign linkman="周先生">
聯系人:${linkman}
<#assign info = {"mobile":"1234548541633",'address':'北京市'}>
電話 : ${info.mobile} 地址:${info.address}


<#--引入其他頁面-->
<#include "head.ftl">


<#--遍歷集合-->
<#list numberList as number>
${number}
<#if number_index == 0>
這是第一次循環
<#else >這是索引號${number_index}
</#if>
</#list>

<#--遍歷Map集合-->
<#list persionMap?keys as key>
${key} ${persionMap[key]}
</#list>

<#--演示展示日期類型數據-->
當前日期:${today?date}<br>
當前時間:${today?time}<br>
當前日期加時間:${today?datetime}<br>
日期格式化:${today?string("yyyy年MM月")}<br>

<#--展示數值對象-->
${point}

<#--原樣展示-->
${point?c}


<#--測試是否存在-->
<#if aaa??>
aaa變量存在
<#else >aaa變量不存在
</#if>

<#--測試${aaa}是否存在 存在展示自己不存在展示!后面的-->
${aaa!"母雞"}



<#list ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] as n>

<#if (n_index == 1) || (n_index == 3)>
${n} --紅色
<#else>
${n} --綠色

</#if>-
</#list>

</body>
</html>

  

以上代碼的技術知識點總結:(極其重要)

  <#list

  <#if

  <#else

  

<#--引入其他頁面-->
<#include "head.ftl">

<#--在頁面定義變量-->
<#assign linkman="周先生">

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM