基础语法种类
1、注释,即<#‐‐和‐‐>,介于其之间的内容会被freemarker忽略
2、插值(Interpolation):即
.
.
部
分
,
f
r
e
e
m
a
r
k
e
r
会
用
真
实
的
值
代
替
{..}部分,freemarker会用真实的值代替
..部分,freemarker会用真实的值代替{…}
3、FTL指令:和HTML标记类似,名字前加#予以区分,Freemarker会解析标签中的表达式或逻辑。
4、文本,仅文本信息,这些不是freemarker的注释、插值、FTL指令的内容会被freemarker忽略解析,直接输出内容
list指令
模板:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf‐8">
<title>Hello World!</title>
</head>
<body>
Hello ${name}!
<br/>
<#--遍历数据模型中的list学生信息(数据模型中的名称为stus)-->
<table>
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
<td>金额</td>
</tr>
<#--_index:得到循环的下标,使用方法是在stu后边加"_index",它的值是从0开始-->
<#list stus as stu>
<tr>
<td>${stu_index + 1}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.money}</td>
</tr>
</#list>
</table>
</body>
</html>
数据模型:
@RequestMapping("/freemarker")
@Controller
public class FreemarkerController {
//测试1
@RequestMapping("/test1")
public String test1(Model model){
//向数据模型放数据
model.addAttribute("name","三年二班");
Student stu1 = new Student();
stu1.setName("小明");
stu1.setAge(18);
stu1.setMoney(1000.86f);
stu1.setBirthday(new Date());
Student stu2 = new Student();
stu2.setName("小红");
stu2.setMoney(200.1f);
stu2.setAge(19);
//向数据模型放数据
model.addAttribute("stus",stus);
//返回模板文件名称
return "test1";
展示效果: