freemarker 项目案例 可以看下https://github.com/binlian/binglian-blog
这个项目就是用的freemarker模板来写的
准备工作
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>xc-framework-parent</artifactId>
<groupId>com.xuecheng</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../xc-framework-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-freemarker</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 8088 #服务端口
spring:
application:
name: test-freemarker #指定服务名
freemarker:
cache: false #关闭模板缓存,方便测试
settings:
template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
实体类对象
package com.xuecheng.test.freemarker.model;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
import java.util.List;
/**
* @author Administrator
* @version 1.0
* @create 2018-06-13 8:24
**/
@Data
@ToString
public class Student {
private String name;//姓名
private int age;//年龄
private Date birthday;//生日
private Float mondy;//钱包
private List<Student> friends;//朋友列表
private Student bestFriend;//最好的朋友
}
package com.xuecheng.test.freemarker.controller;
import com.xuecheng.test.freemarker.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import java.util.*;
/**
* @author Administrator
* @version 1.0
* @create 2018-06-12 18:40
**/
@RequestMapping("/freemarker")
@Controller //这里不要使用@RestController.要输出html网页,restController输出的是json
public class FreemarkerController {
@RequestMapping("/test1")
public String test1(Map<String,Object> map){
//map就是freemarker模板所使用的数据
map.put("name","冰怜");
//返回freemarker模板的位置。基于resources/templates路径的
return "test1";
}
}
freemarker前端代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello World!</title>
</head>
<body>
Hello ${name}!
</body>
</html>
list
package com.xuecheng.test.freemarker.controller;
import com.xuecheng.test.freemarker.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import java.util.*;
/**
* @author Administrator
* @version 1.0
* @create 2018-06-12 18:40
**/
@RequestMapping("/freemarker")
@Controller //这里不要使用@RestController.要输出html网页,restController输出的是json
public class FreemarkerController {
@RequestMapping("/test1")
public String test1(Map<String,Object> map){
//map就是freemarker模板所使用的数据
map.put("name","冰怜");
Student student1=new Student();
student1.setName("小明");
student1.setAge(18);
student1.setMondy(1000.86f);
student1.setBirthday(new Date());
Student student2=new Student();
student2.setName("小红");
student2.setMondy(200.1f);
student2.setAge(19);
student2.setBirthday(new Date());
//朋友列表
List<Student> friends=new ArrayList<>();
friends.add(student1);
//给第二名学生设置朋友列表
student2.setFriends(friends);
//给第二名学生设置最好朋友
student2.setBestFriend(student1);
List<Student> students=new ArrayList<>();
students.add(student1);
students.add(student2);
//将学生列表放在数据模型中
map.put("stus",students);
//准备map数据
HashMap<String,Student> stuMap=new HashMap<>();
stuMap.put("stu1",student1);
stuMap.put("stu2",student2);
//向数据模型map放数据
map.put("stu1",student1);
//向数据模型放数据
map.put("stuMap",stuMap);
//返回freemarker模板的位置。基于resources/templates路径的
return "test1";
}
}
<#list stus as stu>
<tr>
<td>${stu_index+1}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.mondy}</td>
<#--<td>${stu.birthday}</td>-->
</tr>
</#list>
这里的stu_inde是下标 模板的 0开始 所以这里+1
_index:得到循环的下标,使用方法是在stu后边加"_ind
<!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>
<td>出生日期</td>
</tr>
<#list stus as stu>
<tr>
<td>${stu_index+1}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.mondy}</td>
<#--<td>${stu.birthday}</td>-->
</tr>
</#list>
</table>
<br/>
<br/>
数据模型中的stuMap(map数据),第一种方法:在中括号中添加map的key,第二种方法:在map后边直接加 "点key"
</br>
姓名:${stuMap['stu1'].name}<br/>
年龄:${stuMap['stu1'].age}<br/>
姓名:${stuMap.stu1.name}<br/>
年龄:${stuMap.stu1.age}<br/>
遍历map中的key.stuMap?keys就是key列表(是一个list)
<br/>
<#list stuMap ?keys as k>
姓名:${stuMap[k].name}<br/>
年龄:${stuMap[k].age}<br/>
</#list>
</body>
</html>
map
数据模型中的stuMap(map数据),第一种方法:在中括号中添加map的key,第二种方法:在map后边直接加 "点key"
</br>
姓名:${stuMap['stu1'].name}<br/>
年龄:${stuMap['stu1'].age}<br/>
姓名:${stuMap.stu1.name}<br/>
年龄:${stuMap.stu1.age}<br/>
遍历map中的key.stuMap?keys就是key列表(是一个list)
<br/>
<#list stuMap ?keys as k>
姓名:${stuMap[k].name}<br/>
年龄:${stuMap[k].age}<br/>
</#list>
if 指令
即判断指令,是常用的FTL指令,freemarker在解析时遇到if会进行判断,条件为真则输出if中间的内容,否 则跳过内容不再输出。
判断stu.name是不是小明 样式就变
<#list stus as stu>
<tr>
<td>${stu_index+1}</td>
<td
<#if stu.name=="小明"> style="background:cornflowerblue;" </#if>
>
${stu.name}
</td>
<td>${stu.age}</td>
<td>${stu.mondy}</td>
<#--<td>${stu.birthday}</td>-->
</tr>
</#list>
表达式中支持的比较运算符有如下几个:
1 =或者==判断两个值是否相等
2 !=判断两个值是否不等
3 >或者gte判断左边值是否大于右边值
4 >=或者gte:判断左边值是否小于右边值
5 <或者lt :判断做编制是否小于右边值
6 <=或者lte:判断左边值是否小于等于右边值
stu.mondy gt 300大于300 一样的边样式
<#list stus as stu>
<tr>
<td>${stu_index+1}</td>
<td
<#if stu.name=="小明"> style="background:cornflowerblue;" </#if>
>
${stu.name}
</td>
<td>${stu.age}</td>
<td
<#if stu.mondy gt 300>
style="background:cornflowerblue"
</#if>
>${stu.mondy}</td>
<#--<td>${stu.birthday}</td>-->
</tr>
</#list>
??判断为空
<#if stuMap?? && stuMap.stu1??>
姓名:${stuMap['stu1'].name}<br/>
</#if>
内建函数
内建函数语法格式:变量+?+函数名称
1.和到某个集合的大小
${集合名?size}
2、日期格式化
显示年月日: ${today?date}
显示时分秒:${today?time}
显示日期+时间:${today?datetime}
自定义格式化: ${today?string("yyyy年MM月")}
3、内建函数c
map.put("point", 102920122);
point是数字型,使用${point}会显示这个数字的值,不并每三位使用逗号分隔。
如果不想显示为每三位分隔的数字,可以使用c函数将数字型转成字符串输出 ${point?c}
4、将json字符串转成对象 一个例子: 其中用到了 assign标签,assign的作用是定义一个变量。
<#assign text="{'bank':'工商银行','account':'10101920201920212'}" />
<#assign data=text?eval /> 开户行:${data.bank} 账号:${data.account}
这里使用内置函数 size 和date 这里时间如果不使用?date 就会报错
<#list stus as stu>
<tr>
<td>${stu_index+1}</td>
<td
<#if stu.name=="小明"> style="background:cornflowerblue;" </#if>
>
${stu.name}
</td>
<td>${stu.age}</td>
<td
<#if stu.mondy gt 300>
style="background:cornflowerblue"
</#if>
>${stu.mondy}</td>
<td>${stu.birthday?date}</td>
<#--或者 ${stu/birthday?string("yyyy年MM月")}}-->
</tr>
</#list>
<br/>
学生的个数:${stus?size}
<br/>