• flt基础知识记录并分享



    ****************************指 令 ****************************************
    1,插值
    用户名:${user.name}
    组名:${user.group.name}

    2,if..else..
    <#if user.age lt 18>
    ${user.name}还是一个小孩
    <#else>
    ${user.name}是成年人了
    </#if>



    4,list...
    <#assign seq = ["winter", "spring", "summer", "autumn"]/>
    <#list seq as x>
    ${x_index + 1}. ${x}<#if x_has_next>,</#if>
    </#list>

    5,noparse指令 (错误)
    指定FreeMarker不处理该指定里包含的内容
    <#assign books = ["winter", "spring", "summer", "autumn"]/>
    <#noparse>
    <#list books as book>
    <tr><td>${book.name}<td>作者:${book.author}
    </#list>
    </#noparse>

    6,compress 指令
    移除html,xml中多余的空白,仅保留一个空白</br>
    <#compress>
    1 2 3 4 5

    test only

    I said, test only

    </#compress>

    忽略标记中的空白(查看源文件)
    --
    1 <#t/>
    2<#t/>
    3<#lt/>
    4
    5<#rt/>
    6
    --

    7,escape,noescape 转义指令
    html转义 防范xss攻击(区别需查看源文件),主要用法是将整个文档包裹在转义指令内
    <#assign title="<title>req & resp</title>"/>
    <#escape x as x?html>
    First name: ${title}
    <#noescape>Last name: ${title}</#noescape>
    </#escape>

    8,setting 系统环境设置
    locale 输出本地化语言
    number_format 用来转化数字到字符串的格式设置[computer,currency,percent]
    boolean_format
    date_format
    time_format
    datetime_format
    time_zone </br>
    classic_compatible 兼容传统模式
    <#setting locale="hu_hu"/>
    ${1.2}
    <#setting locale="en_US"/>
    ${1.2}

    9,macro,nested,return宏变量指令
    nested执行自定义指令开始和结束标签中间的模板片段
    <@list items=["mouse", "elephant", "python"] title="Animals"/>
    <#macro list title items>
    ${title?cap_first}:
    <#list items as x>
    *${x?cap_first}
    </#list>
    </#macro>

    <#macro repeat count>
    <#list 1..count as x>
    <#nested x, x/2, x==count>
    </#list>
    </#macro>
    <@repeat count=4; c, half, last>
    ${c}. ${half}
    <#if last>
    Last!
    </#if>
    </@repeat>

    10,用户自定义指令@
    <#--定义一个自定义指令 -->
    <#macro book>
    j2ee
    </#macro>
    <#--使用刚才定义的指令-->
    <@book />

    11,function 函数指令
    区别于宏对象,带返回值
    <#function avg x y>
    <#return (x + y) / 2>
    </#function>
    ${avg(10, 20)}

    12,attempt,recover 异常处理指令
    <#attempt>
    正确: ${user.name}
    <#recover>
    异常.
    </#attempt>

    13,include指令 嵌入页
    <#assign me = "李善福"/>
    <span><strong><span><#include "/copyright.ftl" encoding="UTF-8"></span></strong></span>

    14,import指令 导入宏库
    <#import "mylib.ftl" as my/>
    <@my.copyright '李善福'/>

    <#import "mylib.ftl" as my/>
    <@my.list items=["mouse", "elephant", "python"] title="Animals"/>

    ****************************内建函数 ****************************************
    <#assign str1="freemarker截取字符串"/>
    字符串截取:

    字符串长度:
    ${str1?length}
    字符串替换:
    ${str1?replace("ee","EE")}
    首字母大写:
    ${str1?cap_first}
    首字母小写:
    ${str1?uncap_first}
    单词首字母大写:
    ${str1?capitalize}
    全部转小写:
    ${str1?lower_case}
    全部转大写:
    ${str1?upper_case}
    是否“字符串”结尾
    ${str1?ends_with("字符串")?string}
    是否“free”开头
    ${str1?starts_with("free")?string}
    是否包含字符串"marker"
    ${str1?contains("marker")?string}
    mar 索引所在位置-前
    ${str1?index_of("mar")}
    mar 索引所在位置-后
    ${str1?last_index_of("mar")}
    距左边距:
    ${str1?left_pad(10)}
    距右边距:
    ${str1?right_pad(10)}
    去掉字符串首尾空格:
    ${str1?trim}
    分词列表大小:
    ${str1?word_list?size}
    <hr/>
    <#assign str7="aaa;bbb;ccc"/>
    <#list str7?split(";") as fileName>
    ${fileName}<br/>
    </#list>

    <#assign str2="200"/>
    转换为数字:
    ${str2?number}<br/>
    <hr/>
    <#assign date1="2009-10-12"?date("yyyy-MM-dd")>
    <#assign date2="9:28:20"?time("HH:mm:ss")>
    <#assign date3="2009-10-12 9:28:20"?datetime("yyyy-MM-dd HH:mm:ss")>
    转换为日期:
    ${date1}
    转换为时间:
    ${date2}
    转换为时间戳:
    ${date3}

    <#assign str3 = 'The "foo" bean.'>
    字符串转义:
    ${str3?j_string}<br/>
    <#assign str4 = "Readonly 's pet name is 'Cross Bone''">
    js转义:
    ${str4?js_string}
    <#assign str5>
    {"res":"0","msg":"OK","count":"3","pagenum":"1","pagesize":"1","totalpage":"1","userlist":[{"id":"1","name":"aa","sex":"男","age":"22"},{"id":"2","name":"bb","sex":"男","age":"21"},{"id":"3","name":"cc","sex":"女","age":"20"}]}
    </#assign>
    <#assign json=str5?eval />
    json转义:
    <#list json.userlist as item>
    id:${item.id}, name:${item.name},sex:${item.sex}
    </#list>
    <br/>
    <#assign str6 = "Tom&Jerry">
    html转义(查看源):
    ${str6?html}
    正则匹配
    <#if "fxo"?matches("f.?o")>
    Matches.
    <#else>
    Does not match.
    </#if>
    <br/>
    ---------------------------------------------------
    Freemarker 对空值的判断
    1,如果name为null,freemarker就会报错。如果需要判断对象是否为空
    <#if name??>
    </#if>
    <#if (user.name)??>
    </#if>
    <#if user.name??>
    </#if>
    2,设置默认值来避免对象为空的错误
    ${name!''}
    3,name为user的属性时,user,name都有可能为空
    ${(user.name)!''}
    4,name为user的属性时,name有可能为空
    ${user.name!''}

    ${student.birthday}改为${student.birthday?string('yyyy-MM-dd HH:mm:ss')}
    ---------------------------------------------------
    偏好设置
    Window|MyEclipse|Validation
    保留manual(手动)部分,build下只留"classpath dependency Validator"
    手动验证: 右键文件 -> Myeclipse -> Run Validation
    ---------------------------------------------------
    basePath
    1,viewResolverFtl
    <!-- 获取绝对路径basePath -->
    <property name="requestContextAttribute" value="request" />
    2,ftl
    <#assign base=request.contextPath />
    3,js
    <base id="base" href="${base}">
    var base = document.getElementById("base").href;
    4,与后台交互
    _send = function(async, url, value, success, error) {
    $.ajax({
    async : async,
    url : base + '/' + url,
    contentType : "application/x-www-form-urlencoded; charset=utf-8",
    data : value,
    dataType : 'json',
    type : 'post',
    success : function(data) {
    success(data);
    },
    error : function(data) {
    error(data);
    }
    });
    };
    ---------------------------------------------------
    freemark获取context方法
    1,taglibs.ftl
    <#macro basePath>
    <#if springMacroRequestContext.getContextPath()=="/">
    <#else>
    ${springMacroRequestContext.getContextPath()}
    </#if>
    </#macro>
    <#global contextPath><@basePath/></#global>
    2,在ftl文件中引入
    <#import "/spring.ftl" as spring />
    <#include "/common/taglibs.ftl" />
    3,在其它文件里 ${contextPath} 就可以获取根目录了

  • 相关阅读:
    pycharm的list的应用
    pycharm的list中copy的应用
    pycharm的list中clear的应用
    pycharm的list中append的应用
    crontab 管理指定用户的定时任务
    vsftp 搭建及虚拟账号配置
    MySQL 主从配置
    Ant 学习及常用任务
    ansible 小试
    微信H5页面分享
  • 原文地址:https://www.cnblogs.com/maz9666/p/5009220.html
Copyright © 2020-2023  润新知