页面静态化技术----freemarker
0.什么是freemarker
FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
简单一点静态化技术就是模板+数据,freemarker并不关心数据的来源,只是根据模板的内容,将数据模型在模板中显示并输出文件。
1.freemarker测试环境
本项目基于SpringBoot构建测试工程。
配置文件,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会有缓存不方便进行模板测试
数据模型
@Data
@ToString
public class Student {
private String name;//姓名
private int age;//年龄
private Date birthday;//生日
private Float mondy;//钱包
private List<Student> friends;//朋友列表
private Student bestFriend;//最好的朋友
}
test.ftl文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello World!</title>
</head>
<body>
${name}!
</body>
</html>
测试controller类
@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {
@RequestMapping("/test1")
public String freemarker(Map map){
//向数据模型放数据
map.put("name","测试数据");
//返回模板文件名称
return "test1";
}
创建启动类
@SpringBootApplication
public class FreemarkerTestApplication {
public static void main(String[] args) {
SpringApplication.run(FreemarkerTestApplication.class,args);
}
}
2.Freemarker语法基础
2.1list标签
<table>
<tr>
<td>序号</td>
<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.bestFriend.name)!}</td>
</tr>
</#list>
</#if>
</table>
2.2map数据的遍历
输出学生的信息:<br/>
姓名:${stuMap.stu1.name}<br/>
年龄:${stuMap.stu1.age}<br/>
遍历输出两个学生的信息<br/>
<#list stuMap?keys as k>
<tr>
<td>${k_index+1}</td>
<td>${stuMap[k].name}</td>
<td>${stuMap[k].age}</td>
<td>${stuMap[k].mondy}</td>
</tr>
</#list>
2.3if指令
<#list stus as stu>
<tr>
<td>${stu_index+1}</td>
<td <#if stu.name =='小明'>style="background: red" </#if>>${stu.name}</td>
<td>${stu.age}</td>
<td <#if (stu.mondy>500 )>style="background: red" </#if>>${stu.mondy}</td>
<td>${(stu.bestFriend.name)!}</td>
</tr>
</#list>
2.4其他指令
2.4.1运算符指令
1、算数运算符 FreeMarker表达式中完全支持算术运算,FreeMarker支持的算术运算符包括:+, - , * , / , %
2、逻辑运算符 逻辑运算符有如下几个: 逻辑与:&& 逻辑或:|| 逻辑非:! 逻辑运算符只能作用于布尔值,否则将产生错误
3、比较运算符 表达式中支持的比较运算符有如下几个: 1 =或者==:判断两个值是否相等. 2 !=:判断两个值是否不等. 3 >
或者gt:判断左边值是否大于右边值
4 >=或者gte:判断左边值是否大于等于右边值
5 <或者lt:判断左边值是否小于右边值
6 <=或者lte:判断左边值是否小于等于右边值
注意: =和!=可以用于字符串,数值和日期来比较是否相等,但=和!=两边必须是相同类型的值,否则会产生错误,而且
FreeMarker是精确比较,“x”,"x ","X"是不等的.其它的运行符可以作用于数字和日期,但不能作用于字符串,大部分的时
候,使用gt等字母运算符代替>会有更好的效果,因为 FreeMarker会把>解释成FTL标签的结束字符,当然,也可以使用括
号来避免这种情况,如:<#if (x>y)>
2.4.2空值处理
判断某变量是否存在使用 “??” 用法为:variable??,如果该变量存在,返回true,否则返回false
<#if stus??>
<#list stus as stu>
<tr>
<td>${stu_index+1}</td>
<td <#if stu.name =='小明'>style="background: red" </#if>>${stu.name}</td>
<td>${stu.age}</td>
<td <#if (stu.mondy>500 )>style="background: red" </#if>>${stu.mondy}</td>
<td>${(stu.birthday?string("yyyy-MM-dd"))!""}</td>
</tr>
</#list>
</#if>
缺失变量默认值使用 “!” 使用!要以指定一个默认值,当变量为空时显示默认值。
<td>${(stu.bestFriend.name)!''}</td>
2.4.3内建函数
内建函数语法格式: 变量+?+函数名称
1、和到某个集合的大小
${stus?size}
2、日期格式化
显示年月日: ${today?date}
显示时分秒:${today?time}
显示日期+时间:${today?datetime} <br>
自定义格式化: ${today?string("yyyy年MM月")}
3、内建函数c
map.put(“point”, 102920122);
point是数字型,使用${point}会显示这个数字的值,不并每三位使用逗号分隔。如果不想显示为每三位分隔的数字,可以使用c函数将数字型转成字符串输出
${point?c}
4、将json字符串转成对象
其中用到了 assign标签,assign的作用是定义一个变量
<#--json转化对象-->
<#assign text="{'bank':'工商银行','account':'10101920201920212'}" />
<#assign data=text?eval/>
开户行:${data.bank} 账号:${data.account}