Java模版引擎之Freemarker

Java模版引擎之Freemarkerhtml

freemarker是一款模版引擎,是一種基於模版生成靜態文件的通用工具,它是爲Java程序員提供的一個類庫,它不是面向最終用戶的,而是爲程序員提供了一款能夠嵌入他們開發產品的應用程序。程序員

 1. 插值ide

  1.1 經常使用${var}語法進行取值函數

  1.2 null、不存在對象取值${var!’設置默認值’}工具

  1.3 取包裝對象的值,經過「點」語法:${user.name}spa

  1.4 取值的時候進行計算、賦值:code

    <#assign str=’我是自定義值’ />htm

    <span>${‘輸出:’ + str}</span>對象

  1.5 Date類型格式${date?String(yyyy-MM-dd‘’)}blog

  1.6 轉義HTML內容:${var?html}

2. 邏輯指定:ifswitch

2.1 if

<#if var == ‘str1’>
    var = ‘str1’
<#else>
   var != ‘str1’
</#if>

 

<#if var ==  ‘str1’>
    var = ‘str1’
<#elseif var ==  ’str2’>
    var != ‘str2’
<#else>
    var != ‘str1’
</#if>

  用<#if var??><#if>  <#if var?exite><#if>  判斷變量是否存在

2.2 switch

<#switch var>
    <#case 1>
        case1
        <#break>
    <default>
        other
</#switch>    

 

3. 集合遍歷

  3.1 list遍歷

    <#list myList as item>

      下標:${item_index}

      值:${item}

    </#list>

  3.2 map遍歷

    <#list myMap?keys as key>

      ${key} : ${map[key]}

    </#list>

4. string基本操做指令

  ${str?substring(5,8)}:截取,取頭不取尾

  ${str?length}:獲取長度

  ${str?upper_case}:所有轉爲大寫

  ${str?lower_case}:所有轉爲小寫

  ${str?index_of(‘xx’)}:查找子串第一次出現的位置

  ${str?last_index_of(‘xx’)}:查找子串最後一次出現的位置

  ${str?replace(‘a’,’xx’)}:替換

5. 自定義函數(繼承TemplateMethodModelEx接口)

  5.1 建立自定義函數類

public class TemplateMethd implements TemplateMethodModelEx {
    @Override
    public Object exec(List list) throws TemplateModelException {
        // 獲取參數
        SimpleSequence simpleSequence = (SimpleSequence) list.get(0);
        List<BigDecimal> list1 = simpleSequence.toList();
        Collections.sort(list1, (a, b) -> { return a.intValue() - b.intValue(); });
        return list1;
    }
}

 

  5.2 將自定義函數對象傳到模版中

modelAndView.setViewName("index");
modelAndView.addObject("sort_int_list", new TemplateMethd());
return modelAndView;

 

  5.3 在模版中使用

<#assign myList=[1,4,5,2,3,7,5,9,0] />
<#list sort_int_list(myList) as item>
    ${item},
</#list>

 

6. List的指令

  6.1 排序

    <#list myList?sort as item>

      ${item_index} : ${item}

    </#list>

  6.2 反轉

    <#list myList?reverse as item>

      ${item_index} : ${item}

    </#list>

  6.3 長度

    ${myList.size}

7. Freemarker內建函數

  7.1 處理字符串內建函數

    substring:字符串截取

    cap_first:首字母大寫

    starts_with:是否以子串開始

    ends_with:是否以子串結尾

    index_of:查找子串第一次出現下標

    last_index_of:查找子串最後一次出現下標

    split:分隔字符串

    trim:去首位空格

    contains:是否包含子串

    date/datetime/time:字符串轉換爲日期格式

  7.2 處理數字的內建函數

    string:轉爲字符串

    x?string(「0.##」):轉爲小數點

    round:四捨五入

    floor:四捨五入

    ceiling:四捨五入進一

  7.3 處理List的內建函數

    first/list:取第一個/最後一個值

    seq_contains:序列是否包含該值

    seq_index_of:該值在list中第一次出現的下標

    size/reverse/sort/sort_by:大小/反轉/排序/list中爲一個對象,能夠根據某個屬性進行排序

    chunk(num):把list進行分塊處理,每num爲一塊  

  7.4 其餘內建函數

    is函數:is_string/is_number/is_method(判斷是否爲string/number/method

    has_content函數:判斷一個對象是不是null、不存在

8. Function指令

<#function doAdd param1 param2>

<@return param1 + param2 />

</@function>

調用: ${doAdd(5, 6)}
相關文章
相關標籤/搜索