您的位置:首页 > 其它

FreeMarker笔记 前言&第1章 入门

2014-09-10 14:08 375 查看

简介

简介

FreeMarker是一款模板引擎:一种基于模板的、用来生成输出文本(任何来自于HTML格式的文本用来自动生成源代码)的通用工具。它是为Java程序员提供的一个开发包或者说是类库。它不是面向最终用户,而是为程序员提供的可以嵌入他们开发产品的一款应用程序。

特点

功能

基础

概要、关键字

建议

前言

FreeMarker是一款模板引擎:一种基于模板的、用来生成输出文本(任何来自于HTML格式的文本用来自动生成源代码)的通用工具。它是为Java程序员提供的一个开发包或者说是类库。它不是面向最终用户,而是为程序员提供的可以嵌入他们开发产品的一款应用程序。 FreeMarker的设计实际上是被用来生成HTML网页,尤其是通过基于实现了MVC(Model View Controller,模型-视图-控制器)模式的Servlet应用程序。使用MVC模式的动态网页的构思使得你可以将前端设计者(编写HTML)从程序员中分离出来。所有人各司其职,发挥其擅长的一面。网页设计师可以改写页面的显示效果而不受程序员编译代码的影响,因为应用程序的逻辑(Java程序)和页面设计(FreeMarker模板)已经分开了。页面模板代码不会受到复杂的程序代码影响。这种分离的思想即便对一个程序员和页面设计师是同一个人的项目来说都是非常有用的,因为分离使得代码保持简洁而且便于维护。

FreeMarker不是Web应用框架。它是Web应用框架中的一个适用的组件。

第1章 入门

1.2 模板+数据模型=输出

1.3 数据模型一览

1.4 模板一览

1.4.1 简介 FTL tags标签:FreeMarker模板的语言标签。一般以符合
#
开头,用户自定义的FTL标签使用
@
代替
#
Comments注释:FreeMarker的注释和HTML的注释相似,但是它用
<#--
-->
来分隔。 directives指令:就是所指的FTL标签。 1.4.2 指令示例 1.4.2.1 if指令 假设你只想向你的老板Big Joe(而不是其他人)问好,就可以这样做:
<h1>
Welcome ${user}<#if user == "Big Joe">, our beloved leader</#if>!
</h1>

使用<#else>标签:
<#if animals.python.price < animals.elephant.price>
Pythons are cheaper than elephants today.
<#else>
Pythons are not cheaper than elephants today.
</#if>

如果变量本身就是布尔值,可以直接作为if的条件;
<#if animals.python.protected>
Warniing! Pythons are protected animals!
</#if>

实例
/FreeMarker-hello-web/src/main/java/org/yejq/fre/model/Animal.java
public class Animal {

private String name;
private double price;
private boolean protect;

。。。
}

/FreeMarker-hello-web/src/main/java/org/yejq/fre/service/Exercises.java
public void testIf(Model model){
model.addAttribute("user", "Big Joe");

Map<String, Animal> animals = new HashMap<String, Animal>();
animals.put("python", new Animal("python", 300, true));
animals.put("elephant", new Animal("elephant", 400, false));
model.addAttribute("animals", animals);
}

/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/if.ftl
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>If指令</title>
</head>
<body>
<h1>Welcome ${user}<#if user == "Big Joe">, our beloved leader</#if></h1>
<p>
<#--大于号两边要加括号括起来,否则会以为是结束标签 -->
<#if (animals.python.price > animals.elephant.price)>
python.price > elephant.price
<#else>
python.price <= elephant.price
</#if>
</p>
<p>
<#if animals.python.protect>
python.protect = true;
</#if>
</p>
</body>
</html>

测试: http://localhost/test/2/if/testIf
1.4.2.2 list指令
当需求遍历集合的内容时,list指令是非常好用的。
<#list animals as being>
<tr><td>${being.name}<td>${being.price} Euros
</#list>

实例
/FreeMarker-hello-web/src/main/java/org/yejq/fre/service/Exercises.java
public void testList(Model model){
List<Animal> animals = new ArrayList<Animal>();
animals.add(new Animal("python", 300, true));
animals.add(new Animal("elephant", 400, false));
model.addAttribute("animals", animals);
}

/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/list.ftl
<h3>list指令</h3>
<table border=1>
<#list animals as animal>
<tr>
<#-- boolean类型要设置默认输出值,否则报错 -->
<td>${animal.name}, ${animal.price}, ${animal.protect?c}</td>
</tr>
</#list>
</table>

测试:
http://localhost/test/2/list/testList
1.4.2.3 include指令
在当前模板中插入其他文件的内容。
copyright_footer.html:
<hr>
<i>
Copyright (c) 2000<a href="http://www.xxx.com">Acmee Inc</a>,
<br>
All Rights Reserved.
</i>

当需要copyright时,引入
<#include "/copyright_footer.html">

实例
/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/copyright.html
<hr>
<i>
Copyright (c) 2000<a href="http://www.xqsoso.com">Acmee Inc</a>,
<br>
All Rights Reserved.中文测试
</i>

/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/include.ftl
<h3>include指令</h3>
<#include "copyright.html">

测试:http://localhost/test/2/include/null
1.4.2.4 联合使用指令
指令可以嵌套使用;
1.4.2.5 处理不存在的变量
<h1>Welcome ${user!"Anonymous"}!</h1>

通过在变量名后边跟一个
!
和默认值。
<h1>Welcome ${user!"Anonymous"}!</h1>

可以使用??询问freemarker一个变量是否存在,将它和if指令合并,那么如果user变量不存在的话将会忽略整个问候代码段;
<#if user??><h1>Welcome ${user}!</h1></#if>

对于多级访问的变量,比如animals.python.price,书写代码:animals.python.price!0,仅当animals.python存在且最后一个子变量price可能不存在(这种情况下我们假设价格是0)。如果animals或者python不存在,那么模板处理过程将会以“未定义的变量”错误而停止。为了防止这种情况的发生,可以这样来书写代码
(animals.python.price)!0
。这种情况下当animals或python不存在时表达式的结果仍然是0。对于??也是同样用来的处理这种逻辑的,animals.python.price??对比(animals.python.price)??来看;
实例
/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/null.ftl
<h3>处理不存在的变量</h3>
<p>welcome, ${user!"anonymous"}</p>
<p>检测user是否存在,<#if user??>Welcome, ${user}</#if></p>
<#--不加括号会报错: nested exception is freemarker.core.InvalidReferenceException: The following has evaluated to null or missing-->
<p>多级访问, ${(animals.python.price)!0}</p>

测试: http://localhost/test/2/null/null

参考资料

B1 :《FreeMarker中文版文档.pdf》
B2 :

项目

P1:
F:\360\Learn\FreeMarker\workspace\FreeMarker-hello-java\
https://github.com/yejq/FreeMarker-hello-java.git
P2:
F:\360\Learn\freemarker\workspace\FreeMarker-hello-web\
https://github.com/yejq/FreeMarker-hello-web.git
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: