FreeMarker笔记 前言第1章 入门

简介

简介

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

特点

功能

基础

概要、关键字

建议

前言

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

FreeMarker的设计实际上是被用来生成HTML网页,尤其是通过基于实现了MVC(Model View Controller,模型-视图-控制器)模式的Servlet应用程序。使用MVC模式的动态网页的构思使得你可以将前端设计者(编写HTML)从程序员中分离出来。所有人各司其职,发挥其擅长的一面。网页设计师可以改写页面的显示效果而不受程序员编译代码的影响,因为应用程序的逻辑(Java程序)和页面设计(FreeMarker模板)已经分开了。页面模板代码不会受到复杂的程序代码影响。这种分离的思想即便对一个程序员和页面设计师是同一个人的项目来说都是非常有用的,因为分离使得代码保持简洁而且便于维护。

2014-09-03_101642[5]

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

 

参考资料

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

项目

  1. P1:F:\360\Learn\FreeMarker\workspace\FreeMarker-hello-java\,https://github.com/yejq/FreeMarker-hello-java.git。
  2. P2:F:\360\Learn\freemarker\workspace\FreeMarker-hello-web\, https://github.com/yejq/FreeMarker-hello-web.git。

转载于:https://www.cnblogs.com/yejq/p/3964326.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546117.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

优先级调度算法动态优先级_与优先级调度有关的问题及其解决方案

优先级调度算法动态优先级We are already familiar with what Priority Scheduling is. It is one of the most used process scheduling algorithm used in operating systems, in which every process is assigned with a priority. According to this algorithm, the proces…

hdoj 1013 Digital Roots

链接&#xff1a;zoj 1115 或 hdoj 1013 或poj 1519 虽说是水题&#xff0c;却几经波折才搞定。该题目中的数字可能非常大&#xff0c;所以不能使用整型数&#xff0c;只能采用字符变量 代码如下&#xff1a; #include <stdio.h>int digitalRoot(int n); int digitS…

厉害了,3万字的MySQL精华总结 + 面试100问!

这是我的第 202 期分享作者 | 派大新来源 | JavaKeeper&#xff08;ID&#xff1a;JavaKeeper&#xff09;分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;❝写在之前&#xff1a;不建议那种上来就是各种面试题罗列&#xff0c;然后背书式的去记忆&#x…

网页视频播放器代码大全 + 21个为您的站点和博客提供的免费视频播放器

推荐 使用 极酷 Web在线播放器。网页中嵌入视频代码综合全然版 1.avi格式 代码片断例如以下&#xff1a;  程序代码 <objectid"video"width"400"height"200"border"0"classid"clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA&q…

codejam题目_嵌套深度-Google CodeJam 2020资格回合问题解决方案

codejam题目Problem statement: 问题陈述&#xff1a; Given a string of digits S, insert a minimum number of opening and closing parentheses into it such that the resulting string is balanced and each digit d is inside exactly d pairs of matching parentheses…

hdoj 1015 Safecracker

见hdoj 1015 或 zoj 1403 或tzu 1308 我使用了枚举法&#xff0c;代码写的很罗嗦&#xff0c;可能还是深度优先搜索写起来更清晰。 /* hdoj 1015 */ #include <stdio.h> #include <string.h>#define MAX (125) #define RESLEN 5 int bubSort(int a[], int n); in…

漫话:为什么计算机起始时间是1970年1月1日?

这是我的第 203 期分享作者 | 漫画编程来源 | 漫画编程&#xff08;ID&#xff1a;mhcoding&#xff09;分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;问题复现1970-01-01对于开发者来说都是不陌生的&#xff0c;有些系统对于时间的处理如果不够好的话&…

puppeteer执行js_使用Node.js和Puppeteer与表单和网页进行交互– 1

puppeteer执行jsHi guys! Today lets look at another powerful function of the puppeteer API using Node.js. 嗨&#xff0c;大家好&#xff01; 今天&#xff0c;让我们看看使用Node.js的puppeteer API的另一个强大功能。 In the first part of this section, lets look a…

