freemarker基本操作


theme: smartblue

依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>

application.yml

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

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

目录结构

image.png

controller

``` @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

<!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>

页面展示

image.png

案例二:加载Map、List

Controller

``` 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

``` 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";
}

} ```

页面展示

image.png

指令语法

  1. 集合大小:${集合名?size}
  2. 日期格式化:

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

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

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

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

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

    ${point?c}

  4. json->对象

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

静态文件生成

``` @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")); }

```

``` private Map getData(){ HashMap 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;

} ```

文件地址

image.png

展示

image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值