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;
    }
}
相关推荐
DYS_房东的猫3 小时前
《 C++ 零基础入门教程》第6章:模板与 STL 算法 —— 写一次,用万次
开发语言·c++·算法
诗意地回家3 小时前
淘宝小游戏反编译
开发语言·前端·javascript
wangkay883 小时前
【Java 转运营】Day04:抖音新号起号前准备全指南
java·开发语言·新媒体运营
点云SLAM3 小时前
C++ 静态初始化顺序问题(SIOF)和SLAM / ROS 工程实战问题
开发语言·c++·slam·静态初始化顺序问题·工程实战技术·c++static 关键字
D3bugRealm3 小时前
MATLAB解决物理问题:从基础运动学到进阶力学的实战指南
开发语言·其他·matlab
小李独爱秋3 小时前
计算机网络经典问题透视:TLS协议工作过程全景解析
运维·服务器·开发语言·网络协议·计算机网络·php
亲爱的非洲野猪3 小时前
Java线程池深度解析:从原理到最佳实践
java·网络·python
pen-ai3 小时前
打通 Python 与 C++ 的参数传递机制
开发语言·c++·python
亲爱的非洲野猪3 小时前
深入解析享元模式:用Java实现高性能对象复用
java·开发语言·享元模式
qq_401700413 小时前
Qt的.pro文件
开发语言·qt