今天分享一下一个模板语言的使用,它就是Freemarker,有点类似与前些日子做Python的Django中的模板语言,其实原理上都是相似的。所以这里就不对那些基础性的语法类的直至进行讲解了,就拿几个实用的小例子来分析分析。
依赖
我们需要导入一个jar包,名为freemarker.jar
。随便到网上下载一个就行,而且对其他诸如servlet等没有依赖,所以我们可以很轻松的进行移植操作。
工具类FreemarkerUtil.java
package main;import java.io.File;import java.io.FileWriter;import java.io.PrintWriter;import java.util.Map;import freemarker.template.Configuration;import freemarker.template.Template;public class FreemarkerUtil { /** * 根据给定的ftl(freemarker template language)来获得一个用于操作的模板 * @param name * @return */ public Template getTemplate(String name) { try { // 通过Freemark而的Configuration读取到相应的模板ftl Configuration cfg = new Configuration(); // 设定去哪里读取相关的模板FTL文件 cfg.setClassForTemplateLoading(this.getClass(), "/ftl"); // 在模板文件目录中找到名为name的文件 Template template = cfg.getTemplate(name); return template != null ? template : null; } catch (Exception e) { throw new RuntimeException(e); } } /** * 通过控制台输出文件信息 * * @param name * @param root */ public void print(String name, Map<String, Object> root) { try { // 通过Template可以将模板文件输出到相应的流 Template template = this.getTemplate(name); template.process(root, new PrintWriter(System.out)); } catch (Exception e) { throw new RuntimeException(e); } } /** * 输出为HTML文件 * * @param name * @param root * @param outfile */ public void htmlprint(String name, Map<String, Object> root, String outfile) { FileWriter writer = null; try { // 使用一个路径实现将文件的输出 writer = new FileWriter(new File("src/"+ outfile)); Template template = this.getTemplate(name); template.process(root, writer); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
案例分析一
我使用的第一个简单的模板01.ftl如下:
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>测试</title></head><body><h1>你好${username}</h1></body></html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
测试代码如下:
/** * 仅仅针对有一个数据的测试 * * @throws Exception */ @Test public void testftl1() throws Exception { FreemarkerUtil util = new FreemarkerUtil(); Template template = util.getTemplate("01.ftl"); Map<String, Object> map = new HashMap<String, Object>(); map.put("username", "XIAO MARK"); // 默认输出到了控制台上 template.process(map, new OutputStreamWriter(System.out)); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
案例分析二
使用到的模板03.ftl
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h1>${user.id}-----${user.name}-----${user.age}</h1><#if user.age lt 12> ${user.name}还是一个小孩<#elseif user.age lt 18> ${user.name}快成年<#else> ${user.name}已经成年</#if></body></html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
我们可以从模板中看到user.id,那就说明我们使用到了对象,所以UserBean 详情如下。
package main;import java.io.Serializable;public class User implements Serializable { private int id; private String name; private int age; private Group group; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", group=" + group + "]"; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
内有组合类Group的使用,
package main;import java.io.Serializable;public class Group implements Serializable{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Group [name=" + name + "]"; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
测试代码如下:
/** * 控制台输出带有对象的模板使用案例 * * @throws Exception */ @Test public void testftl3() throws Exception { FreemarkerUtil util = new FreemarkerUtil(); Template template = util.getTemplate("03.ftl"); Map<String, Object> map = new HashMap<String, Object>(); User user = new User(); user.setId(1); user.setName(" 妈的智障 "); user.setAge(21); map.put("user", user); template.process(map, new OutputStreamWriter(System.out)); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
/** * 输出HTML文件形式的带有对象的测试案例 * * @throws Exception */ @Test public void testftl3outtofile() throws Exception { FreemarkerUtil util = new FreemarkerUtil(); Template template = util.getTemplate("03.ftl"); Map<String, Object> map = new HashMap<String, Object>(); User user = new User(); user.setId(1); user.setName(" 妈的智障 "); user.setAge(21); map.put("user", user); util.htmlprint("03.ftl", map, "./../page/03ftloutfile.html"); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
案例分析三
使用到的模板05.ftl如下:
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><hr/><#list users as user>${user.id}---------${user.name}-------${user.age}<br/></#list></body></html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
测试代码如下:
/** * 输出HTML文件形式的带有对象的测试案例 * * @throws Exception */ @Test public void testftl5outtofile() throws Exception { FreemarkerUtil util = new FreemarkerUtil(); Template template = util.getTemplate("03.ftl"); Map<String, Object> map = new HashMap<String, Object>(); List<User> users = new ArrayList<User>(); for (int i = 1; i <= 10; i++) { User user = new User(); user.setId(i); user.setName(" 妈的智障 " + (i * i)); user.setAge((int) (Math.random() * 100)); users.add(user); } map.put("users", users); util.htmlprint("05.ftl", map, "./../page/05ftloutfile.html"); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
案例分析四
使用到的模板文件06.ftl如下:
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>${user.id}-------${user.name}------${user.group!} <#-- !后为空就不输出 --><#--${user.group.name!}--><#-- 按照以上的方式加! freemarker仅仅只会判断group.name是不是空值 -->${(user.group.name)!"1234"} ${(a.b)!"没有a.b元素"}<#--!:指定缺失变量的默认值 ??:判断某个变量是否存在,返回boolean值 --><#if (a.b)??> <#--if后不用加$--> 不为空<#else> 为空</#if></body></html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
测试代码如下:
@Test public void testftl6() throws Exception { FreemarkerUtil util = new FreemarkerUtil(); Map<String,Object > map = new HashMap<String,Object>(); User user = new User(); Group group = new Group(); group.setName("1234"); user.setId(28); user.setAge(27); user.setName(" 妈的智障 "); user.setGroup(group); map.put("user", user); util.print("06.ftl", map); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
程序运行结果浏览
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow