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

文件地址

展示

相关推荐
码拉松1 小时前
千万不要错过,优惠券设计与思考初探
后端·面试·架构
白总Server1 小时前
MongoDB解说
开发语言·数据库·后端·mongodb·golang·rust·php
计算机学姐2 小时前
基于python+django+vue的家居全屋定制系统
开发语言·vue.js·后端·python·django·numpy·web3.py
程序员-珍2 小时前
SpringBoot v2.6.13 整合 swagger
java·spring boot·后端
海里真的有鱼3 小时前
好文推荐-架构
后端
骆晨学长3 小时前
基于springboot的智慧社区微信小程序
java·数据库·spring boot·后端·微信小程序·小程序
AskHarries3 小时前
利用反射实现动态代理
java·后端·reflect
Flying_Fish_roe4 小时前
Spring Boot-Session管理问题
java·spring boot·后端
hai405874 小时前
Spring Boot中的响应与分层解耦架构
spring boot·后端·架构
Adolf_19936 小时前
Flask-JWT-Extended登录验证, 不用自定义
后端·python·flask