Java Freemarker基本操作

依赖

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

application.yml

yaml 复制代码
spring:
  freemarker:
    cache: false  #关闭模板缓存,方便测试
    settings:
    #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
      template_update_delay: 0 
    suffix: .ftl               #指定Freemarker模板文件的后缀名
    template-loader-path: classpath:/templates

案例一:加载单个属性、类

目录结构

controller

typescript 复制代码
@Controller
public class HelloController {

    @GetMapping("/basic")
    public String hello(Model model){
        // name
        model.addAttribute("name","xyy");
        // stu
        Student student = new Student();
        student.setName("xyz");
        model.addAttribute("stu",student);
        return "01-basic";
    }
  }

前端模板01-basic.ftl

xml 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Hello World!</title>
</head>
<body>
    <b>普通文本 String 展示:</b>
    <br>
        <br>Hello ${name!"空值"} <br>
    <hr>
    <b>对象Student中的数据展示:</b>
    <br/>
    姓名:${(stu.name)!"姓名空值"}<br/>
    年龄:${(stu.age)!"年龄空值"}
    <hr>
</body>
</html>

页面展示

案例二:加载Map、List

Controller

java 复制代码
package com.heima.freemarker.controller;

import com.heima.freemarker.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author 飞宇千虹
 * @date 2023-06-23 11:55
 */
@Controller
public class HelloController {
    @GetMapping("/list")
    public String list(Model model){

        //------------------------------------
        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数据
        HashMap<String,Student> stuMap = new HashMap<>();
        stuMap.put("stu1",stu1);
        stuMap.put("stu2",stu2);
        // 3.1 向model中存放Map数据
        model.addAttribute("stuMap", stuMap);

        return "02-list";
    }
}

页面模板02-list.ftl

java 复制代码
package com.heima.freemarker.controller;

import com.heima.freemarker.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author 飞宇千虹
 * @date 2023-06-23 11:55
 */
@Controller
public class HelloController {

    @GetMapping("/basic")
    public String hello(Model model){
        // name
        model.addAttribute("name","xyy");
        // stu
        Student student = new Student();
        student.setAge(22);
        model.addAttribute("stu",student);
        return "01-basic";
    }

    @GetMapping("/list")
    public String list(Model model){

        //------------------------------------
        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数据
        HashMap<String,Student> stuMap = new HashMap<>();
        stuMap.put("stu1",stu1);
        stuMap.put("stu2",stu2);
        // 3.1 向model中存放Map数据
        model.addAttribute("stuMap", stuMap);

        return "02-list";
    }
}

页面展示

指令语法

  1. 集合大小:${集合名?size}

  2. 日期格式化:

    显示年月日:${today?date}

    显示时分秒:${today?time}

    显示日期+时间:${today?datetime}

    自定义格式化:${today?string("yyyy年MM月")}

  3. 内建函数c :将数字型转成字符串输出

    ${point?c}

  4. json->对象

scss 复制代码
 <#assign text="{'bank':'银行','account':'123'}">
 <#assign data=text?eval>
 开户行: ${data.bank}  账号:${data.account}

静态文件生成

less 复制代码
@RunWith(SpringRunner.class)
@SpringBootTest(classes = FreeDemoApplication.class)
public class FreemarkerTest {
    @Resource
    private Configuration configuration;
    @Test
    public void Test() throws IOException, TemplateException {
        Template template = configuration.getTemplate("02-list.ftl");
        /*合成方法
        *
        * 两个参数,
        * 第一个,模型数据
        * 第二个,输出流
        *
        * */
        template.process(getData(),new FileWriter("E:/桌面/learning-files/code/toutiao/day2/list.html"));
    }
ini 复制代码
private Map getData(){
    HashMap<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集合数据
    map.put("stus",stus);

    //------------------------------------

    //创建Map数据
    HashMap<String,Student> stuMap = new HashMap<>();
    stuMap.put("stu1",stu1);
    stuMap.put("stu2",stu2);
    // 3.1 向model中存放Map数据

    map.put("stuMap",stuMap);

    return map;
}

文件地址

展示

相关推荐
来自旧金山的勇士27 分钟前
WSL->Ubunut安装Redis
后端
大葱白菜29 分钟前
Java Set 集合详解:从基础语法到实战应用,彻底掌握去重与唯一性集合
java·后端
大葱白菜30 分钟前
Java Map 集合详解:从基础语法到实战应用,彻底掌握键值对数据结构
java·后端
小猪乔治爱打球36 分钟前
[Golang修仙之路] 算法专题:回溯(递归)
后端·面试
昵称为空C44 分钟前
SpringBoot数据存储时区选择,符合国际化和特定时区方案
spring boot·后端
ldj20202 小时前
SpringBoot为什么使用new RuntimeException() 来获取调用栈?
java·spring boot·后端
超龄超能程序猿2 小时前
Spring 应用中 Swagger 2.0 迁移 OpenAPI 3.0 详解:配置、注解与实践
java·spring boot·后端·spring·spring cloud
江南一点雨2 小时前
Tokenizer 和 BPE
后端
风象南2 小时前
SpringBoot配置属性热更新的轻量级实现
java·spring boot·后端
洛阳泰山2 小时前
Spring Boot 整合 Nacos 实战教程:服务注册发现与配置中心详解
java·spring boot·后端·nacos