我们要学习freemarker,就要学习它的语法,下面我会为大家详细介绍freemarker的常用语法。
访问map中的key
上文已经介绍过了,在此并不过多赘述。
访问pojo中的属性
首先创建一个pojo类——Student.java,如下图所示。
为了方便大家复制,现将Student类的代码给出。
public class Student {
private int id;
private String name;
private int age;
private String address;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Student() {
}
public Student(int id, String name, int age, String address) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
}
下面我们便针对这个pojo来创建一个新的模板first.htm,如下图所示。
为方便大家复制,现将first.htm模板的内容给出。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>${title}</title>
</head>
<body>
学生的姓名:${stu.name}
</body>
</html>
下面对FreemarkerTest单元测试类的testFreemarkerFirst方法做下修改,如下图所示。
为方便大家复制,现将FreemarkerTest单元测试类的代码给出。
public class FreemarkerTest {
@Test
public void testFreemarkerFirst() throws Exception {
// 创建一个Configuration对象
Configuration configuration = new Configuration(Configuration.getVersion());
// 设置模板所在的目录
configuration.setDirectoryForTemplateLoading(new File("F:/Java/my-taotao/taotao-item-web/src/main/resources/ftl"));
// 设置模板字符集
configuration.setDefaultEncoding("UTF-8");
// 加载模板文件
// Template template = configuration.getTemplate("hello.ftl");
Template template = configuration.getTemplate("first.htm");
// 创建一个数据集
Map data = new HashMap();
// data.put("hello", "Hello Freemarker!!!");
data.put("title", "Hello Freemarker!!!");
data.put("stu", new Student(1, "小明", 18, "温都水城"));
// 设置模板输出的目录及输出的文件名
// FileWriter writer = new FileWriter(new File("F:/temp/freemarker/hello.html"));
FileWriter writer = new FileWriter(new File("F:/temp/freemarker/first.html"));
// 生成文件
template.process(data, writer);
// 关闭流
writer.close();
}
}
运行上面的测试方法后会在F:/temp/freemarker目录下生成一个first.html页面,我们双击在浏览器中查看,可以正常看到pojo类中的属性,如下图所示。
取集合中的数据
首先对FreemarkerTest单元测试类中的testFreemarkerFirst方法做下修改,在该测试方法中添加一个集合,如下图所示。
为方便大家复制,现将FreemarkerTest单元测试类的代码给出。
public class FreemarkerTest {
@Test
public void testFreemarkerFirst() throws Exception {
// 创建一个Configuration对象
Configuration configuration = new Configuration(Configuration.getVersion());
// 设置模板所在的目录
configuration.setDirectoryForTemplateLoading(new File("F:/Java/my-taotao/taotao-item-web/src/main/resources/ftl"));
// 设置模板字符集
configuration.setDefaultEncoding("UTF-8");
// 加载模板文件
// Template template = configuration.getTemplate("hello.ftl");
Template template = configuration.getTemplate("first.htm");
// 创建一个数据集
Map data = new HashMap();
// data.put("hello", "Hello Freemarker!!!");
data.put("title", "Hello Freemarker!!!");
data.put("stu", new Student(1, "小明", 18, "温都水城"));
List<Student> stuList = new ArrayList<Student>();
stuList.add(new Student(1, "小明1", 18, "温都水城"));
stuList.add(new Student(2, "小明2", 17, "温都水城"));
stuList.add(new Student(3, "小明3", 16, "温都水城"));
stuList.add(new Student(4, "小明4", 15, "温都水城"));
stuList.add(new Student(5, "小明5", 14, "温都水城"));
stuList.add(new Student(6, "小明6", 13, "温都水城"));
stuList.add(new Student(7, "小明7", 12, "温都水城"));
data.put("stuList", stuList);
// 设置模板输出的目录及输出的文件名
// FileWriter writer = new FileWriter(new File("F:/temp/freemarker/hello.html"));
FileWriter writer = new FileWriter(new File("F:/temp/freemarker/first.html"));
// 生成文件
template.process(data, writer);
// 关闭流
writer.close();
}
}
接着我们要循环取集合中的数据,语法为:<#list 集合 as 循环的变量></#list>
,例如:
<#list studentList as student>
${student.id}/${studnet.name}
</#list>
所以,我们应将first.htm模板修改为:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>${title}</title>
</head>
<body>
学生的姓名:${stu.name}
<br/>
学生列表:
<br/>
<table border="1">
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>家庭住址</th>
</tr>
<#list stuList as student>
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.address}</td>
</tr>
</#list>
</table>
</body>
</html>
运行testFreemarkerFirst测试方法,成功后,我们再刷新静态网页,如下图所示,正常展示出了学生列表。
取循环中的下标
对于列表展示,如果我们想获取行的下标的话,用freemarker很容易实现,为了直观的查看下标,我们在模板的列表展示中添加一列”序号”,值的话就直接使用当前对象及下划线和index便可以获取,如下图所示。
为方便大家复制,现将first.htm模板的内容给出。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>${title}</title>
</head>
<body>
学生的姓名:${stu.name}
<br/>
学生列表:
<br/>
<table border="1">
<tr>
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>家庭住址</th>
</tr>
<#list stuList as student>
<tr>
<td>${student_index}</td>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.address}</td>
</tr>
</#list>
</table>
</body>
</html>
修改完后,我们重新运行测试方法,然后重新刷新静态页面,可以看到正确展示出了每行的下标。
if条件的使用
在列表展示当中经常要用到隔行变色的效果,隔行变色就要用到if判断,如果是奇数行就用某种颜色,偶数行就用另外一种颜色。比如我现在想把奇数行背景颜色变为蓝色,把偶数行背景颜色变为红色,语法为:
<#if student_index % 2 == 0>
...
<#else>
...
</#if>
这样,我们就要修改first.htm模板了,修改之后如下图所示。
为方便大家复制,现将first.htm模板的内容给出。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>${title}</title>
</head>
<body>
学生的姓名:${stu.name}
<br/>
学生列表:
<br/>
<table border="1">
<tr>
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>家庭住址</th>
</tr>
<#list stuList as student>
<#if student_index % 2 == 0>
<tr bgcolor="blue">
<#else>
<tr bgcolor="red">
</#if>
<td>${student_index}</td>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.address}</td>
</tr>
</#list>
</table>
</body>
</html>
修改完后重新运行测试方法,然后重新刷新静态页面,可以看到已经展现出隔行变色的效果了。
日期类型格式化
我们先来看默认情况下日期的展示效果,在FreemarkerTest单元测试类的testFreemarkerFirst方法中添加一行代码(把日期加入map当中),如下图所示。
然后在first.htm模板中把日期取出来,如下图所示。
修改完后,运行testFreemarkerFirst测试方法,会看到如下图所示的错误,告诉我们无法将日期转换为string类型,并且在”Tip”当中告诉了我们可以通过在date后面加?date或?time或?datetime来将日期类型转换为string。
既然告诉了我们应该怎么办,我们就按照上面的提示,在模板的date后面加?date或?time或?datetime,如下所示。
当前日期:${date?date}
<br/>
当前时间:${date?time}
<br/>
当前日期和时间:${date?datetime}
修改完后运行testFreemarkerFirst测试方法,然后重新刷新静态网页,可以看到如下图所示界面,在页面最下方展示出了我们要看到的东西。
从上图可以发现,展示的日期格式是比较固定的,那么如果我们想按自己想要的方式展示日期,该怎么办呢?这个其实刚才报错信息中已经给出提示信息了,如下图所示,明确的告诉了我们可以在date后面加?string('yyyy/MM/dd HH:mm:ss')
来展示我们的日期。
再在first.htm模板中添加如下代码:
<br/>
自定义日期格式:${date?string("yyyy/MM/dd HH:mm:ss")}
重新运行testFreemarkerFirst测试方法并重新刷新静态网页,如下图所示,发现正常按照我们的想法进行展示了。
Null值的处理
在页面展示时有可能遇到null值的处理,如果我们展示一个不存在的变量的话,就会变错,如下图所示,在最下方添加展示不存在的变量。
运行testFreemarkerFirst方法,会出现如下图所示错误,错误消息提示展示的内容是null或者不存在,要解决这个问题,”Tip”给出了提示信息,告诉我们可以在变量后面加”!默认值”。
然后在first.htm模板中对于变量的展示做以下修改:
null值的测试:${mytest!"默认值"}
再次运行测试方法并重新刷新静态页面,可以看到正常展示出了”默认值”,如下图所示。
还有一种处理null值的方法是使用if判断。这时,我们就要修改first.htm模板了,修改之后如下图所示。
运行测试方法,然后重新刷新静态页面,可以看到输出了”mytest为null”。
我们现在让mytest变量有值,方法是在testFreemarkerFirst测试方法中添加上data.put("mytest", 123);
这句代码,如下图所示。
再次运行测试方法然后重新刷新静态页面,可以看到mytest已经有值了。
include标签
静态页面中最常见的是页眉和页脚的内容是固定的,这意味着我们不必要重复写页眉页脚,只需要各写一遍即可,然后某个页面需要的时候引入进来就可以了。在这里,我举个例子,把上文所写的模板include进来,如下图所示。
为方便大家复制,现将first.htm模板的内容给出。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>${title}</title>
</head>
<body>
<#include "hello.ftl" />
<br/>
学生的姓名:${stu.name}
<br/>
学生列表:
<br/>
<table border="1">
<tr>
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>家庭住址</th>
</tr>
<#list stuList as student>
<#if student_index % 2 == 0>
<tr bgcolor="blue">
<#else>
<tr bgcolor="red">
</#if>
<td>${student_index}</td>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.address}</td>
</tr>
</#list>
</table>
<br/>
当前日期:${date?date}
<br/>
当前时间:${date?time}
<br/>
当前日期和时间:${date?datetime}
<br/>
自定义日期格式:${date?string("yyyy/MM/dd HH:mm:ss")}
<br/>
null值的测试:${mytest!""}<br/>
null值的测试:${mytest!}<br/>
null值的测试:${mytest!"默认值"}
<br/>
null值判断:
<#if mytest??>
mytest不为null,值为:${mytest}
<#else>
mytest为null
</#if>
</body>
</html>
在运行testFreemarkerFirst测试方法之前,将该测试方法中的data.put("hello", "Hello Freemarker!!!");
这句代码的注释去掉,如下图所示。
然后再运行测试方法,接着重新刷新静态页面,如下图所示,可以看到正常展示出所包含的内容了。
有了这些语法,我们便可以制作出各种各样的模板了。