1.pom文件引入依赖
<!--framework 模板依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency>
<!--rapid-framework 模板继承框架-->
<dependency>
<groupId>com.googlecode.rapid-framework</groupId>
<artifactId>rapid-core</artifactId>
<version>4.0.5</version>
</dependency>
<!-- lang包 缺少的话可能会报错 -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
2.yml文件配置
spring:
freemarker:
charset: UTF-8
check-template-location: true
template-loader-path: classpath:/templates
suffix: .html
request-context-attribute: request
3.config配置
import cn.org.rapid_framework.freemarker.directive.BlockDirective;
import cn.org.rapid_framework.freemarker.directive.ExtendsDirective;
import cn.org.rapid_framework.freemarker.directive.OverrideDirective;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
/**
* <p>
* Freemarker配置
* </p>
*
* @author lzx
* @since 2020/06/10
*/
@Configuration
public class FreemarkerConfig {
@Autowired
freemarker.template.Configuration configuration;
@PostConstruct
public void setSharedVariable(){
configuration.setSharedVariable("block", new BlockDirective());
configuration.setSharedVariable("override", new OverrideDirective());
configuration.setSharedVariable("extends", new ExtendsDirective());
}
}
4.Freemarker 继承用法
- Freemarker 通过 rapid java实现继承。实际是rapid的jar包提供的三个自定义标签。实现继承用到的有三个标签:@extends,@block ,@override. 他们三个都有一个共同的属性: name
@extend标签: 要继承的模板
@block 标签: 声明在一个模板中定义那些代码是可以被重写的(@ovrride)
@override标签: 选择要重写的代码块
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
<@block name="title">父模板的 title</@block>
</title>
<@block name="css"></@block>
</head>
<body>
<div>
<@block name="main">主内容</@block>
</div>
</body>
<@block name="dialog"></@block>
<@block name="js"></@block>
<@block name="script"></@block>
</html>
<@override name="title">
子模版的 title
</@override>
<@override name="css">
<!-- 子模版的 css -->
<style>
.title{
color: #000;
}
</style>
</@override>
<@override name="script">
<!-- 子模版的 script -->
<script>
location.reload();
</script>
</@override>
<@override name="main">
<div>子模版的 body</div>
</@override>
<!--继承的模板要写在最下面-->
<@extends name="layout.html"/>
5.Freemarker 页面跳转
//当前端请求/或者index.html资源时返回templates/index/index.html并向页面传输name参数
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class IndexController {
@GetMapping({"/","/index.html"})
public ModelAndView index(){
modelAndView.setViewName("index/index");
modelAndView.addObject("name","老铁");
return modelAndView;
}
}
//templates/index/index.html获取参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
Hello,${name}
</div>
</body>
</html>
6.Freemarker 语法
// 取值
${key.property}
// 遍历集合
<#list studentList as student>
<tr>
<td>${student.id}</td>
<td>${studnet.name}</td>
//取下标的值
<td>${student_index}</td>
</tr>
</#list>
// if判断
<#if student_index % 2 == 0>
<#else>
...
</#if>
//为空判断
${val!"val的值为null"}<br>
判断val的值是否为null:<br>
<#if val??>
val中有内容
<#else>
val的值为null
</#if>
//日期
// 可以使用?date,?time,?datetime,?string(parten)
当前日期:${date?string("yyyy/MM/dd HH:mm:ss")}
//Include标签
<#include "hello.ftl">