freemarker中文手册 联系客服

发布时间 : 星期六 文章freemarker中文手册更新完毕开始阅读

开发笔记

截取字符

<#if news.title?length lt 13 > ${news.title}

<#else>

${news.title[0..12]}...

一、freemarker的空值和默认值

${user?if_exists}

${user?default('your name')}

判断对象是不是null <#if mouse?exists> Mouse found <#else>

或<#if book.name?? >

Mouse found

list 空值判断 <#if bookList?size = 0>

<#list recommendlist as content> <#if content_index%3==0>

<#if content_index%3==1>

  • ${content.title}
  • <#if content_index%3==2>

  • ${content.title}
  • <#list recommendlist as content> <#if content_index%3==0>

    font-weight:bold; font-size:14px;

    List指令还隐含了两个循环变量:

    item_index:当前迭代项在所有迭代项中的位置,是数字值。

    item_has_next:用于判断当前迭代项是否是所有迭代项中的最后一项。

    在循环过程中,如果您想跳出循环,那么可以使用结合break指令,即<#break>来完成。 <#escape x as (x)!\ //去除null报错

    三、 Freemarker如何遍历List实例应用

    创通过上面的介绍,相信您已经对Freemarker如何遍历List有了了解,那么我们就废话少说,开始做一个应用吧。 User类

    public class User{ private String username; private

    (省略set和get方法) } user.ftl

    <#--Freemarker遍历list--> 简单遍历list: <#list userList as user> 用户名:${user.userName} 密 码:${user.userPassword} 年 龄: ${user.age}

    <#--Freemarker遍历list并应用list隐含变量item_index--> item_index使用: <#list userList as user> 第${user_index+1}个用户 用户名:${user.userName} 密 码:${user.userPassword}

    年 龄: ${user.age}

    <#--Freemarker遍历list并应用list隐含变量item_has_next--> item_has_next,size使用: <#list userList as user> 用户名:${user.userName} 密 码:${user.userPassword} 年 龄: ${user.age} <#if !user_has_next>

    共有${userList?size}最后一个用户是:${user.userName}

    <#--Freemarker遍历list并按用户年龄升序排序--> 按用户年龄升序排序:

    <#list userList?sort_by(\ 用户名:${user.userName} 密 码:${user.userPassword} 年 龄: ${user.age}

    <#--Freemarker遍历list并按用户年龄降序排序--> 按用户年龄降序排序:

    <#list userList?sort_by(\ 用户名:${user.userName} 密 码:${user.userPassword} 年 龄: ${user.age}

    <#--Freemarker遍历list当用户年龄大于21岁时,停止输出--> list中应用break:

    <#list userList?sort_by(\ 用户名:${user.userName} 密 码:${user.userPassword} 年 龄: ${user.age} <#if (user.age>21) > <#break>

    1、快速入门

    (1)模板 + 数据模型 = 输出

    ? FreeMarker基于设计者和程序员是具有不同专业技能的不同个体的观念

    ? 他们是分工劳动的:设计者专注于表示——创建HTML文件、图片、Web页面

    的其它可视化方面;程序员创建系统,生成设计页面要显示的数据

    ? 经常会遇到的问题是:在Web页面(或其它类型的文档)中显示的信息在设计页面时是无效的,是基于动态数据的

    ? 在这里,你可以在HTML(或其它要输出的文本)中加入一些特定指令,FreeMarker会在输出页面给最终用户时,用适当的数据替代这些代码 ? 下面是一个例子:

    Welcome!

    Welcome ${user}!

    Our latest product: !