public String test1(Map<String, Object> map){//形参map,放在springmvc的形参最终request会响应给用户,也就是这个map虽然只是放在形参,但是request域里面的数据会拿到这些map信息,也就是freeMark在生成模板的时候就可以拿到map里的数据。
//map就是freeMark模板所使用的数据
map.put("name","java程序员");
Student stu1 = new Student();
stu1.setName("小明");
stu1.setAge(18);
stu1.setMondy(1000.86f);
stu1.setBirthday(new Date());
Student stu2 = new Student();
stu2.setName("小红");
stu2.setMondy(200.1f);
stu2.setAge(19);
// stu2.setBirthday(new Date());
//朋友列表
List<Student> friends = new ArrayList<>();
friends.add(stu1);
//给第二个学生设置朋友列表
stu2.setFriends(friends);
//给第二个学生设置最好的朋友
stu2.setBestFriend(stu1);
//学生列表
List<Student> stus = new ArrayList<>();
stus.add(stu1);
stus.add(stu2);
//将学生列表放入map数据模型中。向数据模型放数据
map.put("stus",stus);
//准备map数据
HashMap<String,Student> stuMap = new HashMap<>();
stuMap.put("stu1",stu1);//"stu1"是key
stuMap.put("stu2",stu2);//"stu2"是key
//向数据模型放数据
map.put("stu1",stu1);
//向数据模型放数据
map.put("stuMap",stuMap);
//模板文件名称:返回freemark模板位置,基于resource/templates路径。
return "test1";//如何test1在resource/templates/aa/test1.ftl则这里写成return "aa/test1";
}
<table>
<tr>
<td>姓名</td>
<td>年龄</td>
<td>出生日期</td>
<td>钱包</td>
<td>最好的朋友</td>
<td>朋友个数</td>
</tr>
<#if stus??>
<#list stus as stu>
<tr>
<td>${stu.name!''}</td>
<td>${stu.age}</td>
<td>${(stu.birthday?date)!''}---------${(stu.birthday?string("YYYY年MM月dd日"))!''}</td>
<td>${stu.mondy}</td>
<td>${(stu.bestFriend.name)!''}</td>
<td>${(stu.friends?size)!0}</td>
</tr>
</#list>
</#if>
</table>
内建函数
内建函数语法格式: 变量+?+函数名称
1、和到某个集合的大小 ${集合名?size}
2、日期格式化
显示年月日: ${today?date}
显示时分秒:${today?time}
显示日期+时间:${today?datetime} <br>
自定义格式化: ${today?string("yyyy年MM月")}
3、内建函数c
map.put("point", 102920122);
point是数字型,使用${point}会显示这个数字的值,不并每三位使用逗号分隔。 如果不想显示为每三位分隔的数字,可以使用c函数将数字型转成字符串输出 ${point?c}
<br>
${point}
<br>
${point?c}
4、将json字符串转成对象
一个例子:
其中用到了 assign标签,assign的作用是定义一个变量。
<#assign text="{'bank':'工商银行','account':'10101920201920212'}" />
<#assign data=text?eval />
开户行:${data.bank} 账号:${data.account}