JAVA项目中freemarker静态模板技术

目录

需求分析

Freemarker概述

测试环境搭建

Freemarker基础语法种类

集合指令LIst

集合指令MAP

IF指令

运算符

空值处理

内建函数

输出静态化文件


需求分析

Freemarker概述

测试环境搭建

Freemarker基础语法种类

集合指令LIst

集合指令MAP

IF指令

运算符

空值处理

内建函数

输出静态化文件

java 复制代码
@SpringBootTest(classes = FreemarkerDemoApplication.class)
@RunWith(SpringRunner.class)
public class FreemarkerTest {
    @Autowired
    private Configuration configuration;

    @Test
    public void test() throws IOException, TemplateException {
        Template template = configuration.getTemplate("02-list.ftl");
//        合成方法
//        两个参数  模型数据   输出流
        template.process(getData(), new FileWriter("D:/list.html"));
    }
    private Map getData(){
        Map<String, Object> 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集合数据
//        model.addAttribute("stus",stus);

        map.put("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);
        map.put("stuMap", stuMap);
        return map;
    }
}
相关推荐
野猪亨利6672 分钟前
Qt day1
开发语言·数据库·qt
lastHertz9 分钟前
Golang 项目中使用 Swagger
开发语言·后端·golang
本就一无所有 何惧重新开始19 分钟前
Redis技术应用
java·数据库·spring boot·redis·后端·缓存
惜月_treasure20 分钟前
LlamaIndex多模态RAG开发实现详解
开发语言·python·机器学习
isaki13722 分钟前
qt day1
开发语言·数据库·qt
流星白龙30 分钟前
【Qt】4.项目文件解析
开发语言·数据库·qt
低音钢琴32 分钟前
【SpringBoot从初学者到专家的成长11】Spring Boot中的application.properties与application.yml详解
java·spring boot·后端
iuuia34 分钟前
05--JavaScript基础语法(1)
开发语言·javascript·ecmascript
郝学胜-神的一滴35 分钟前
深入解析Linux下的`lseek`函数:文件定位与操作的艺术
linux·运维·服务器·开发语言·c++·软件工程
一晌小贪欢43 分钟前
Python爬虫第4课:XPath与lxml高级解析技术
开发语言·爬虫·python·网络爬虫·python爬虫·python3·python办公