theme: smartblue
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
application.yml
spring: freemarker: cache: false #关闭模板缓存,方便测试 settings: #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试 template_update_delay: 0 suffix: .ftl #指定Freemarker模板文件的后缀名 template-loader-path: classpath:/templates
案例一:加载单个属性、类
目录结构
controller
``` @Controller public class HelloController {
@GetMapping("/basic")
public String hello(Model model){
// name
model.addAttribute("name","xyy");
// stu
Student student = new Student();
student.setName("xyz");
model.addAttribute("stu",student);
return "01-basic";
}
} ```
前端模板01-basic.ftl
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Hello World!</title> </head> <body> <b>普通文本 String 展示:</b> <br> <br>Hello ${name!"空值"} <br> <hr> <b>对象Student中的数据展示:</b> <br/> 姓名:${(stu.name)!"姓名空值"}<br/> 年龄:${(stu.age)!"年龄空值"} <hr> </body> </html>
页面展示
案例二:加载Map、List
Controller
``` package com.heima.freemarker.controller;
import com.heima.freemarker.entity.Student; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;
import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List;
/** * @author 飞宇千虹 * @date 2023-06-23 11:55 */ @Controller public class HelloController { @GetMapping("/list") public String list(Model model){
//------------------------------------
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);
//将两个对象模型数据存放到List集合中
List<Student> stus = new ArrayList<>();
stus.add(stu1);
stus.add(stu2);
//向model中存放List集合数据
model.addAttribute("stus",stus);
//------------------------------------
//创建Map数据
HashMap<String,Student> stuMap = new HashMap<>();
stuMap.put("stu1",stu1);
stuMap.put("stu2",stu2);
// 3.1 向model中存放Map数据
model.addAttribute("stuMap", stuMap);
return "02-list";
}
} ```
页面模板02-list.ftl
``` package com.heima.freemarker.controller;
import com.heima.freemarker.entity.Student; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;
import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List;
/** * @author 飞宇千虹 * @date 2023-06-23 11:55 */ @Controller public class HelloController {
@GetMapping("/basic")
public String hello(Model model){
// name
model.addAttribute("name","xyy");
// stu
Student student = new Student();
student.setAge(22);
model.addAttribute("stu",student);
return "01-basic";
}
@GetMapping("/list")
public String list(Model model){
//------------------------------------
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);
//将两个对象模型数据存放到List集合中
List<Student> stus = new ArrayList<>();
stus.add(stu1);
stus.add(stu2);
//向model中存放List集合数据
model.addAttribute("stus",stus);
//------------------------------------
//创建Map数据
HashMap<String,Student> stuMap = new HashMap<>();
stuMap.put("stu1",stu1);
stuMap.put("stu2",stu2);
// 3.1 向model中存放Map数据
model.addAttribute("stuMap", stuMap);
return "02-list";
}
} ```
页面展示
指令语法
- 集合大小:
${集合名?size}
日期格式化:
显示年月日:
${today?date}
显示时分秒:
${today?time}
显示日期+时间:
${today?datetime}
自定义格式化:
${today?string("yyyy年MM月")}
内建函数c :将数字型转成字符串输出
${point?c}
json->对象
<#assign text="{'bank':'银行','account':'123'}"> <#assign data=text?eval> 开户行: ${data.bank} 账号:${data.account}
静态文件生成
``` @RunWith(SpringRunner.class) @SpringBootTest(classes = FreeDemoApplication.class) public class FreemarkerTest { @Resource private Configuration configuration; @Test public void Test() throws IOException, TemplateException { Template template = configuration.getTemplate("02-list.ftl"); /*合成方法 * * 两个参数, * 第一个,模型数据 * 第二个,输出流 * * */ template.process(getData(),new FileWriter("E:/桌面/learning-files/code/toutiao/day2/list.html")); }
```
``` private Map getData(){ HashMap map = new HashMap<>(); //------------------------------------ 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);
//将两个对象模型数据存放到List集合中
List<Student> stus = new ArrayList<>();
stus.add(stu1);
stus.add(stu2);
//向model中存放List集合数据
map.put("stus",stus);
//------------------------------------
//创建Map数据
HashMap<String,Student> stuMap = new HashMap<>();
stuMap.put("stu1",stu1);
stuMap.put("stu2",stu2);
// 3.1 向model中存放Map数据
map.put("stuMap",stuMap);
return map;
} ```