zoukankan      html  css  js  c++  java
  • FreeMarkerD1

    评论页面content:
    [@cms_content_page]
    [#if tag_pagination.list?size = 0]
    暂无评论
    [#else]
    [#list tag_pagination.list as c]
    用户:${(c.commentUser.username)!匿名网友} 时间:${c.createtime}
    内容:${c.textHtml!}
    [/#list]
    [/#if]
    [/@cms_content_page]

    标签使用:
    1. 先引入模版标签[@+标签名]
    2. 如果有list的使用标签[#list 标签名 as c](不包含判断是否为空集合)
    3. 输出${c.commentUser.username} 由于Spring的依赖注入模版标签直接调用了entity(也就是pojo)的get方法,能调用对象和属性

    [@process_time/]——页面加载所用时间
    [@cms_comment_list]
    [@cms_channel_list]
    [@cms_lucene_page]
    [@cms_tag_list]

    指令类型:预定义指令和用户自定义指令
    <#directivename parameter>${parameter.name}</directivename> 预定义
    <@user></@user> 用户自定义
    此外,自定义指令可以使用macor(指令)来定义greet(宏)
    <#macor greet person> <#-- 这里可以定义多个变量 -->
    Hello 你好!${person}
    </#macor>

    为了表明字符串是原生字符串,在开始的引号或单引号之前放置字母r

    内嵌函数:${user.upper_case} 转成大写,前提是user为字符串
    双重内嵌函数:${user.upper_case.html}先会把user答谢转换然后在进行html内建函数转换

    方法调用:repeat(args ..) 前提是程序员事先定义好的方法。
    不存在的值: FreeMarker里没有null值这个概念(在2.3.7之后的版本可处理null值了)
    默认值: ${mouse!"NO MOUSE"} 假如没有定义mouse这个变量那么输出将会是NO MOUSE


    检测不存在的值:一般在if条件句中常用,例如:
    <#if username??>
    username found
    <#else>
    username not found
    <#if>
    此时如果想定义一个username那么可以这样:<#assign username="maomao"> 在执行上面的语句就会输出username found

    ${a == 2}不会输出true或false,这是错误的。 换个角度可以使用内建函数${a?string("true","false")}

    嵌套内容输出例子:
    <#macro greet count>
    <#local y="test">
    <#list 1..count as x>
    ${y} ${count}/${x}:<#nested>
    </#list>
    </#local>
    </#macro>

    <@repeat count=3>${y!"?"} ${x!"?"} ${count!"?"} </@repeat>

    循环语句:(新的表示法)
    <#macro do_thrice>
    <#nested 1>
    <#nested 2>
    <#nested 3>
    </#macro>

    <@do_thrice ; x> <#-- 这里的;号相当于集合遍历中的as -->
    ${x} Anything.
    </@do_thirce>
    输出:
    1 Anything.
    2 Anything.
    3 Anything.

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

    分析:
    1. macro是自定义指令、repeat代表宏、count标识一个变量
    2. list是一集合,从1到count值进行遍历
    3. nested指令输出时指定到对应的${c}. ${halfc}<#if last> Last!</#if> 其中的变量一一对应
    4. <@repeat count=4 ; c, halfc, last> 这里涉及到了一个宏内部使用循环,;号之后的变量与nested中的参数一一对应
    结果应该是:
    1. 0.5
    2. 1
    3. 1.5
    4. 2 Last!
    注:repeat宏指定的参数应该是小于等于nested中参数的个数


    Java应用FreeMarker模版。
    1. 创建并指定模版路径。如下: Configuration是存储FreeMarker模版的中央区域,主要负责模版的创建和缓存
    Configuration cfg = new Configuration();
    // Specify the data source where the template files come from.
    // Here I set a file directory for it:
    //指定加载模版的目录
    cfg.setDirectoryForTemplateLoading(
    new File("/where/you/store/templates"));
    // Specify how templates will see the data-model. This is an advanced topic...
    // but just use this:
    cfg.setObjectWrapper(new DefaultObjectWrapper());
    注意:应该使用单实例配置
    2. 创建数据模型,如下:
    // Create the root hash
    Map root = new HashMap();
    // Put string "user" into the root
    root.put("user", "Big Joe");
    // Create the hash for "latestProduct"
    Map latest = new HashMap();
    // and put it into the root
    root.put("latestProduct", latest);
    // put "url" and "name" into latest
    latest.put("url", "products/greenmouse.html");
    latest.put("name", "green mouse");
    相当于生成一个树形结构
    3. 创建模版
    从Configuration实例中获取Template
    Template temp = cfg.getTemplate("test.ftl");
    调用这个方法会创建一个test.ftl的Template实例,并去读取编译它,虽然Template实例以解析后的形式存储模版,而不是以源文件的形式存
    储。 以后在此获取test.ftl时就不会再创建Template实例了
    4. 将模版和数据模型合并
    Writer out = new OutputStreamWriter(System.out);
    temp.process(root, out);
    out.flush();

  • 相关阅读:
    轻节点如何验证交易的存在
    梯度爆炸/消失与初始化参数
    归一化能够加速训练的原因
    正则化可以防止过拟合的原因
    关于周志华《机器学习》中假设空间规模大小65的计算
    linux学习0001
    目标检测算法
    opencv安装与卸载
    前端学习02
    前端学习01
  • 原文地址:https://www.cnblogs.com/xmaomao/p/2985112.html
Copyright © 2011-2022 走看看