接着freemarker(一)
1. 输出html
02.ftl
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <h1>您好,${username}</h1> </body> </html>
test02
@Test public void test02(){ Map<String,Object> root = new HashMap<String,Object>(); root.put("username", "张三"); fu.print("02.ftl", root); fu.fprint("02.ftl", root, "G:\\studyDemo\\freemarker\\02.html"); }
输出
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <h1>您好,张三</h1> </body> </html>
2. if指令
03.ftl
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <h5> ${user.id}------>${user.name}------>${user.age} </h5> <#--if指令--> <#-- 语法:<#if condition></#if> --> <#if user.age lt 12> ${user.name}还是一个小孩 <#elseif user.age == 18> ${user.name}刚成年 <#else> ${user.name}已成年 </#if> </body> </html>
test3
@Test public void test03(){ Map<String,Object> root = new HashMap<String,Object>(); root.put("user", new User(1,"zhangsan",22)); fu.print("03.ftl", root); fu.fprint("03.ftl", root, "G:\\studyDemo\\freemarker\\03.html"); }
输出
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <h5> 1------>zhangsan------>22 </h5> zhangsan已成年 </body> </html>
3. list指令
04.ftl
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <#--list指令--> <#-- 语法:<#list sequence as loopVariable>repeatThis</#list> --> <#list users as user> <div>${user.id}------>${user.name}------>${user.age}</div> </#list> </body> </html>
test4
@Test public void test04(){ Map<String,Object> root = new HashMap<String,Object>(); List<User> users = new ArrayList<User>(); users.add(new User(1,"zhangsan",22)); users.add(new User(2,"lisi",33)); users.add(new User(2,"wangwu",44)); root.put("users", users); fu.print("04.ftl", root); fu.fprint("04.ftl", root, "G:\\studyDemo\\freemarker\\04.html"); }
输出
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <div>1------>zhangsan------>22</div> <div>2------>lisi------>33</div> <div>2------>wangwu------>44</div> </body> </html>
5. include指令
05.ftl
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <#--include指令--> <div class="header"> <#include "/inc/inc.ftl"> <div> <div class="footer"> <#include "/inc/copyright_footer.html"> </div> </body> </html>
test5
@Test public void test05(){ Map<String,Object> root = new HashMap<String,Object>(); root.put("username", "张三"); fu.print("05.ftl", root); fu.fprint("05.ftl", root, "G:\\studyDemo\\freemarker\\05.html"); }
输出
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <div class="header"> <h1>欢迎张三进入系统!</h1> <div> <div class="footer"> <hr> <i> Copyright (c) 2000 <a href="http://www.acmee.com">Acmee Inc</a>, <br> All Rights Reserved. </i> </div> </body> </html>
6. freemarker处理null
06.ftl
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <#--freemarker处理null值 --> <#--在值之后添加一个"!"可以为这个值进行判断,如果不存在则使用!号声明的值--> ${user.id}------>${user.name}------>${user.group!"没有值"} <#--使用括号括起来之后,会连续判断所有的对象,如果为空则使用!号声明的值--> ${(user.group.name)!} <#--没有的元素也可以用"!"来进行判断--> ${(a.b)!"没有a.b元素"} <#--"??"用来判断是否为空,如果为true表示不为空,否则表示为空--> <#if (a.b)??> 不为空 <#else> 为空 </#if> </body> </html>
test6
@Test public void test06(){ Map<String,Object> root = new HashMap<String,Object>(); root.put("user", new User(1,"zhangsan",22)); fu.print("06.ftl", root); fu.fprint("06.ftl", root, "G:\\studyDemo\\freemarker\\06.html"); }
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> 1------>zhangsan------>没有值 没有a.b元素 为空 </body> </html>
7. freemarker基本数据类型
07.ftl
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <#--freemarker基本数据类型--> <#--定义变量--> <#assign username="张三" /> ${username} <#--定义数字--> <#assign num=10 /> ${num} <#assign str="10" /> ${str+11} <#--值会完成覆盖--> <#assign str="12" /> ${str+11} <#--不能直接输出数字或都字符串以外的类型,否则都会报错,需要转换为字符才能输出 使用xxx?string可以完成对字符串的转换 --> <#assign b=true /> <#--${b}--> ${b?string} <#--日期不能直接输出,需要转换成字符串--> ${now?string("yyyy-MM-dd HH:mm:ss")} <#--以下显示了使用字符链接和插值的方式连接字符串--> ${"hello"+username} ${"hello${username}"} <#--字符串转换成日期 data用来转换日期,datatime转换日期和时间,time转换时间--> <#assign bir="2015-07-12"?date("yyyy-MM-dd")/> ${bir} <#assign bir="2015-07-12 13:16:55"?datetime("yyyy-MM-dd HH:mm:ss")/> ${bir} </body> </html>
test7
@Test public void test07(){ Map<String,Object> root = new HashMap<String,Object>(); root.put("now", new Date()); fu.print("07.ftl", root); fu.fprint("07.ftl", root, "G:\\studyDemo\\freemarker\\07.html"); }
输出
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> 张三 10 1011 1211 true 2016-05-08 22:41:09 hello张三 hello张三 2015-7-12 2015-7-12 13:16:55 </body> </html>
8. freemarker序列和哈希表
08.ftl
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <#--freemarker序列和哈希表--> <#assign nums=[1,2,3,45,66]/> <#list nums as num> ${num} </#list> <#--特别注意,以下定义不用使用[1..10]--> <#assign nums=1..10/><#--定义一个连续的序列从1到10--> <#list nums as num> ${num} </#list> <#--定义一个map集合--> <#assign maps={"1": "张三","2": "李四"}> ${maps["1"]} <#--以下代码可以将map的key转换为相应的序列--> <#assign keys=maps?keys> <#list keys as key> ${key}------>${maps[key]} </#list> </body> </html>
test8
@Test public void test08(){ Map<String,Object> root = new HashMap<String,Object>(); fu.print("08.ftl", root); fu.fprint("08.ftl", root, "G:\\studyDemo\\freemarker\\08.html"); }
输出
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> 1 2 3 45 66 1 2 3 4 5 6 7 8 9 10 张三 1------>张三 2------>李四 </body> </html>
9. freemarker自定义指令
09.ftl
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <#--freemarker自定义指令--> <#-- macro指令语法: <#macro name param1 param2 ... paramN> ... <#nested loopvar1, loopvar2, ..., loopvarN> ... <#return> ... </#macro> --> <#--定义一个hello无参方法--> <#macro hello> <h3>hello</h3> </#macro> <#--调用hello方法--> <#--<@hello />--> <#--定义一个hello方法,有两个参数nums,world--> <#macro hello nums world> <#list 1..nums as num> ${"hello${world}${num?string}"} </#list> </#macro> <#--调用<@hello />会报错,因为hello有两个参数,在定义参数的值是参数名不能省略--> <#--<@hello nums=10 world="world" />--> <#--定义一个hello方法,有两个参数nums,world,并给了初始值,此时调用指令就可以省略参数,如果省略则使用默认值--> <#macro hello nums=10 world="world" > <#list 1..nums as num> ${"hello${world}${num?string}"} </#list> </#macro> <#--<@hello/>--> <#macro hello> <#--nested会输出指令中的内容--> <#nested /> <#nested /> </#macro> <@hello> <h3>hello</h3> </@hello> <#macro hello> <#--nested会输出指令中的内容--> <#nested 11 12/> <#nested 22 23/> </#macro> <@hello;x,y> <h3>hello--${x}--${y}</h3> </@hello> </body> </html>
test9
@Test public void test09(){ Map<String,Object> root = new HashMap<String,Object>(); fu.print("09.ftl", root); fu.fprint("09.ftl", root, "G:\\studyDemo\\freemarker\\09.html"); }
输出
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <h3>hello</h3> <h3>hello</h3> <h3>hello--11--12</h3> <h3>hello--22--23</h3> </body> </html>
10. freemarker变量
10.ftl
<html> <head> <title>MyHtml.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <#--freemarker变量--> <#-- 变量分为: 1. 数据模型中的变量; --root中的变量 2. 模版中的变量; --使用<#assign>定义的变量 3. 局部变量; --指令中的变量 4. 循环变量; --在循环中的变量 --> <#--数据模型中的变量--> ${username} <#--模版中的变量--> <#assign username="李四" /> <#--此时模版中的变量与数据模型中的变量名称一致,不是覆盖,而是隐藏--> ${username} <#--使用.globals可以访问数据模型中的变量--> ${.globals.username} <#--局部变量--> <#macro test> <#--使用local可以声明局部变量,所以在macro中非特殊使用局部变量--> <#local username="王五" /> ${username} </#macro> ${username} <#--输出"李四"--> <#--循环变量--> <#list 1..10 as username> <#--循环中的变量,出了循环就消失了--> ${username} </#list> ${username} <#--输出"李四"--> </body> </html>
test10
@Test public void test10(){ Map<String,Object> root = new HashMap<String,Object>(); root.put("username", "张三"); fu.print("10.ftl", root); fu.fprint("10.ftl", root, "G:\\studyDemo\\freemarker\\10.html"); }
11. import指令
11.ftl
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <#-- 使用incldue可能会出现覆盖的问题,可以使用import来完成导入,并且加入名称空间 <#include "/inc/inc1.ftl"/> <#include "/inc/inc2.ftl"/> --> <#import "/inc/inc2.ftl" as inc2/> <#import "/inc/inc1.ftl" as inc1/> ${inc2.username} ${inc1.username} <#--将一个变量定义到名称空间中--> <#assign age=12 in inc2/> ${inc2.age} <#--访问名称空间中的自定义指令--> <@inc1.test/> </body> </html>
test11
@Test public void test11() { Map<String,Object> root = new HashMap<String,Object>(); fu.print("11.ftl", root); }
输出
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 李四 张三 12 hello world </body> </html>
相关推荐
freemarker详解
Freemarker简介及标签详解大全 Freemarker简介及标签使用说明
FreeMarker允许Java servlet保持图形设计同应用程序逻辑的分离,这是通过在模板中密封HTML完成的。模板用servlet提供的数据动态地生成 HTML。模板语言是强大的直观的,编译器速度快,输出接近静态HTML页面的速度。
很全的freemarker标签详解分享资源
本篇文章主要介绍了springboot整合freemarker详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
freemarker详解
FreeMarker详解,内包括freemarker教程_中文版、freemarker中文参考手册、FreeMarker标签使用、freemarker学习文档、freemarker根据模版生成文件使用例子 等文档
Freemarker简介及标签详解大全.pdf
FreeMarker模板文件的组成及基本语法详解(一)
学习Freemarker简介及标签详解大全.pdf
此案例实现了freemarker与spring框架的整合,并详细说明如何将视图层从jsp改为ftl模板
Freemarker标签详解(一个不漏)
主要介绍了java Freemarker页面静态化实例详解的相关资料,需要的朋友可以参考下
包含freemarker 快速入门详解 及 中文手册
spring MVC详解,FreeMarker+SpringMVC
PDF格式中文版手册,是学习FreeMarker的必备手册。freemark 开发指南
在本篇内容里小编给大家整理的是关于构建SpringBoot+MyBatis+Freemarker的项目的具体步骤以及实例代码,需要的朋友们参考下。
FreeMarker.pdf 页面静态化详解,看了还不错