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;
    }
}
相关推荐
daidaidaiyu8 小时前
ThingsBoard 集群的核心逻辑源码分析
java
2601_962885729 小时前
如何用 Python 自动识别股票的支撑位与压力位?
开发语言·python
爱吃奥利奥_wen9 小时前
面向对象三板斧:封装、继承、多态,到底在“装“什么、“承“什么、“变“什么?
开发语言·经验分享·c#
我可能是个假开发9 小时前
FTP Unicode 文件名导致 `550` 的问题分析与解决方案
java·开发语言·spring
caoerzhong9 小时前
中小企业上 WMS 该先上哪几块:JeeWMS 开源 Java 仓库管理系统的分批上线清单
java·python·开源
WL学习笔记10 小时前
日期类(C++)
开发语言·c++·日期类实现
【JAVA】玩家10 小时前
Spring核心原理全解析:从零到生产实战
java·后端·spring
Wang's Blog11 小时前
Java框架 SpringCloud 快速入门: 实现 Feign 最佳实践(抽取方式)
java·spring cloud
谢亮_vipxieliang11 小时前
Go map与结构体的正确使用
开发语言·golang·哈希算法
H.莓飛11 小时前
【C++】命名空间、缺省参数、函数重载与引用
linux·开发语言·c++·visual studio