zoj 1154 Niven numbers

见zoj 1154 还是需要将输入数据当作字符串来处理&#xff0c;不能直接使用整型。 /* zoj 1154 Niven numbers */#include <stdio.h> #define MAX 100 int isNivenNum(int base, char str[]);int main(void) {int totalBlocks;int base;int first 1;char str[MAX];scanf…

面试官:不会看SQL执行计划,简历也敢写精通SQL优化?

这是我的第 204 期分享作者 | 程序员内点事来源 | 程序员内点事&#xff08;ID&#xff1a;chengxy-nds&#xff09;分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;昨天中午在食堂&#xff0c;和部门的技术大牛们坐在一桌吃饭&#xff0c;作为一个卑微技…

scrollTop的兼容性小结

2019独角兽企业重金招聘Python工程师标准>>> 在页面上加上了 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 之后&#xff0c;document.body.scrollTop的值…

stl标准模板库_C ++标准模板库(STL)中的数组及其常用功能

stl标准模板库"array" is a container in C STL, which has fixed size, which is defined in "array" header. “ array”是C STL中的一个容器&#xff0c;具有固定大小&#xff0c;在“ array”标头中定义。 Declaration: 宣言&#xff1a; array <…

zoj 1074 To the MAX

见zoj 1074 参考了别人的思路才搞定。见http://blog.csdn.net/acm_davidcn/article/details/5834454 使用了最大连续子序列和的算法&#xff0c;虽然自己也知道这个算法&#xff0c;但是却没办法做到活学活用。 /* zoj 1074 To the Max */ #include <stdio.h> #inc…

阿里巴巴为什么让初始化集合时必须指定大小?

这是我的第 205 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;哈喽&#xff0c;亲爱的小伙伴们&#xff0c;技术学磊哥&#xff0c;进步没得说&#xff01;欢迎来到…

ios页面间跳转方式总结

转自&#xff1a;http://www.cnblogs.com/anywherego/p/3542202.html 下面以OldViewController(oldC)的按钮btn点击后跳转到NewViewController(newC)为例说明: 1.Storyboard的segues方式 鼠标点击按钮btn然后按住control键拖拽到newC页面&#xff0c;在弹出的segue页面中选择跳…

__asm___错误:“”前应有'=',',',',','asm'或'_attribute_'

__asm__A very common error in C programming language, it occurs when # is not used before the include. 这是C编程语言中非常常见的错误&#xff0c;当在include之前不使用&#xff03;时&#xff0c;就会发生此错误。 As we know that #include is a preprocessor dire…

Photoshop CS3 ICO 图标保存插件

最近编程发现&#xff0c;没有啥好看的图标文件于是&#xff0c;本人使用功能强大的ps&#xff0c;制作了图标文件做后发现&#xff0c;无法保存为ico图标文件在网上搜索了半天&#xff0c;终于从茫茫网海找到ico保存插件下载存放的地方是 PS根目录 即Adobe\Adobe Photoshop CS…

zoj 1005 jugs

题目内容见zoj1005 由于A&#xff0c;B互素且A的容量小于B&#xff0c;那么可以将B装满并且倒入A中&#xff0c;如果A被装满则将A中的内容全部清空&#xff0c;一直进行下去直到某一刻B中容量恰好等于目标的容量。这种方法能得到正确的结果&#xff0c;但是通常得不到最优结果…

啪啪打脸!领导说:try-catch要放在循环体外!

这是我的第 206 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;哈喽&#xff0c;亲爱的小伙伴们&#xff0c;技术学磊哥&#xff0c;进步没得说&#xff01;欢迎来到…

人类智商一般在多少左右?爱因斯坦的智商是多少?

智商 智商就是智力商数。智力通常叫智慧&#xff0c;也叫智能。是人们认识客观事物并运用知识解决实际问题的能力。智力包含多个方面&#xff0c;如观察力、记忆力、想象力、分析推断能力、思维能力、应变能力等。智力的高低通经常使用智力商数来表示&#xff0c;是用以标示智力…