freemarker中文手册

2019-09-01 13:28

开发笔记

截取字符

<#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: ! ? 这个例子是在简单的HTML中加入了一些由${…}包围的特定代码,这些特定代

码是FreeMarker的指令,而包含FreeMarker的指令的文件就称为模板(Template) ? 至于user、latestProduct.url和latestProduct.name来自于数据模型(data model) ? 数据模型由程序员编程来创建,向模板提供变化的信息,这些信息来自于数据库、

文件,甚至于在程序中直接生成

? 模板设计者不关心数据从那儿来,只知道使用已经建立的数据模型 ? 下面是一个可能的数据模型:

(root) | +- user = \ | +- latestProduct | +- url = \ | +- name = \? 数据模型类似于计算机的文件系统,latestProduct可以看作是目录,而user、url和name看作是文件,url和name文件位于latestProduct目录中(这只是一个比喻,实际并不存在) ? 当FreeMarker将上面的数据模型合并到模板中,就创建了下面的输出:

Welcome!

Welcome Big Joe!

Our latest product: ! (2)数据模型 ? 典型的数据模型是树型结构,可以任意复杂和深层次,如下面的例子:

(root)


freemarker中文手册.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:湖北蓝田股份有限公司财务报表分析

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: