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;
    }
}
相关推荐
Edward111111116 小时前
一AI名词
java·开发语言·数据库·学习
栈溢出了6 小时前
Redis repl_backlog 学习笔记
java·开发语言·redis·学习
urkay-6 小时前
Kotlin Flow分类
android·开发语言·kotlin
并不喜欢吃鱼6 小时前
从零开始 C++----- 十五.一文吃透 C++ 异常:try-catch、栈展开、自定义异常、异常安全 noexcept 底层全剖析
java·开发语言·jvm
gr95ZS6E66 小时前
基础入门java安全(一)--CC1基础分析
开发语言·python
智码看视界6 小时前
Day19 Spring Boot启动优化:启动时间从8秒降到2秒
java·spring boot·后端·启动优化
北京阿法龙科技有限公司7 小时前
AR智能眼镜安防应用核心指标:识别距离筑牢防线
java·开发语言·ar
杜子不疼.7 小时前
【C++】继承—C++的秘密武器,get父类的智慧
开发语言·c++
殘殤血7 小时前
Tomcat的事件监听机制:观察者模式 _
java·观察者模式·tomcat
汤姆yu7 小时前
面向具身智能的物理视频模拟器:蚂蚁灵波LingBot-Video开源模型全解析
java·大数据·人工智能·gpt·开源·大模型·音视频