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;
    }
}
相关推荐
o0o_-_3 分钟前
【go/gopls/mcp】官方gopls内置mcp server使用
开发语言·后端·golang
wuxuanok13 分钟前
SpringBoot -原理篇
java·spring boot·spring
柿蒂15 分钟前
从if-else和switch,聊聊“八股“的作用
android·java·kotlin
二饭18 分钟前
Spring Boot 项目启动报错:MongoSocketOpenException 连接被拒绝排查日记
java·spring boot·后端
Dxy123931021618 分钟前
python把文件从一个文件复制到另一个文件夹
开发语言·python
懒虫虫~35 分钟前
通过内存去重替换SQL中distinct,优化SQL查询效率
java·sql·慢sql治理
鼠鼠我捏,要死了捏39 分钟前
基于Redisson的分布式锁原理深度解析与性能优化实践指南
java·高并发·redisson
backordinary1 小时前
微服务学习笔记25版
java·java-ee
酷飞飞1 小时前
Qt Designer与事件处理
开发语言·qt·命令模式
ZZHow10241 小时前
Maven入门_简介、安装与配置
java·笔记·maven